Older Version Newer Version

ChrisIverson ChrisIverson Mar 24, 2008

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.
 stylebits #WIN.CTRL, 0, 0, _WS_EX_ACCEPTFILES, 0 
texteditor #WIN.CTRL, 20, 27, 470, 290

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 as long,_
ret as long '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 as long,_
0 as long,_
ret as long

#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 as long,_
ret as long

#WIN.CTRL, trim$(buf$)
Next x

CallDLL #shell32, "DragFinish",_ 'Free the memory used for the DragQueryFile array
hDrop as ulong,_
ret as long

WMHandler = 0
End Function




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 as long,_
ret as long

[wait]
scan
calldll #kernel32, "Sleep", 50 as long, r as void
goto [wait]


Sub Quit hndl$
Close #hndl$
Close #wm
END
End Sub

Sub QuitF
Call Quit "#m"
End Sub

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 as long,_
0 as long,_
ret as long

#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 as long,_
ret as long
#m.te, trim$(buf$)
Next x

CallDLL #shell32, "DragFinish",_ 'Free the memory used for the DragQueryFile array
hDrop as ulong,_
ret as long

WMHandler = 0
End Function


- thedarkfreak thedarkfreak Mar 24, 2008 (Chris Iverson)