Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

I'm using Win32 API / MFC.

like image 426
Mark Ingram Avatar asked Feb 04 '09 10:02

Mark Ingram


People also ask

How do I know if Windows is active?

The active window, labeled in red, is the text document window open above the browser window.

What is active window?

An active window is the currently focused window in the current window manager. Different window managers indicate the currently-active window in different ways and allow the user to switch between windows in different ways.


1 Answers

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

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

like image 147
Bob Moore Avatar answered Nov 17 '22 01:11

Bob Moore