How to determine if the current window is the active window?(如何判断当前窗口是否为活动窗口?)
问题描述
如何判断我的窗口是否是当前活动窗口?我目前的猜测是做 GetForegroundWindow 并将 HWND 与我的窗口进行比较.还有比这更好的方法吗?
How can I tell if my window is the current active window? My current guess is to do GetForegroundWindow and compare the HWND with that of my window. Is there a better method than that?
我正在使用 Win32 API/MFC.
I'm using Win32 API / MFC.
推荐答案
是的,这是我知道的唯一方法.
Yes, that's the only way that I'm aware of.
但是您必须处理 GFW 可以返回 NULL 的事实.通常,当另一个桌面(例如屏幕保护程序桌面)处于活动状态时会发生这种情况.请注意,使用保护程序密码会影响是否使用不同的桌面(这取决于 Windows 版本,我不记得不同版本如何工作的详细信息).
But you have to handle the fact that GFW can return NULL. Typically, this happens when another desktop (e.g. the screen saver desktop) is active. Note that use of a saver password can affect whether a different desktop is used (this is windows version-dependent and I can't remember the details of how different versions work).
此外,这段代码在 Visual Studio 下的调试模式下也无法正常工作,因为您将获得 VS 的窗口句柄.
Also this code won't work properly in debug mode under Visual Studio, because you will get VS's window handle.
除此之外,一切都很美好:-)
Other than that everything's peachy :-)
这篇关于如何判断当前窗口是否为活动窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何判断当前窗口是否为活动窗口?
基础教程推荐
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
