Jader Dias
Jader Dias

Reputation: 90603

How to get the foreground window on Windows?

Windows API has a method called GetForegroundWindow. But it considers the Desktop as a foreground window when you click on it. We all know that when it happens the previous foreground window isn't superposed by it. How to get the real foreground window handle?

Upvotes: 0

Views: 1836

Answers (2)

Peter Ruderman
Peter Ruderman

Reputation: 12485

I think you're overengineering your solution. If your application window is not the foreground window when you receive an update, then flash the window. The point of flashing is to capture the user's attention, and you have no way to determine if you have their attention programmatically.

It seems like what you really want to know is: "Is the portion of my window that changes currently visible to the user?" That's quite a complex question to answer, and even if you answer it correctly, you have no way to know if the user will notice the change.

Upvotes: 3

Anders
Anders

Reputation: 101774

Both the "desktop" (Explorers desktop listview on top of the real desktop window) and the taskbar are real windows where the user might be "working" (Tab'ing around, using menus etc)

If you want to find the "real" foreground window that bad you have find it yourself, your best bet is going for the window at the top of the z-order, maybe something like:

... enumfunc(hwnd,...) 
{
    if (GetClassName(hwnd)!= "Shell_TrayWnd")
    {
        if (IsWindowEnabled(hwnd) && IsWindowVisible(hwnd) && GetWindow(hwnd,GW_OWNER)==NULL)
        {
            DoSomethingWithRealForegroundWindow(hwnd)
            return FALSE
        }
    }
}

EnumWindows(enumfunc,0)

TaskSwitchXP is a open source alt-tab replacement, it probably has a better algorithm that you can use...

Upvotes: 1

Related Questions