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.
hasFocus() The hasFocus() method of the Document interface returns a boolean value indicating whether the document or any element inside the document has focus. This method can be used to determine whether the active element in a document has focus.
Call GetForegroundWindow to get the handle of the focused window, and then call GetWindowThreadProcessId to get the ID of the process that created that window. What you do with that ID is up to you. Save this answer.
Win32 is the 32-bit application programming interface (API) for versions of Windows from 95 onwards. The API consists of functions implemented, as with Win16, in system DLLs. The core DLLs of Win32 are kernel32. dll, user32.
The Win32 API (also called the Windows API) is the native platform for Windows apps. This API is best for desktop apps that require direct access to system features and hardware. The Windows API can be used in all desktop apps, and the same functions are generally supported on 32-bit and 64-bit Windows.
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
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 ;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With