如何让我的窗体接受从资源管理器拖过来的文件 ?

解决方案 »

  1.   

    新建一个工程,试试这个:
    unit UDropFiles;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,ShellAPI;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormPaint(Sender: TObject);
      private
        { Private declarations }
          Dropped : boolean;
          filelist: array [0..MAX_PATH-1] of string;    procedure WmDropFiles(var msg: TWmDropFiles); message WM_DROPFILES;
      public
        { Public declarations }
      end;var
      Form1: TForm1;
    implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      DragAcceptFiles(handle,true);
    end;procedure TForm1.WmDropFiles(var msg: TWmDropFiles);
    var
      i,count: integer;
      filename: array [0..MAX_PATH-1] of Char;
    begin
      Dropped:=true;
      count:= DragQueryFile(msg.Drop ,$FFFFFFFF,nil,0);
      for i:=0 to count-1 do begin
        DragQueryFile(msg.Drop,i,@filename,sizeof(filename));
        self.Canvas.TextOut(0,i*16,filename);
        filelist[i]:=filename; 
      end;
    end;procedure TForm1.FormPaint(Sender: TObject);
    var i: integer;
    begin
      for i:=low(filelist) to high(filelist) do begin
        self.Canvas.TextOut(0,i*16,filelist[i]);
      end;
      
      Dropped:=false;
    end;end.
      

  2.   

    上次看到一个例子,可以参考一下:
    unit UntDrag;interface //用来告诉Windows你的Form可以接受文件拖放 
    {$EXTERNALSYM DragAcceptFiles}procedure DragAcceptFiles(hWnd: Cardinal; fAccept: Boolean); stdcall; 
    //得到拖放文件名和文件个数的API 
    {$EXTERNALSYM DragQueryFile}
    function DragQueryFile(hDrop: Cardinal; iFile: Cardinal; lpszFile: PChar; cch: Integer): Integer; stdcall; 
    //释放Windows分配给拖放操作的内存 
    {$EXTERNALSYM DragFinish} 
    procedure DragFinish(hDrop: Cardinal); stdcall; 
    //得到拖放的文件个数 
    function GetDragFileCount(hDrop: Cardinal): Integer; 
    //得到拖放的文件名,通过FileIndex来指定文件编号,默认为第一个文件 
    function GetDragFileName(hDrop: Cardinal; FileIndex: Integer = 1): string; implementation procedure DragAcceptFiles; external 'Shell32'; 
    function DragQueryFile; external 'Shell32'; 
    procedure DragFinish; external 'Shell32'; function GetDragFileCount(hDrop: Cardinal): Integer; 
    const 
      DragFileCount=High(Cardinal); 
    begin 
      Result:= DragQueryFile(hDrop, DragFileCount, nil, 0); 
    end; function GetDragFileName(hDrop: Cardinal; FileIndex: Integer = 1): string; 
    const 
      Size=255; 
    var 
      Len: Integer; 
      FileName: string; 
    begin 
      SetLength (FileName, Size); 
      Len:= DragQueryFile(hDrop, FileIndex-1, PChar(FileName), Size); 
      SetLength (FileName, Len); 
      Result:= FileName; 
    end; 
    end. 使用时:
    uses UntDrag;
    public
    procedure MyDrag(var Msg: TWMDropFiles); message WM_DropFiles;
    procedure TForm1.FormCreate(Sender:TObject);
    begin
      DragAcceptFiles(Handle, True);
    end;procedure TForm1.MyDrag(var Msg:TWMDropFiles);
    var
      hDrop:Cardinal;
    begin
      hDrop:=Msg.Drop; //这个是拖放句柄
      //...(在这里可以用GetDragFileName和GetDragFileCount)  //最后记得要用这两句话:
      DragFinish(hDrop);
      Msg.Result:= 0;
    end;