gpt4 book ai didi

Delphi - 何时调用 DragAcceptFiles

转载 作者:行者123 更新时间:2023-12-03 15:07:17 26 4
gpt4 key购买 nike

我有

procedure TMainForm.FormCreate(Sender: TObject);
begin
DragAcceptFiles (Handle, True ) ;
end ;

但表单不接受拖动的文件 - 不放置光标,不触发 WM_DROPFILES 消息。

我的 FormShow 事件中有以下构造(出于不同的原因 - 有些代码我只想在创建表单后执行一次,并且 FormShow 在初始化期间多次触发):

procedure TMainForm.FormShow(Sender: TObject);

begin
if (not FRunOnce) then // as FormShow can be called twice - if Form.Position assigned to
begin
DragAcceptFiles (Handle, True ) ;
FRunOnce := True ;
end ;
end ;

所示位置的 DragAcceptFiles (Handle, True ) 仍然不起作用。如果我将它移到例程的顶部(因此它执行两次),它确实有效:

procedure TMainForm.FormShow(Sender: TObject);

begin
DragAcceptFiles (Handle, True ) ;
if (not FRunOnce) then // as FormShow can be called twice - if Form.Position assigned to
begin
FRunOnce := True ;
end ;
end ;

我发现的所有示例代码似乎都在 OnCreate 期间调用 DragAccept。我的实验表明这还为时过早,OnShow 的第一次火灾也是如此。我确信我的代码在其他地方有问题,但可能是什么原因导致的呢?

最佳答案

任何 TWinControl 的 HWND 在其生命周期内都可以多次创建和重新创建。调用 DragAcceptFiles() 的最佳位置是在重写的 CreateWnd() 和 DestroyWnd() 方法中,以便您可以在所有(重新)创建期间(重新)注册并在所有销毁期间取消注册,例如:

procedure TMainForm.CreateWnd;
begin
inherited;
DragAcceptFiles(Handle, True);
end;

procedure TMainForm.DestroyWnd;
begin
DragAcceptFiles(Handle, False);
inherited;
end;

关于Delphi - 何时调用 DragAcceptFiles,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3927956/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com