Wikia

Delphi Programming

Drag and Drop from Explorer

Talk0
2,911pages on
this wiki

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

Around Wikia's network

Random Wiki