检测我的应用程序激活之前哪个窗口处于活动状态

发布于 2024-10-14 10:59:43 字数 404 浏览 7 评论 0原文

在 Delphi XE 中,我尝试使用 GetForegroundWindow 来检测在激活应用程序之前立即处于活动状态的窗口。 “激活”意味着应用程序可能已经从图标状态恢复,或者通过alt+tab切换到,或者通过激活热键等调出。

问题是所有可用的应用程序事件(OnActivate,OnRestore)也随之而来晚了,以便 GetForegroundWindow 返回我自己的表单句柄。主窗体的事件就更没用了。在主窗体中处理 WM_ACTIVATEAPP 也发生得太晚了。

一种方法是在我的应用程序不活动时继续检查计时器上的活动窗口,但这似乎相当浪费。更好的解决方案是子类化 TApplication.WndProc 并在处理 WM_ACTIVATEAPP 之前执行我的操作,但我不认为 TApplication 可以子类化。

有更好的办法吗?

In Delphi XE, I'm trying to use GetForegroundWindow to detect the window that was active immediately before my application was activated. "Activated" means that the app may have been restored from iconic state, or it was switched to via alt+tab, or brought up by activaton hotkey, etc.

The problem is that all the available application events (OnActivate, OnRestore) come too late, so that GetForegroundWindow returns my own form's handle. The main form's events are even less useful. Processing WM_ACTIVATEAPP in the main form happens too late, too.

One way would be to keep checking the active window on timer while my application is not active, but this seems rather wasteful. A much better solution would be to subclass TApplication.WndProc and do my thing before WM_ACTIVATEAPP is processed, but I don't think TApplication can be subclassed.

Is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卖梦商人 2024-10-21 10:59:43

当您的应用程序激活时,它会成为最上面的窗口。 IOW,它跳到 Z 顺序的顶部位置。

这样,先前的最顶层窗口就会降级到“新的最顶层窗口的正下方”位置。 IOW,它在 Z 顺序位置中成为第二。

调用 GetNextWindow 并向其传递表单的句柄和 GW_HWNDNEXT可能会给你你需要的结果。

When your application activates, it becomes the topmost window. IOW, it jumps to the top Z-order position.

With this, previous topmost window is demoted to the "just below the new topmost window" position. IOW, it becomes second in the Z-order position.

Calling GetNextWindow and passing it the handle of your form and GW_HWNDNEXT may give you just the result you need.

や三分注定 2024-10-21 10:59:43

所有窗口都有一个 z 顺序,窗口使用这个数字来确定哪个窗口应该绘制在哪个窗口之上。

当您激活程序时,您的应用程序将被放置在 z 列表的顶部。

当您按 Alt-Tab 时,Windows 会循环浏览该列表。因此,按住 Alt-Tab 可以查看内容的排序方式。

检查一下,看看如何模拟 alt-tab:
http://www.swissdelphicenter.ch/torry/showcode.php?id=第2103章

All windows have a z-order, which is a number that windows uses to determine which window should be painted on top of which.

When you activate a program, your application is put on top of the z-list.

When you press alt-tab, windows cycles through that list. So press alt-tab and hold it, to see how stuff is ordered.

Check this out to see how to simulate alt-tab:
http://www.swissdelphicenter.ch/torry/showcode.php?id=2103

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文