下面的代码实现可以不同程序之间的文件拖放,但为什么只能在ListBox1中实现程序的拖放,而在Button1和Memo1中去不能实现呢?我在测试时,将文件拖放到Button1和Memo1上时,根本就没有触发DragDropFiles,请问该怎样写代码才能实现将文件拖放到Button1和Memo1上时得到拖放过来的文件的文件名?unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI, OleCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure DragDropFiles(var Msg:TMessage);
message WM_DROPFILES;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DragDropFiles(var Msg: TMessage);
var
char2:pchar;
i,sum:integer;
begin
char2:=StrAlloc(128);
sum:=DragQueryFile(Msg.WParam,$FFFFFFFF,nil,0);
for i:=0 to sum-1 do
begin
DragQueryFile(Msg.WParam,i,char2,128);
ListBox1.Items.Add(char2);
end;
DragFinish(msg.WParam);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(ListBox1.Handle,True);
DragAcceptFiles(button1.Handle,True);
DragAcceptFiles(Memo1.Handle,True);
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ShellAPI, OleCtrls;type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure DragDropFiles(var Msg:TMessage);
message WM_DROPFILES;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.DragDropFiles(var Msg: TMessage);
var
char2:pchar;
i,sum:integer;
begin
char2:=StrAlloc(128);
sum:=DragQueryFile(Msg.WParam,$FFFFFFFF,nil,0);
for i:=0 to sum-1 do
begin
DragQueryFile(Msg.WParam,i,char2,128);
ListBox1.Items.Add(char2);
end;
DragFinish(msg.WParam);
end;procedure TForm1.FormCreate(Sender: TObject);
begin
DragAcceptFiles(ListBox1.Handle,True);
DragAcceptFiles(button1.Handle,True);
DragAcceptFiles(Memo1.Handle,True);
end;end.
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货