HOW TO: Drag and drop files from the Explorer to the control
Of course, this so-called file refers to the full file name, as for the content of the file, it must be further operated according to the actual situation.
The controls I here are a listbox. code show as below:
Private Sub Form1_Load (Byval E AS System.Object, Byval E AS System.Eventargs) Handles MyBase.Load
Me.ListBox1.Allowdrop = TRUE
End Sub
PRIVATE SUB ListBox1_Dragenter (Byval e as system.windows.forms.drageventargs) Handles ListBox1.dragenter
Dim o as object = e.data.getdata (dataformats.filedro)
IF not o is nothing then
e.effect = DragDropeffects.copy
END IF
End Sub
PRIVATE SUB ListBox1_dragdrop (Byval e as system.windows.forms.drageventargs) Handles ListBox1.dragdrop
Dim filenames as string () = ctype (E.Data.GetData (dataformats.filedro), string ())
Me.ListBox1.Items.addrange (filenames)
End Sub
Rewinding this how to, the main thing is to see some of the API implementation, the code is as follows:
Private const wm_dropfiles as integer = 563
Private Declare Function DragacceptFiles LIB "shell32.dll" (byval accept as boolean) AS Long
Private Declare Function DragqueryFile Lib "shell32.dll" (Byval File as INTEGER, BYVAL FileName As System.Text.StringBuilder, Byval Size As INT32) AS INT32
Private Declare Sub Dragfinish LIB "Shell32.dll" (Byval HDROP AS INTPTR)
Protected Overrides Sub WndProc (Byref M As System.windows.Forms.Message)
IF m.msg = wm_dropfiles then
Dim Inumoffiles As Int32 = Dragqueryfile (M.WParam, & HFFFFFFF, NOTING, 0)
DIM IPNT AS INT32
For iPnt = 0 to inumoffiles - 1
DIM SB As New System.Text.StringBuilder (256)
DIM IRET AS INT32 = DragQueryFile (M.WPARAM, IPNT, SB, Sb.capacity)
ListBox1.Items.add (sb.tostring)
NEXT
Dragfinish (m.wparam)
Else
MyBase.WndProc (M) endiff
End Sub
Private Sub Form1_Load (Byval E AS ISTEM.EventArgs) Handles MyBase.Load
Dragacceptfiles (ListBox1.Handle, True)
End Sub