Windows / C ++:检测焦点在窗口之间的变化(全局) [英] Windows/C++: detect when focus has changed between windows (globally)

查看:140
本文介绍了Windows / C ++:检测焦点在窗口之间的变化(全局)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种方法来检测焦点是否改变到另一个窗口(而不必每X秒轮询一次)。

I'm trying to find a way to detect when focus is changed to another window (without having to poll every X ms).

我已经想到了使用WH_SHELL和HSHELL_ACTIVATESHELLWINDOW来检测焦点在应用程序之间切换的方式。

I've already figured out a way to detect when focus is switched between applications using WH_SHELL and HSHELL_ACTIVATESHELLWINDOW.

问题是我想检测焦点在同一个应用程序中的对话框/ 。

The problem is I want to detect when focus is switched between dialog/windows within the same app.

ie。在记事本中,我可以确定应用程序何时切换到记事本,但我无法检测到打开或保存对话框何时出现,因为焦点仍在同一个应用程序中。

ie. In Notepad, I can determine when the app switches to Notepad, but I cannot detect when the "Open" or "Save" dialogs appear because the focus is still within the same application.

推荐答案

您可以使用 SetWindowsHookEx < a>使用WH_CBT挂钩类型。如果你只想检测应用程序中的焦点更改,请将GetCurrentThreadId()作为最后一个参数,否则钩子将用于当前桌面上的所有线程。

You can use SetWindowsHookEx with a WH_CBT hook type. If you just want to detect focus changes within an application, pass GetCurrentThreadId() as the last parameter, otherwise the hook will be for all threads on the current desktop.

请注意,使用Windows钩子可能会对系统性能产生不利影响,因此只应在必要时安装钩子,并且您应在钩子中执行最少量的工作程序。

Note that using windows hooks can have an adverse effect on system performance, so the hook should only be installed when necessary and you should do a minimum amount of work in the hook procedure.

这篇关于Windows / C ++:检测焦点在窗口之间的变化(全局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆