【发布时间】:2013-01-09 18:49:00
【问题描述】:
我尝试接受从文件资源管理器拖放到表单的文件,但它不起作用。我的 WM_DROPFILES 处理程序永远不会被调用。如果这有什么不同,我正在运行 Windows 8。
这是我所做的一个简单示例(我只是在表单上有一个 TMemo):
type
TForm1 = class(TForm)
Memo1: TMemo;
private
{ Private declarations }
procedure WMDROPFILES(var msg : TWMDropFiles) ; message WM_DROPFILES;
procedure CreateWnd; override;
procedure DestroyWnd; override;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
procedure TForm1.CreateWnd;
begin
inherited;
DragAcceptFiles(Handle, True);
end;
procedure TForm1.DestroyWnd;
begin
inherited;
DragAcceptFiles(Handle, false);
end;
procedure TForm1.WMDROPFILES(var msg: TWMDropFiles);
var
i, fileCount: integer;
fileName: array[0..MAX_PATH] of char;
begin
fileCount:=DragQueryFile(msg.Drop, $FFFFFFFF, fileName, MAX_PATH);
for i := 0 to fileCount - 1 do
begin
DragQueryFile(msg.Drop, i, fileName, MAX_PATH);
Memo1.Lines.Add(fileName);
end;
DragFinish(msg.Drop);
end;
【问题讨论】:
-
我已经测试了你的代码并且它可以工作。 Windows 7,德尔福 XE。我只在
DestroyWnd中换了行,但这没关系 - 它按预期工作。 -
@Serg:
DestroyWnd()中的顺序很重要。DragAcceptFiles()需要在inherited之前调用,因为inherited实际上会破坏HWND。Handle被销毁后下次读取时,会通过CreateWnd()创建一个新的HWND。如果顺序是倒序的,最终结果将是最后调用DragAcceptFiles(FALSE),禁用拖放。
标签: delphi delphi-2010