gpt4 book ai didi

delphi - Delphi 7 中 Application.OnActivate 的意外行为

转载 作者:行者123 更新时间:2023-12-03 14:54:38 40 4
gpt4 key购买 nike

我编写了一个 D7 应用程序来测试 Application.OnActivate 的行为。
这是相关部分:

procedure TMainForm.FormCreate (Sender: TObject);
begin
Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.FormCreate - Begin');

Application.OnActivate := AppActivate;

Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.FormCreate - End');
end;


procedure TMainForm.AppActivate (Sender: TObject);
begin
Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.AppActivate - Begin');

ShowWidthsHeights (Sender);

Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.AppActivate - End');
end;


procedure TMainForm.ShowWidthsHeights (Sender: TObject);

begin
Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.ShowWidthsHeights - Begin');

Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.ShowWidthsHeights - End');
end;

启动应用程序后,Memo1的内容为:
1 - MainForm.FormCreate - 开始
2 - MainForm.FormCreate - 结束
3 - MainForm.AppActivate - 开始
4 - MainForm.ShowWidthsHeights - 开始
5 - MainForm.ShowWidthsHeights - 结束
6 - MainForm.AppActivate - 结束

这是正确的。

但是如果我单击任务栏中的快捷方式,这 4 行就会添加到 Memo1 中:
7 - MainForm.AppActivate - 开始
8 - MainForm.ShowWidthsHeights - 开始
9 - MainForm.ShowWidthsHeights - 结束
10 - MainForm.AppActivate - 结束

为什么D7要这么做?
我的应用程序还没有被激活,相反,它已经被停用了!

<小时/>

为了测试 Application.OnDeactivate 事件是否也被触发,我添加了此事件处理程序:

procedure TMainForm.AppDeactivate(Sender: TObject);
begin
Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.AppDeactivate - Begin');

Memo1.Lines.Add (IntToStr (Memo1.Lines.Count + 1) + ' - MainForm.AppDeactivate - End');
end;

并将此语句添加到 TMainForm.FormCreate 中:

Application.OnDeactivate := AppDeactivate;  

启动应用程序后,Memo1 包含与原始情况相同的 6 行,但单击任务栏中的快捷方式会导致 Memo1 中多出 8 行:

7 - MainForm.AppDeactivate - Begin  
8 - MainForm.AppDeactivate - End
9 - MainForm.AppActivate - Begin
10 - MainForm.ShowWidthsHeights - Begin
11 - MainForm.ShowWidthsHeights - End
12 - MainForm.AppActivate - End
13 - MainForm.AppDeactivate - Begin
14 - MainForm.AppDeactivate - End

所以,我的应用程序被停用,然后激活,然后再次停用!
这真是令人困惑!

最佳答案

正如David的评论已经指出的,它反射(reflect)了WM_ACTIVATE消息的接收。 http://msdn.microsoft.com/en-us/library/windows/desktop/ms632614(v=vs.85).aspx给出答案:

Sent when a window belonging to a different application than the active window is about to be activated. The message is sent to the application whose window is being activated and to the application whose window is being deactivated.

以及消息的参数进一步解释:

wParam:

Indicates whether the window is being activated or deactivated. This parameter is TRUE if the window is being activated; it is FALSE if the window is being deactivated.

关于delphi - Delphi 7 中 Application.OnActivate 的意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11223297/

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