Using Drag and Drop with Files and Folders in Visual Basic
In this article I will carry on from last week when I showed you how to search you computer for a specified program. This week I will show you how to allow the user to be able to drag a file or folder onto a list box or textbox and it will display the relative path and file name if you dragged a file. It will also tell you how many files you last dropped to the control on the form.
This is very useful, when you want a number of files, say to check the integrity of the specified file. It also can be used for many other functions, including the path of where certain files are. This can be accomplished by using four Windows API's. Which are:
I have included an example of how this works below. If you would like the sample project then click here!
1. Start a new standard EXE project in Visual Basic. Form one is created by default
2. Add a normal module to the project, by clicking on the Project menu and choosing insert module.
3. You will need to declare the following types, by typing the following in to the beginning of the module
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
4. You now need to add the following Windows API calls to the module. Make sure that each API is on one line
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 Public Const PM_NOREMOVE = &H0 Public Const PM_NOYIELD = &H2 Public Const PM_REMOVE = &H1 Public Const WM_DROPFILES = &H233
5. You will now need to add the following procedures to the module.
' This is the sub main procedure, that is called ' for any of the forms are loaded.Sub Main() ' In order for this to function properly you should ' place of of your program execution code in ' the Sub Main(), Make sure you change the project ' startup to sub Main Form1.Show ' This must be the last line! Nothing gets called ' after this WatchForFiles End Sub Public Sub WatchForFiles() ' This subroutine 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 setDo ' 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 DoEvents Loop End Sub
6. Open up form one. Draw the following
- 3 command buttons
- 1 list box
- 2 labels - one called "lblNumDropped"
7. Now open up the code window for form1 and type the following
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 True/False as the ' SecondIf 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 Command3_Click() ' End the program End End Sub Private Sub Form_Unload(Cancel As Integer) End End Sub Private Sub List1_Click() Dim x, y, z Dim AnyString, MyStr z = Right(List1.Text, 3) ' Define string. If z = "EXE" Then y = List1.Text x = Shell(y, vbNormalFocus) Else y = "Explorer " & List1.Text x = Shell(y, vbNormalFocus) End If End Sub Private Sub Form_Load() Command1.caption = "&Accept Files" Command2.caption = "Clear" Command3.caption = "End" Label1.caption = "The number of files that were " & _ "last dropped on the list box" End Sub
8. Go into the Project Properties dialog box and change the Startup Object to Sub Main.
9. Now run the project. Click on the Accept files command button. Now drag some files and folder on the list box. They will be disaplyed in the list box. If you click on a file or folder in the list box the folder or file will be opened.