Fandom

Delphi Programming

Drag and Drop from Explorer

2,918pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Implementing drag&drop of files from the Windows Explorer is done in three steps:

  1. . Add a message handler for the WM_DROPFILES message
  2. . Implement it
  3. . Register your drop handler with Windows

Adding a message handler for WM_DROPFILES Edit

type
  TMyform = class(TForm)
  ...
  protected
    procedure WMDropFiles(var Message: TWMDropFiles); message WM_DROPFILES;
  ...
  end;

Implement the message handler Edit

procedure TMyform.WMDropFiles(var Message: TWMDropFiles);
var
  numfiles: integer;
  buf: array[0..MAX_PATH] of char;
begin
  numfiles := DragQueryFile(Message.Drop,$FFFFFFFF,nil,0);
  if numfiles>0 then begin
    Application.BringToFront;
    // only open 1 file at a time
    // - you could open more than 1 file
    // at a time by looping thru numfiles
    DragQueryFile(Message.Drop,0,buf,MAX_PATH);

    { call some method which opens the file
      - buf gets automatically
      typecast to a string if necessary }
    DoOpenFile(buf);

    DragFinish(Message.Drop); // clean up
  end;
end;

Register the drop handler with Windows Edit

Register your drop handler in your form's OnCreate or OnShow event.

// accept dropped files (handle is the form's handle)
DragAcceptFiles(Handle, true);

(from an email by Chris Morgan in b.p.d.nativeapi.win32)

See alsoEdit


Code Snippets
DatabasesFiles and I/OForms/WindowsGraphicsNetworkingMath and AlgorithmsMiscellaneousMultimediaSystemVCL

Also on Fandom

Random Wiki