Image of Navigational Map linked to Home / Contents / Search Add File Manager Drag-Drop Support to Your VB App

Dan Appleman - Desaware
Image of Line Break

File Drag-And-Drop is becoming a popular method for passing file information between applications. One typical use of file drag-and-drop is to use File Manager or Explorer to drag files into an application that can accept dropped files. This will pass the file names and paths of all the files dropped to the receiving application. In order for a window to receive dropped files, it has to have a special style flag set. Setting the WS_EX_ACCEPTFILES style flag for a particular window allows that window to accept dropped files. You can call the DragAcceptFiles API function (from the shell library) to set this flag. When a file is dragged outside of File Manager or Explorer, the mouse cursor is constantly monitoring the window it overlays, testing to see whether the window’s WS_EX_ACCEPTFILES style bit is set. Once a file is dropped onto a window that accepts dropped files, a WM_DROPFILES message is posted to that window. Upon receiving this message, you may call the DragQueryFile API function to retrieve the names and paths of the dropped files.

It’s easy with dweasy

Unfortunately, Visual Basic does not support this feature since it requires subclassing the WM_DROPFILES message. You can use SpyWorks’ subclass control to do this but the new drag-drop feature in dweasy makes this even easier. Dweasy has a new property call DetectDropped. When this property is set to True, it sets the WS_EX_ACCEPTFILES style for dweasy’s container and all the container’s child windows. Dweasy will then subclass these windows for the WM_DROPFILES message. Setting another new dweasy property, FileDropHwnd to a window handle will set the WS_EX_ACCEPTFILES style for that specified window and cause dweasy to subclass it for the WM_DROPFILES message. When files are dropped to a window subclassed by dweasy, the dweasy’s FilesDropped event is triggered. Inside this event, you can access dweasy’s DroppedFile array to retrieve the paths and names of the files that were dropped.

How easy is it? Here’s a sample program that will display a list of droppedfiles. Place a list box and a dweasy control on a form. Set the DetectDropped property for the dwEasy control to True. Add the following code to the FilesDropped event.

  Private Sub SbcEasy1_FilesDropped(ByVal FileCount As Long)
    Dim cnt&
    For cnt = 0 To FileCount-1
      List1.AddItem SbcEasy1.DroppedFile(cnt)
    Next cnt
  End Sub

That’s all there is to it!

Written by: Dan Appleman
November '96

Image of Arrow linked to Next Article
Image of Line Break