type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure WMDropFiles(var Message: TMessage); message WM_DropFiles;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
ShellApi;
procedure TForm1.WMDropFiles(var Message: TMessage);
var
Index, iFileCount: Integer;
sFileName: array[0..MAX_PATH] of Char;
begin
Index := -1;
iFileCount := DragQueryFile(Message.WParam, Index, nil, 0);
for Index := 0 to iFileCount - 1 do
begin
DragQueryFile(Message.WParam, Index, sFileName, MAX_PATH);
ListBox1.Items.Add(sFileName);
end;
DragFinish(Message.WParam);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(Handle, True);
end;
procedure Register;
begin
RegisterComponents('Sunisoft', [TDropFileListBox]); //注册组件到组件板上
end;
constructor TDropFileListBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FEnabled:=true; //类被构造时,使DropEnabeld的默认值为True
end;
destructor TDropFileListBox.Destroy;
begin
inherited Destroy;
end;
//改变属性DropEnabled的调用过程
procedure TDropFileListBox.FDropEnabled(Enabled:Boolean);
begin
FEnabled:=Enabled;
DragAcceptFiles(Self.Handle,Enabled);//设置组件窗口是否接受文件拖放
end;
//接受WM_DropFiles消息的过程
procedure TDropFileListBox.DropFiles(var Mes:TMessage);
var FN:TStringList;
FileName:array [1..256] of char;
sFN:String;
i,Count,p:integer;
begin
FN:=TStringList.Create;
Count:=DragQueryFile(Mes.WParam,$FFFFFFFF,@FileName,256);//得到拖放文件的个数
For i:=0 to Count-1 do
begin
DragQueryFile(mes.WParam,i,@FileName,256);//查询文件名称
sFN:=FileName;
p:=pos(chr(0),sFN);//去掉文件名末尾的ASCII码为0的字符
sFN:=copy(sFN,1,p-1);
FN.Add(sFN);
end;
DragFinish(mes.WParam); //释放所使用的资源
if Assigned(FDropFile) then
FDropFile(self, FN); //调用事件,并返回文件名列表参数
FN.Free;
end;