DelphiFAQ Home Search:

Drag and Drop from FileManager

 

commentsThis article has not been rated yet. After reading, feel free to leave comments and rate it.

You need to use these 3 functions from the ShellApi:

  1. DragAcceptFiles - registers whether a window accepts dropped files
  2. DragQueryFile - retrieves the filename of a dropped file
  3. 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:

  1. When dropping files, the DroppedFilename is the complete path, not just the filename.ext
  2. It is possible to drag and drop just a directory. So if you are expecting filenames, you have to check for existence yourself.
  3. The filenames come in uppercased.

Comments:

 

 

NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.
 

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: