| This cool little tip uses the DragAcceptFiles API to detect the dragging and dropping of files onto a list box. To use this code, add a module (Dragdrop.bas) and a form (Form1). Add 2 command buttons to the form (Command1 & Command2), a label (lblNumdropped) and a list box (List1). Set the command buttons caption to &Accept Files. Code: Copy the following code into the module: ' Types Required Type POINTAPI x As Long y As Long End Type Type MSG hWnd As Long message As Long wParam As Long lParam As Long time As Long pt As POINTAPI End Type ' API Declarations Declare Sub DragAcceptFiles Lib "shell32.dll" (ByVal hWnd As Long, ByVal fAccept As Long) Declare Sub DragFinish Lib "shell32.dll" (ByVal hDrop As Long) Declare Function DragQueryFile Lib "shell32.dll" Alias "DragQueryFileA" (ByVal hDrop As _ Long, ByVal UINT As Long, ByVal lpStr As String, ByVal ch As Long) As Long Declare Function PeekMessage Lib "user32" Alias "PeekMessageA" (lpMsg As MSG, ByVal _ hWnd As Long, ByVal wMsgFilterMin As Long, ByVal wMsgFilterMax As Long, ByVal _ wRemoveMsg As Long) As Long ' Constants Public Const PM_NOREMOVE = &H0 Public Const PM_NOYIELD = &H2 Public Const PM_REMOVE = &H1 Public Const WM_DROPFILES = &H233 Sub Main() Form1.Show ' This must be the last line! Nothing gets called after this WatchForFiles End Sub Public Sub WatchForFiles() ' This subrountine watchs for all of your WM_DROPFILES messages ' Dim Variables Dim FileDropMessage As MSG ' Msg Type Dim fileDropped As Boolean ' True if Files where dropped Dim hDrop As Long ' Pointer to the dropped file structure Dim filename As String * 128 ' the dropped filename Dim numOfDroppedFiles As Long ' the amount of dropped files Dim curFile As Long ' the current file number ' loop to keep checking for files ' NOTE : Do any code you want to execute before this set Do ' check for Dropped file messages fileDropped = PeekMessage(FileDropMessage, 0, WM_DROPFILES, WM_DROPFILES, _ PM_REMOVE Or PM_NOYIELD) If fileDropped Then ' Get the pointer to the dropped file structure hDrop = FileDropMessage.wParam ' Get the toal number of files numOfDroppedFiles = DragQueryFile(hDrop, True, filename, 127) For curFile = 1 To numOfDroppedFiles ' Get the file name ret% = DragQueryFile(hDrop, curFile - 1, filename, 127) ' at this pointer you can do what you want with the filename ' the filename will be a full qalified path Form1.lblNumDropped = LTrim$(Str$(numOfDroppedFiles)) Form1.List1.AddItem filename Next curFile ' We are now done with the structure, tell windows to discard it DragFinish (hDrop) End If ' Be nice and DoEvents DoEvents Loop End Sub Copy this code into the form's General Declarations procedure: Private Sub Command1_Click() ' You can turn the form's / controls ability to accept the files by passing the hWnd as ' the first parameter and Ture/False as the Second If Command1.Caption = "&Accept Files" Then ' allow the application to accept files DragAcceptFiles Form1.hWnd, True Command1.Caption = "&Do Not Accept" Else DragAcceptFiles Form1.hWnd, False Command1.Caption = "&Accept Files" End If End Sub Private Sub Command2_Click() ' Clears the contents of the list box List1.Clear End Sub Private Sub Form_Unload(Cancel As Integer) End End Sub Set the projects Startup Object to Sub Main. |
Drag/Drop files onto a Listbox |
Express News India | Freelance ecommerce web development India