Let them drag and drop files on your program
See Also
• Document Scraps:
Save bits and pieces of your files (Windows tip)
• Drag and drop file
names to the Command Prompt (Windows tip)
• Drag and view
(Windows tip)
• Dragging and
dropping objects between full screen applications (Windows tip)
• Explorations
shouldn't be limited to remote resources (Internet tip)
• Fastest way to add
items to your Start Menu (Windows tip)
If you want to let your users drag and drop files on your
program from the File Manager and Windows Explorer, simply add the code inside
//>>> and //<<< to your program as in the following example:
unit dropfile;
interface
uses
Windows, Messages,
SysUtils, Classes,
Graphics, Controls,
Forms, Dialogs;
type
TForm1 =
class(TForm)
procedure
FormCreate(Sender: TObject);
private
{ Private
declarations }
public
{ Public
declarations }
//>>>
// declare our
DROPFILES message handler
procedure
AcceptFiles( var msg : TMessage );
message
WM_DROPFILES;
//<<<
end;
var
Form1: TForm1;
implementation
uses
//>>>
//
// this unit
contains certain
// functions that
we'll be using
//
ShellAPI;
//<<<
{$R *.DFM}
//>>>
procedure TForm1.AcceptFiles( var msg : TMessage );
const
cnMaxFileNameLen =
255;
var
i,
nCount : integer;
acFileName : array
[0..cnMaxFileNameLen] of char;
begin
// find out how
many files we're accepting
nCount :=
DragQueryFile( msg.WParam,
$FFFFFFFF,
acFileName,
cnMaxFileNameLen );
// query Windows
one at a time for the file name
for i := 0 to
nCount-1 do
begin
DragQueryFile(
msg.WParam, i,
acFileName, cnMaxFileNameLen );
// do your thing
with the acFileName
MessageBox(
Handle, acFileName, '', MB_OK );
end;
// let Windows know
that you're done
DragFinish(
msg.WParam );
end;
//<<<
procedure
TForm1.FormCreate(Sender: TObject);
begin
//>>>
//
// tell Windows
that you're
// accepting drag
and drop files
//
DragAcceptFiles(
Handle, True );
//<<<
end;
end.
Listing #1 : Delphi code. Right click dropfile.pas to
download.
Now you can drag and drop files on the form that you
registered as a recipient of dropped files by calling the
"DragAcceptFiles()" function as in the above example.