Author: William Gerbert
Drag and Drop from FileManager
Answer:
You need to use these 3 functions from the ShellApi:
DragAcceptFiles - registers whether a window accepts dropped files
DragQueryFile - retrieves the filename of a dropped file
DragFinish - releases memory allocated for dropping files
1 uses2 ShellApi;
3 4 ..
5 6 procedure TForm1.FormCreate(Sender: TObject);
7 begin8 DragAcceptFiles(Form1.Handle, true);
9 Application.OnMessage := AppMessage;
10 end;
11 12 { message handler procedure }13 // Delphi 1: type DWord = longint; .. FileIndex := -1;14 15 procedure TForm1.AppMessage(var Msg: Tmsg; var Handled: Boolean);
16 const17 BufferLength: DWORD = 511;
18 var19 DroppedFilename: string;
20 FileIndex: DWORD;
21 NumDroppedFiles: DWORD;
22 pDroppedFilename: array[0..511] of Char;
23 DroppedFileLength: DWORD;
24 begin25 if Msg.message = WM_DROPFILES then26 begin27 FileIndex := $FFFFFFFF;
28 NumDroppedFiles := DragQueryFile(Msg.WParam, FileIndex,
29 pDroppedFilename, BufferLength);
30 31 for FileIndex := 0 to (NumDroppedFiles - 1) do32 begin33 DroppedFileLength := DragQueryFile(Msg.WParam, FileIndex,
34 pDroppedFilename, BufferLength);
35 DroppedFilename := StrPas(pDroppedFilename);
36 37 { process the file name you just received }38 end;
39 DragFinish(Msg.WParam); { important to free memory }40 Handled := true;
41 end;
42 end;
Notes:
When dropping files, the DroppedFilename is the complete path, not just the
filename.ext
It is possible to drag and drop just a directory. So if you are expecting
filenames, you have to check for existence yourself.
The filenames come in uppercased.