type
TForm1 = class(TForm)
Label1: TLabel;
ListBox1: TListBox;
private
{ Private declarations }
public
{ Public declarations }
protected
procedure CreateParams(var Params:TCreateParams);override;
procedure WMDropFiles(var Message:TWMDropFiles);message WM_DropFiles;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
//重载CreateParams方法
procedure TForm1.CreateParams(var Params:TCreateParams);
begin
inherited CreateParams(Params);
Params.ExStyle:=Params.ExStyle or WS_EX_ACCEPTFILES;
end;
//响应WM_DROPFILES消息
procedure TForm1.WMDropFiles(var Message:TWMDropFiles);
var
FNumFiles,i,BufSize:Integer;
FFilePath:array of char;
FFileName:string;
begin
//取得文件个数
FNumFiles:=DragQueryFile(Message.Drop,$FFFFFFFF,nil,0);
//处理各个文件
for i:=0 to FNumFiles-1 do
begin
//获得存放文件名buffer的大小
BufSize:=DragQueryFile(Message.Drop,i,nil,0);
//获得文件名。文件名是以0结尾的字符串,包含路径。
SetLength(FFilePath,BufSize+1);
DragQueryFile(Message.Drop,i,PChar(FFilePath),BufSize+1);
//将文件名加入到listbox1中
listbox1.Items.Add(PChar(FFilePath));
end;
DragFinish(Message.drop);
end;