Delphi Programming

Drag and Drop from Explorer

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

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

  TMyform = class(TForm)
    procedure WMDropFiles(var Message: TWMDropFiles); message WM_DROPFILES;

Implement the message handler Edit

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

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

    DragFinish(Message.Drop); // clean up

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

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.