Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Application.AddMessageFilter(Me)
DragAcceptFiles(TextBox1.Handle.ToInt32, True)
End Sub
Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
If m.Msg = WM_DROPFILES Then
'设置拖放的动作
Dim nfiles As Int16
nfiles = DragQueryFile(m.WParam.ToInt32, -1, Nothing, 0)
Dim i As Int16
Dim sb As New System.Text.StringBuilder(256)
Dim sFirstFileName As String '记录第一个文件名
TextBox1.Clear()
For i = 0 To nfiles - 1
DragQueryFile(m.WParam.ToInt32, i, sb, 256)
If i = 0 Then sFirstFileName = sb.ToString
TextBox1.AppendText(ControlChars.CrLf & sb.ToString)
Next
DragFinish(m.WParam.ToInt32) '拖放完成
'显示文件内容
Dim fs As New System.IO.FileStream(sFirstFileName, IO.FileMode.Open)
Dim sr As New System.IO.StreamReader(fs, System.Text.Encoding.GetEncoding("gb2312"))
TextBox1.AppendText(ControlChars.CrLf & sr.ReadToEnd().ToString)
fs.Close()
sr.Close()
End If
Return False
End Function
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
Application.RemoveMessageFilter(Me)
DragAcceptFiles(TextBox1.Handle.ToInt32, False)
MyBase.Dispose(disposing)
End Sub |