windows 如何判断当前窗口是否为活动窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/510845/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:55:53  来源:igfitidea点击:

How to determine if the current window is the active window?

c++windowswinapimfc

提问by Mark Ingram

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?

如何判断我的窗口是否是当前活动窗口?我目前的猜测是执行 GetForegroundWindow 并将 HWND 与我的窗口的 HWND 进行比较。还有比这更好的方法吗?

I'm using Win32 API / MFC.

我正在使用 Win32 API/MFC。

回答by Bob Moore

Yes, that's the only way that I'm aware of.

是的,这是我所知道的唯一方法。

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).

但是您必须处理 GFW 可以返回 NULL 的事实。通常,当另一个桌面(例如屏幕保护程序桌面)处于活动状态时会发生这种情况。请注意,使用保护程序密码会影响是否使用不同的桌面(这取决于 Windows 版本,我不记得不同版本如何工作的详细信息)。

Also this code won't work properly in debug mode under Visual Studio, because you will get VS's window handle.

此外,此代码在 Visual Studio 下的调试模式下无法正常工作,因为您将获得 VS 的窗口句柄。

Other than that everything's peachy :-)

除此之外,一切都很美好:-)

回答by lakshmanaraj

Yes you are correct unless otherwise you want to check activewindow of every thread.

是的,您是对的,除非您想检查每个线程的 activewindow。

回答by Stefan

I assume that you mean the window which has the input focus when you say "active window"?

当您说“活动窗口”时,我假设您的意思是具有输入焦点的窗口?

In that case, forget the GetForegroundWindow() API. That will return the topmost window - not always the window which has the input focus.

在这种情况下,忘记 GetForegroundWindow() API。这将返回最顶层的窗口 - 并不总是具有输入焦点的窗口。

Use GetFocus()instead.

请改用GetFocus()

回答by towry

You can try to use WM_ACTIVATEAPP message.
First define a bool variable bool wActive = false, in the WndProc procedure, here is the next piece of code:

您可以尝试使用 WM_ACTIVATEAPP 消息。
首先定义一个bool变量bool wActive = false,在WndProc过程中,下面是一段代码:

case WM_ACTIVATEAPP:
     wActive = (bool)wParam;
return 0;

You can go to MSDNto find more information about WM_ACTIVATEAPP

你可以去MSDN找到更多关于WM_ACTIVATEAPP

回答by mkesc

Yea, GetForgroundWindow() is a good way to check, behaves correctly even with a "Always on top" window aka HWND_TOPMOST .

是的, GetForgroundWindow() 是一种很好的检查方法,即使使用“始终在顶部”窗口也称为 HWND_TOPMOST 也能正常运行。

Another way is with GetActiveWindow()

另一种方法是使用 GetActiveWindow()

    HWND temp = GetActiveWindow();
    if (temp == hWnd) // Then your current window has focus

Alternatively the following messages report if the focus has changed

或者,以下消息报告焦点是否已更改

    case WM_KILLFOCUS:
        // windowHasFocus = false
    break;

    case WM_SETFOCUS:
        // windowHasFocus = true;
    break;