Add ShellAPI and Messages to your project's uses:
Add a method to your form that handles the WM_DROPFILES message.
For example, the following would be placed in the TForm1 declaration in the protected section:
Código Delphi
[-]
protected
procedure WMDropFiles(var Msg: TWMDropFiles); message WM_DROPFILES;
Drag 'n' drop are activated by calling:
Código Delphi
[-]
DragAcceptFiles(Handle, true);
and deactivated by calling:
Código Delphi
[-]DragAcceptFiles(Handle, false);
It is normal to activate drag 'n' drop in the OnCreate event, and deactivate it in the OnClose or OnDestroy events:
Código Delphi
[-]
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, true);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
DragAcceptFiles(Handle, false);
end;
This first procedure will enable you to drop files one at a time:
Código Delphi
[-]
procedure TForm1.WMDropFiles(var Msg: TWMDropFiles);
var filename: PChar;
length: LongWord;
begin
length:= DragQueryFile(Msg.Drop, 0, NIL, 0);
filename:= StrAlloc(length+1);
DragQueryFile(Msg.Drop,0,filename,length+1);
Msg.Result:=0;
inherited;
end;