yoy.be "Why-o-Why"

Enable dropping files onto your application

2005-06-08 22:18  i96  delphi  [permalink]

Check if the Messages and ShellApi units are included in the uses clause in the start of your unit.

Add a call to DragAcceptFiles in the initialization of the form (OnCreate or OnShow).

Add this bit to the protected clause in the form class declaration. By default there is no need for a protected clause, so if it's missing, type protected at the same level of public or private.

procedure FormDropFiles(var Msg:TWMDropFiles); message WM_DROPFILES;

Declare the method something like this, adapt where neccessary to fit in your application.

procedure TMainWin.FormDropFiles(var Msg: TWMDropFiles);
var
 i,FileCount,FileNameSize:integer;
 FileName:string;
begin
 FileCount:=DragQueryFile(Msg.Drop,-1,nil,0);
 for i:=0 to FileCount-1 do
  begin
   FileNameSize:=DragQueryFile(Msg.Drop,i,nil,0)+1;
   SetLength(FileName,FileNameSize);
   DragQueryFile(Msg.Drop,i,@FileName[1],FileNameSize);
   //skip closing #0 char
   SetLength(FileName,FileNameSize-1);

   //do something with FileName here...

  end;
 DragFinish(Msg.Drop);
end;

 

If you only need one file, you can skip the for loop and just use DragQueryFile(Msg.Drop,0,,);

twitter reddit linkedin facebook