windows 如何判断窗口是否有焦点?(Win32 API)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/466354/
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-09 05:58:03  来源:igfitidea点击:

How can I tell if a Window has focus? (Win32 API)

windowswinapihwnd

提问by Daniel Jennings

Using the Win32 API (in C, but that's inconsequential) how can I tell if a given window (identified by HWND) has focus? I'm hooking an application watching for an event, and when that event occurs I want to check if the application already has focus. If it doesn't, I want to flash the window until they give focus to it.

使用 Win32 API(在 C 中,但这无关紧要)如何判断给定窗口(由 HWND 标识)是否具有焦点?我正在挂钩一个监视事件的应用程序,当该事件发生时,我想检查该应用程序是否已经获得焦点。如果没有,我想闪烁窗口,直到他们把焦点放在它上面。

Alternately, does the FlashWindowEx struct flag FLASHW_TIMERNOFG that flashes until the window has focus just not flash if the window already has focus? I cannot test this now since I am not in my development environment, but I was under the impression that it would flash anyways, which is what I'm trying to avoid.

或者,如果窗口已经获得焦点,则 FlashWindowEx 结构标志 FLASHW_TIMERNOFG 是否闪烁直到窗口获得焦点才不会闪烁?我现在无法测试这个,因为我不在我的开发环境中,但我的印象是它无论如何都会闪烁,这是我试图避免的。

Edit:Also, if it matters, the application uses DirectX in this window.

编辑:此外,如果重要的话,应用程序在此窗口中使用 DirectX。

回答by gkrogers

GetActiveWindowwill return the top-levelwindow that is associated with the input focus. GetFocuswill return the handle of the window that has the input focus.

GetActiveWindow将返回与输入焦点关联的顶级窗口。GetFocus将返回具有输入焦点的窗口句柄。

This article might help:
http://www.microsoft.com/msj/0397/Win32/Win320397.aspx

这篇文章可能会有所帮助:http:
//www.microsoft.com/msj/0397/Win32/Win320397.aspx

回答by bobobobo

Besides gkrogers answer using GetActiveWindow, you can also maintain a boolean variable for the window you want to know if it has focus or not by trapping the WM_SETFOCUSand WM_KILLFOCUSevents, or WM_ACTIVATE:

除了 gkrogers 使用 GetActiveWindow 回答之外,您还可以通过捕获WM_SETFOCUSWM_KILLFOCUS事件为您想知道它是否具有焦点的窗口维护一个布尔变量,或者WM_ACTIVATE

WndProc() ..
case WM_SETFOCUS:
  puts( "Got the focus" ) ;
  break ;

case WM_KILLFOCUS:
  puts( "Lost the focus" ) ;
  break;

case WM_ACTIVATE:
  if( LOWORD(wparam) == WA_INACTIVE )
    puts( "I AM NOW INACTIVE." ) ;
  else // WA_ACTIVE or WA_CLICKACTIVE
    puts( "MEGAZORD ACTIVATED kew kew kew (flashy-eyes)" ) ;
  break ;

回答by Jason Cohen

Do you really mean "focus" or do you mean "active?"

你的意思是“专注”还是“主动”?

One window has the focus -- the one that's first in line to get keyboard events. The outer window (that the user can drag around the screen) is "active" if one of its subwindows has the focus, but it might or might not have focus itself.

一个窗口有焦点——第一个获得键盘事件的窗口。如果其子窗口之一具有焦点,则外部窗口(用户可以在屏幕上拖动)是“活动的”,但它本身可能有也可能没有焦点。

回答by Jason Cohen

Use GetForegroundWindow function to get the Hwnd that you are focusing right now. Then you just need to compare it to the window of your application to check whether it contains focus or not.

使用 GetForegroundWindow 函数获取您现在正在关注的 Hwnd。然后您只需将它与您的应用程序的窗口进行比较以检查它是否包含焦点。

回答by user11910606

For multiple modeless children: Within the Child you could save the focus, 13/08/19 VS2017. You can save the focus so the parent knows which modeless child was clicked on.

对于多个无模式孩子:在 Child 中,您可以保存焦点,13/08/19 VS2017。您可以保存焦点,以便父级知道点击了哪个无模式子级。

In the childs callback handler:

在孩子的回调处理程序中:

case WM_CHILDACTIVATE: //only gets called when the child border is click on.
    //CurrentFocus = hDlg; //example : can save the focus globally for parent usage.
    //Beep(2000, 250); // so you can test
break;

case WM_GETMINMAXINFO: //gets called when child window is being moved or sized.
        //Beep(2000, 250);
break;

case WM_LBUTTONDOWN:  //Only called when cursor is inside the child client area
    //CurrentFocus = hDlg; // following the focus.
    //Beep(2000, 250);
break;