views:

625

answers:

3

I'm using the following code to get a handle of the topmost window:

HWND hwnd;
hwnd = GetForegroundWindow();

The problem with this is that it returns the top most system-wide. Is there any way to get the topmost ONLY from my own application?

I want to get the top most window ONLY of my application. This means, that I need an API to get my own's app top most window and NOT the systemwide top most window as GetForegroundWindow() does. Thanks!

EDIT:

OK, let me be clear here. My problem is that I am able to get the HWND for a window that doesn't belong to MY application. What I want to get is the TOPMOST for ONLY my application. If the HWND belongs to another application then I should not get the information.

+1  A: 

Use the GetTopWindow Function, like this:

HWND hwnd;
hwnd = GetTopWindow(NULL);
Nick D
"The GetTopWindow function examines the Z order of the child windows associated with the specified parent window and retrieves a handle to the child window at the top of the Z order."what if my application's main window is the one that is the topmost?
wonderer
With `NULL` as the parameter you'll get the top most window, even if it's the main window. Do you mean that you don't want the main window?
Nick D
GetTopWindow() gives me a handle to the item in the taskbar, not the handle to the window. when trying to get the window rect i'm getting only the taskbar button measurements
wonderer
I do want the main window, i was asking if the function would only take the child windows. In any case, it's behaving very strangely
wonderer
A: 

I don't know that there is a function that does exactly this, but you could probably write one yourself. If your application windows all have a particular window class, then you can use FindWindow or FindWindowEx.

Alternatively, you could use GetForegroundWindow to get the foreground window from all applications and then use GetWindowLong to check the HINSTANCE. If it's not from your application, then keep enumerating the windows by Z-order (using GetWindow) until you find the first one from your application.

Nick Meyer
thanks for the answer but i need a quick a short solution.
wonderer
+2  A: 

Here is a callback you can use with EnumWindows():

BOOL CALLBACK FindTopmostWnd(HWND hwnd, LPARAM lParam)
{
    HWND* pHwnd = (HWND*)lParam;

    HWND myParent = hwnd;
    do
    {
        myParent = GetParent(myParent);
    }
    while (myParent && (myParent != *pHwnd));

    if (myParent != 0)
    {
        // If the window is a menu_worker window then use it's parent
        TCHAR szClassName[7];
        while (0 != GetClassName(hwnd, szClassName, 7)
            && 0 != _tcsncmp(szClassName, TEXT("Dialog"), 6)
            && 0 != _tcsncmp(szClassName, TEXT("Afx"), 3)
            )
        {
            // find the worker's parent
            hwnd = GetParent(hwnd);
        }

        *pHwnd = hwnd;

        return FALSE;
    }

    return TRUE;
}

As Adam points out, the LPARAM passed to EnumWindows() should be a pointer to an HWND. So you probably want to do something like this:

HWND hTopmostWnd = hWnd;
EnumWindows(FindTopmostWnd, (LPARAM)&hTopmostWnd);
Jared
You should also mention that the LPARAM passed to `EnumWindows()` should be a pointer to an HWND.
Adam Rosenfield
where are you getting the hWnd?
wonderer
Any existing HWND from your app should work.
Jared
I don't have one. my original code returns the hwnd that i need.Ok, this is getting beyond what i needed, I mean i posted 2 lines of code and now i need to start finding windows titles to get an hwnd to pass to a callback function to see if the hwnd is the topmost.While i do appreciate the help, there's gotta be an easy and shorter way to do this.
wonderer
Sorry, I didn't mean to be rude. It's just that it doesn't make sense to have to write such a long piece of code for something so trivial.
wonderer
OK, i wrote the whole code and still it didn't solve my problem.I am still able to get the window rec for a window that is not on my application.
wonderer
Jared
OK, maybe I wasn't clean. That is exactly what i want to avoid. I want to get the top most window ONLY of my application. This means, that I need an API to get my own's app top most window and NOT the systemwide top most window as GetForegroundWindow() does. That is the whole problem. That is what i need to change and that is was I asked.
wonderer
Now there might be a way to use isChild() but I can't get it to work correctly. I think the problem begins with getting my own application HWND which is a pain to get. I tried GetForegroundWindow() and others (and no, i don't want to use Findwindow()).
wonderer
ok, this answer helped me find the solution. thanks!
wonderer