4th february

Pascal

Delphi

VCL Components

CLX Components

Glyphs, Icons, Cursors, Videos

Logos

Sample Projects

Tools

Articles, Code Examples, Tips

Links

Code Examples

How to use drag'n'drop feature for accept files dropped from Explorer?

Printable Version

type
  TMainForm = class(TForm)
  ...
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  ...
  private
    procedure WMDropFiles(var Msg: TWMDROPFILES); message WM_DROPFILES;
  ...
  end;

 ...
procedure TMainForm.FormCreate(Sender: TObject);
begin
{ files acceptor registration }
  DragAcceptFiles(MainForm.WindowHandle, True);
end;

procedure TMainForm.FormDestroy(Sender: TObject);
begin
{ files acceptor unregistration }
  DragAcceptFiles(MainForm.WindowHandle, False);
end;

procedure TMainForm.WMDropFiles(var Msg: TWMDropFiles);
var
  CFileName: array [0..MAX_PATH] of Char;
  WhichFiles: TStringList;
  I, FilesCount: Integer;
begin
  WhichFiles := TStringList.Create;
  try
    FilesCount := DragQueryFile(Msg.Drop, $FFFFFFFF, CFileName, MAX_PATH);
    for I := 0 to Pred(FilesCount) do
    begin
      if DragQueryFile(Msg.Drop, I, CFileName, MAX_PATH) > 0 then
      begin
        WhichFiles.Add(CFileName);
      end;
    end;

    if (WhichFiles.Count > 0) then
    begin
      Screen.Cursor := crHourGlass;
      for I := 0 to Pred(WhichFiles.Count) do
      begin
        { your code for processing list of filenames }
      end;
      Screen.Cursor := crDefault;
    end;
  finally
    DragFinish(Msg.Drop);
    WhichFiles.Free;
  end;
end;