如何判断当前窗口是否为活动窗口?
如何判断我的窗口是否是当前活动窗口?我目前的猜测是做 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 :-)
相关文章