How can I tell if a window has focus? (Win32 API)
Asked Answered
L

5

34

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.

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.

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

Lowrance answered 21/1, 2009 at 18:2 Comment(2)
Do you want to be asking, "does this window have the focus", or "does any of this application's windows have the focus"?Isidor
I noted in a comment below that this application will only have one Window.Lowrance
H
34

GetActiveWindow will return the top-level window that is associated with the input focus. GetFocus will return the handle of the window that has the input focus.

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

Holiday answered 21/1, 2009 at 18:8 Comment(4)
That utility is awesome!Avis
The link is no longer available at its shown location, but is available as archived: web.archive.org/web/200308Astridastride
Or, for a page which is a bit more modern than @Sabuncu's, try this: msdn.microsoft.com/en-us/library/windows/desktop/…Vedic
learn.microsoft.com/en-us/windows/win32/api/winuser/…Chondrule
E
18

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_SETFOCUS and WM_KILLFOCUS events, or 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 ;
Embrey answered 4/9, 2011 at 19:11 Comment(0)
W
11

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.

Wallenstein answered 21/1, 2009 at 18:5 Comment(1)
The application will only ever have one Window, no sub-Windows or child windows.Lowrance
S
4

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.

Sandhurst answered 8/10, 2013 at 17:53 Comment(1)
#7163334Decimalize
S
0

For multiple modeless children:

Within the child you could save the focus, 13/08/2019 Visual Studio 2017.

You can save the focus so the parent knows which modeless child was clicked on.

In the child's 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;
Spume answered 13/8, 2019 at 6:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.