2004. július 23., péntek

Let them drag and drop files on your program


Problem/Question/Abstract:

Let them drag and drop files on your program

Answer:

If you want to let your users drag and drop files on your program from the File Manager and Windows Explorer, simply add the code inside //>>> and //<<< to your program as in the following example:

unit dropfile;

interface

uses
  Windows, Messages, SysUtils, Classes,
  Graphics, Controls, Forms, Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }

    //>>>

    // declare our DROPFILES message handler
    procedure AcceptFiles(var msg: TMessage);
      message WM_DROPFILES;
    //<<<
  end;

var
  Form1: TForm1;

implementation

uses
  //>>>
  //
  // this unit contains certain
  // functions that we'll be using
  //
  ShellAPI;
//<<<

{$R *.DFM}

//>>>

procedure TForm1.AcceptFiles(var msg: TMessage);
const
  cnMaxFileNameLen = 255;
var
  i,
    nCount: integer;
  acFileName: array[0..cnMaxFileNameLen] of char;
begin
  // find out how many files we're accepting
  nCount := DragQueryFile(msg.WParam,
    $FFFFFFFF,
    acFileName,
    cnMaxFileNameLen);

  // query Windows one at a time for the file name
  for i := 0 to nCount - 1 do
  begin
    DragQueryFile(msg.WParam, i,
      acFileName, cnMaxFileNameLen);

    // do your thing with the acFileName
    MessageBox(Handle, acFileName, '', MB_OK);
  end;

  // let Windows know that you're done
  DragFinish(msg.WParam);
end;
//<<<

procedure TForm1.FormCreate(Sender: TObject);
begin
  //>>>
  //
  // tell Windows that you're
  // accepting drag and drop files
  //
  DragAcceptFiles(Handle, True);
  //<<<
end;

end.

Now you can drag and drop files on the form that you registered as a recipient of dropped files by calling the "DragAcceptFiles()" function as in the above example.

Nincsenek megjegyzések:

Megjegyzés küldése