[ACCEPTED]-How to determine if the current window is the active window?-mfc

Accepted answer
Score: 11

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

But you have to handle the fact that 11 GFW can return NULL. Typically, this happens 10 when another desktop (e.g. the screen saver 9 desktop) is active. Note that use of a saver 8 password can affect whether a different 7 desktop is used (this is windows version-dependent 6 and I can't remember the details of how 5 different versions work).

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

Other than that everything's 1 peachy :-)

Score: 3

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

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

You 2 can go to MSDN to find more information about 1 WM_ACTIVATEAPP

Score: 1

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

Score: 1

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

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

Use 1 GetFocus() instead.

Score: 0

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

Another way is with GetActiveWindow()

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

Alternatively 2 the following messages report if the focus 1 has changed

    case WM_KILLFOCUS:
        // windowHasFocus = false
    break;

    case WM_SETFOCUS:
        // windowHasFocus = true;
    break;

More Related questions