Image of Navigational Map linked to Home / Contents / Search Drag and Drop Enabling VB

by Dave Thompson - GUI Computing
Image of Line Break

This article explains how to get your VB program to accept files dropped from File Manager or Explorer - something I'd always known about but never actually needed to do until recently.

First, you must register that your window is interested in accepting dropped files. This can be done using the windows function DragAcceptFiles. The function accepts 2 arguments, the handle of the registering window and a flag to indicate if it accepts dropped files.

Declare Sub DragAcceptFiles Lib "shell.dll" (ByVal hWnd As Integer, ByVal bAccept As Integer)

Note that the window handle parameter can refer to any window. It could be your main application window, or it could be a control on a form. In this example a listbox called List1 is used. If you get this right, then the cursor will change when files are dragged over List1. The Form_Load event is a good place to do this.

The next part is to respond to the drop. When the files are dropped, windows sends a WM_DROPFILES message to the registered window. This message can be intercepted using message blaster, or any other custom control that translates windows messages into VB events. When you get the WM_DROPFILES message, you can use the DragQueryFile function to determine how many and which files were dropped.

Declare Function DragQueryFile Lib "shell.dll" (ByVal hDrop As Integer, ByVal iFile As Integer, ByVal sFile As String, ByVal iSize As Integer) As Integer

The wParam parameter of the WM_DROPFILES message is used as the drop handle in calls to DragQueryFile. The iFile parameter refers to which file you want to find out the name of. If iFile is -1, DragQueryFile returns the number of files dropped on the registered window. sFile is an output parameter that contains the file name. Remember to initialise this paramter with space$ first, otherwise an error may occur. iSize indicates the length of sFile, so that DragQueryFile does not overwrite more than it should. The following code segment adds the name of all the dropped files to List1.

Sub MsgBlaster1_Message (MsgVal As Integer, wParam As Integer, lParam As Long, ReturnVal As Long)

    Dim hDrop As Integer
    Dim iFiles As Integer
    Dim sFile As String
    Dim i As Integer
    Dim iResult As Integer

    List1.Clear
    hDrop = wParam
    iFiles = DragQueryFile(hDrop, -1, "", 0)
    For i = 0 To iFiles - 1
        sFile = Space$(255)
        iResult = DragQueryFile(hDrop, i, sFile, Len(sFile))
        List1.AddItem sFile
    Next i

    DragFinish hDrop

End Sub

You should call the DragFinish function when you are finished processing the files. This tells windows to release memory used to store the filenames.

Declare Sub DragFinish Lib "shell.dll" (ByVal hDrop As Integer)

Finally, when your form is unloaded, you should use DragAcceptFiles to tell windows you are no longer interested in accepting dropped files.

I've included a small example for your programming pleasure.



Written by: Dave Thompson
October '97

Image of Arrow linked to Previous Article Image of Arrow linked to Next Article
Image of Line Break
[HOME] [TABLE OF CONTENTS] [SEARCH]