1. We set the window or control to accept files using the _WS_EX_ACCEPTFILES stylebit. This uses a texteditor control, although it could be used with anything.
2. We use WMLiberty to capture the _WM_DROPFILES message on the window/control.
callback lpfnCallback, WMHandler(ULong, ULong, ULong, ULong), ULong
hCtrl = hwnd(#WIN.CTRL)
Open"WMLiberty"for DLL as #wm
CallDLL #wm, "SetWMHandler",_
hCtrl as ulong,_
_WM_DROPFILES as ulong,_
lpfnCallback as ulong,_
0 aslong,_
ret aslong 'If successful, ret=0
3. We use the Windows API to get the information about the dropped files. We mainly use 2 functions: DragQueryFileA(), and DragFinish(), both of which are in Shell32.dll. This code will get the number of files and loop through them, displaying each of their names in a texteditor.
Function WMHandler(hWnd, uMsg, wParam, lParam)
hDrop = wParam
a = hexdec("FFFFFFFF")
CallDLL #shell32, "DragQueryFileA",_ 'Call DragQueryFile with 0xFFFFFFFF as the file number to get the number of files
hDrop as ulong,_
a as ulong,_
0 aslong,_
0 aslong,_
ret aslong
#WIN.CTRL, "!cls"'Loop through the files and display their names
For x = 0 to ret - 1
buf$ = space$(1024)
CallDLL #shell32, "DragQueryFileA",_
hDrop as ulong,_
x as ulong,_
buf$ as ptr,_
1024 aslong,_
ret aslong
#WIN.CTRL, trim$(buf$)
Next x
CallDLL #shell32, "DragFinish",_ 'Free the memory used for the DragQueryFile array
hDrop as ulong,_
ret aslong
WMHandler = 0
EndFunction
A working demo(again, you need WMLiberty to use this):
open"WMLiberty"for DLL as #wm
'Form created with the help of Freeform 3 v03-27-03
'Generated on Mar 24, 2008 at 12:01:29
[setup.m.Window]
'-----Begin code for #m
callback lpfnCallback, WMHandler(ULong, ULong, ULong, ULong), ULong
nomainwin
WindowWidth = 550
WindowHeight = 410
UpperLeftX=int((DisplayWidth-WindowWidth)/2)
UpperLeftY=int((DisplayHeight-WindowHeight)/2)
'-----Begin GUI objects code
TexteditorColor$ = "white"
stylebits #m.te, 0, 0, _WS_EX_ACCEPTFILES, 0
texteditor #m.te, 20, 27, 470, 290
'-----End GUI objects code
'-----Begin menu code
menu #m, "File", "Exit", QuitF
menu #m, "Edit"' <-- Texteditor menu.
'-----End menu code
open"Drag and Drop Test"for window as #m
print #m, "font ms_sans_serif 10"print #m, "trapclose Quit"
hTe = hwnd(#m.te)
CallDLL #wm, "SetWMHandler",_
hTe as ulong,_
_WM_DROPFILES as ulong,_
lpfnCallback as ulong,_
0 aslong,_
ret aslong
[wait]
scan
calldll #kernel32, "Sleep", 50 aslong, r as void
goto [wait]
Sub Quit hndl$
Close #hndl$
Close #wm
ENDEndSubSub QuitF
Call Quit "#m"EndSubFunction WMHandler(hWnd, uMsg, wParam, lParam)
hDrop = wParam
a = hexdec("FFFFFFFF")
CallDLL #shell32, "DragQueryFileA",_ 'Call DragQueryFile with 0xFFFFFFFF as the file number to get the number of files
hDrop as ulong,_
a as ulong,_
0 aslong,_
0 aslong,_
ret aslong
#m.te, "!cls"For x = 0 to ret - 1
buf$ = space$(1024)
CallDLL #shell32, "DragQueryFileA",_
hDrop as ulong,_
x as ulong,_
buf$ as ptr,_
1024 aslong,_
ret aslong
#m.te, trim$(buf$)
Next x
CallDLL #shell32, "DragFinish",_ 'Free the memory used for the DragQueryFile array
hDrop as ulong,_
ret aslong
WMHandler = 0
EndFunction
Drag and Drop using _WS_EX_ACCEPTFILES
Please note: This program requires the WMLiberty DLL, available at Bay 6 Software:
http://www.b6sw.com/forum/content.php?mode=site&t=218
To use drag and drop, we do three things:
1. We set the window or control to accept files using the _WS_EX_ACCEPTFILES stylebit. This uses a texteditor control, although it could be used with anything.
2. We use WMLiberty to capture the _WM_DROPFILES message on the window/control.
3. We use the Windows API to get the information about the dropped files. We mainly use 2 functions: DragQueryFileA(), and DragFinish(), both of which are in Shell32.dll. This code will get the number of files and loop through them, displaying each of their names in a texteditor.
A working demo(again, you need WMLiberty to use this):
-