溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何用實例分析VB.NET拖放文件

發(fā)布時間:2021-12-01 14:22:13 來源:億速云 閱讀:168 作者:柒染 欄目:編程語言

如何用實例分析VB.NET拖放文件,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。

VB.NET還是比較常用的,于是我研究了一下VB.NET拖放文件,下面介紹在VB.NET中如何實現(xiàn)接受VB.NET拖放文件。

自從資源管理器中拖放到應用程序中的時候,自動獲取VB.NET拖放文件。文中的例子是一個接受VB.NET拖放文件顯示文件內(nèi)容的VB.NET實例程序。

對于文本格式的文件,我們可以直接拖到記事本中就可以看到內(nèi)容;各種類型的圖片,拖到Photoshop中,就可以直接對其編輯。我們?nèi)绾卧赩B.NET開發(fā)的程序也實現(xiàn)上述效果呢?

我們知道,每一個Windows的應用程序都有一個消息隊列,程序的主體接受系統(tǒng)的消息,然后分發(fā)出去(給一個form,或者一個控件),接受者有相應的程序來處理消息。在.NET的Form中,默認情況下程序是不翻譯這些消息的,也就是說默認我們的Class是不加入應用程序的消息泵。能不能把我們的Form Class加入應用程序的消息泵呢?可以!

在.NET中,任何一個實現(xiàn)IMessageFilter 接口的類,可以添加到應用程序的消息泵中,以在消息被調(diào)度到控件或窗體之前將它篩選出來或執(zhí)行其他操作。使用 Application 類中的 AddMessageFilter 方法,可以將消息篩選器添加到應用程序的消息泵中。

于是我們在程序加載的時候,調(diào)用Application.AddMessageFilter(Me)。然而,默認情況下一個Form或者控件是不能接受VB.NET拖放文件的,我們調(diào)用一個WIN32 API DragAcceptFiles,這個API可以設(shè)置對應的控件是否能接受VB.NET拖放文件。然后可以用DragQueryFile查詢拖放到的文件列表,也就是VB.NET拖放文件地具體路徑和文件名。

  1. Imports System.Runtime.InteropServices  

  2. Public Class Form1  

  3. Inherits System.Windows.Forms.Form  

  4. Implements IMessageFilter  

  5. 'API申明  

  6. Const WM_DROPFILES = &H233‘拖放文件消息  

  7. <DllImport("shell32.dll")> Public Shared Sub DragFinish(ByVal hDrop As Integer)  

  8. End Sub  

  9. <DllImport("shell32.dll")> Public Shared Sub DragAcceptFiles
    (ByVal hwnd As Integer, ByVal fAccept As Boolean)  

  10. End Sub  

  11. <DllImort("shell32.dll")> Public Shared Function DragQueryFile(ByVal HDROP As Integer, 
    ByVal UINT As Integer, ByVal lpStr As System.Text.StringBuilder, ByVal ch As Integer) As Integer  

  12. End Function  

  13. Private Sub Form1_Load(ByVal sender As System.Object, 
    ByVal e As System.EventArgs) Handles MyBase.Load  

  14. Application.AddMessageFilter(Me)  

  15. DragAcceptFiles(TextBox1.Handle.ToInt32, True)  

  16. End Sub  

  17. Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage  

  18. If m.Msg = WM_DROPFILES Then  

  19. '設(shè)置拖放的動作  

  20. Dim nfiles As Int16  

  21. nfiles = DragQueryFile(m.WParam.ToInt32, -1, Nothing, 0)  

  22. Dim i As Int16  

  23. Dim sb As New System.Text.StringBuilder(256)  

  24. Dim sFirstFileName As String '記錄***個文件名  

  25. TextBox1.Clear()  

  26. For i = 0 To nfiles - 1  

  27. DragQueryFile(m.WParam.ToInt32, i, sb, 256)  

  28. If i = 0 Then sFirstFileName = sb.ToString  

  29. TextBox1.AppendText(ControlChars.CrLf & sb.ToString)  

  30. Next  

  31. DragFinish(m.WParam.ToInt32) '拖放完成  

  32. '顯示文件內(nèi)容  

  33. Dim fs As New System.IO.FileStream(sFirstFileName, IO.FileMode.Open)  

  34. Dim sr As New System.IO.StreamReader(fs, System.Text.Encoding.GetEncoding("gb2312"))  

  35. TextBox1.AppendText(ControlChars.CrLf & sr.ReadToEnd().ToString)  

  36. fs.Close()  

  37. sr.Close()  

  38. End If  

  39. Return False  

  40. End Function  

  41. Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)  

  42. If disposing Then  

  43. If Not (components Is Nothing) Then  

  44. components.Dispose()  

  45. End If  

  46. End If  

  47. Application.RemoveMessageFilter(Me)  

  48. DragAcceptFiles(TextBox1.Handle.ToInt32, False)  

  49. MyBase.Dispose(disposing)  

  50. End Sub 

看完上述內(nèi)容是否對您有幫助呢?如果還想對相關(guān)知識有進一步的了解或閱讀更多相關(guān)文章,請關(guān)注億速云行業(yè)資訊頻道,感謝您對億速云的支持。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI