Drag and
Drop from FileManager
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
uses
ShellApi;
..
procedure
TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Form1.Handle, true);
Application.OnMessage := AppMessage;
end;
{ message
handler procedure }
// Delphi
1: type DWord = longint; .. FileIndex := -1;
procedure
TForm1.AppMessage(var Msg: Tmsg; var Handled: Boolean);
const
BufferLength : DWORD = 511;
var
DroppedFilename : string;
FileIndex : DWORD;
NumDroppedFiles : DWORD;
pDroppedFilename : array [ ..511] of Char;
DroppedFileLength : DWORD;
begin
if Msg.message = WM_DROPFILES then
begin
FileIndex := $FFFFFFFF;
NumDroppedFiles :=
DragQueryFile(Msg.WParam, FileIndex,
pDroppedFilename,
BufferLength);
for FileIndex := 0 to (NumDroppedFiles
- 1) do
begin
DroppedFileLength :=
DragQueryFile(Msg.WParam, FileIndex,
pDroppedFilename,
BufferLength);
DroppedFilename :=
StrPas(pDroppedFilename);
{ process the file name you just
received }
end;
DragFinish(Msg.WParam); { important to
free memory }
Handled := true;
end;
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.