偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

高手用實(shí)例談VB.NET拖放文件

開(kāi)發(fā) 后端
這里介紹自從資源管理器中拖放到應(yīng)用程序中的時(shí)候,自動(dòng)獲取VB.NET拖放文件。文中的例子是一個(gè)接受VB.NET拖放文件顯示文件內(nèi)容的VB.NET實(shí)例程序。

#t#VB.NET還是比較常用的,于是我研究了一下VB.NET拖放文件,本文介紹了在VB.NET中如何實(shí)現(xiàn)接受VB.NET拖放文件,在這里拿出來(lái)和大家分享一下,希望對(duì)大家有用。自從資源管理器中拖放到應(yīng)用程序中的時(shí)候,自動(dòng)獲取VB.NET拖放文件。文中的例子是一個(gè)接受VB.NET拖放文件顯示文件內(nèi)容的VB.NET實(shí)例程序。

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

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

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

于是我們?cè)诔绦蚣虞d的時(shí)候,調(diào)用Application.AddMessageFilter(Me)。然而,默認(rèn)情況下一個(gè)Form或者控件是不能接受VB.NET拖放文件的,我們調(diào)用一個(gè)WIN32 API DragAcceptFiles,這個(gè)API可以設(shè)置對(duì)應(yīng)的控件是否能接受VB.NET拖放文件。然后可以用DragQueryFile查詢(xún)拖放到的文件列表,也就是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è)置拖放的動(dòng)作  
  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 '記錄***個(gè)文件名  
  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 
責(zé)任編輯:佚名 來(lái)源: 博客園
相關(guān)推薦

2009-10-28 13:35:25

VB.NET共享成員

2009-10-29 09:40:35

VB.NET Text

2009-10-21 18:28:48

VB.NET表間拖放

2010-01-13 17:47:59

VB.NET拖放

2009-11-11 13:24:29

VB.NET事件

2009-10-21 18:19:36

VB.NET實(shí)現(xiàn)拖放

2009-10-28 09:25:18

VB.NET List

2009-10-28 14:13:32

VB.NET File

2009-10-28 14:34:44

VB.NET Tree

2010-01-19 18:24:29

VB.NET調(diào)用Win

2009-11-02 11:11:07

VB.NET OOP設(shè)

2009-11-04 11:32:20

VB.NET回調(diào)函數(shù)

2009-10-26 16:53:00

VB.NET常用代碼

2009-10-27 16:36:46

VB.NET文件流

2009-10-22 09:20:46

VB.NET Proc

2009-10-21 09:40:23

VB.NET搜索

2009-10-26 19:22:29

VB.NET使用Log

2009-10-30 11:07:04

VB.NET Syst

2009-11-02 12:35:10

VB.NET追加文件

2009-10-29 15:16:02

VB.NET文件傳送
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)