Delphi Programming
Register
Advertisement

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[]

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

Implement the message handler[]

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[]

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 also[]


Code Snippets
DatabasesFiles and I/OForms/WindowsGraphicsNetworkingMath and AlgorithmsMiscellaneousMultimediaSystemVCL
Advertisement