1

I am working with a software developer on a C Program that has a floating preferences window. If I use the Windows taskbar to switch to another program when mine is running, my floating window still resides over the program I switched to. I am not a programmer and I am being told by my programmer that there is no way to prevent this. I am told he is using SetWindowPosition and something called TOPMOST to give this window it's privilege to stay on top. I like it being on top while working in my program but not when I switch to another program.

Is there something I can tell him to do so that this window does not remain topmost when I switch to another program but stays on top when I return to my program?

1
  • I would suggest changing the question title to something like "How do I make a window topmost to only my program's windows and not to other programs's?", if only because the current title is vague and the "to all programs" case seems to be far more common...
    – andlabs
    Nov 26, 2014 at 0:02

2 Answers 2

3

One way is to not make the window top-most (i.e. not call SetWindowPos with the HWND_TOPMOST flag, and not set the WS_EX_TOPMOST window style) at all, but to make the window owned by your main window.

To do that, when the floating window is created (using CreateWindowEx), you specify the handle of the main window as its parent.

Owned windows always appear above their owners, but it will still be floating and will go behind the windows of other applications.

2
  • Just asked him about that and he says the issue is that we have some windows that are already owned by the main window and we need these to have priority over the main window but not priority over the preferences windows. So here is the order: Main Program (has File, Edit Setup windows, etc -> Owned window that floats ->Another preferences window that opens from the Main Program that I want to have priority over the Owned Window but only remain on top of my program (not other programs when I switch to them). Any ideas? And thanks!
    – emailcooke
    Nov 25, 2014 at 22:13
  • @ShawnCooke: Could you make the Preferences window owned by the "Owned window"? Or are these windows not open all the time? Alternatively you could just keep track of whether your application is active or not and add/remove the topmost flag as appropriate. Nov 26, 2014 at 19:25
0

You can use the WM_ACTIVATE message. Windows sends this message when a window is activated or deactivated. If the user switches to another application the current window of your application receives a WM_ACTIVATE message telling it that it is being deactivated.

Here is a little example to set/remove the topmost flag when the user switches to another application (considering the hFloatingWindow is the window handle to your floating window):

LRESULT __stdcall YourWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg)
    {

    /* ... */

    case WM_ACTIVATE:
        DWORD pid = 0;

        GetWindowThreadProcessId((HWND)lParam, &pid);
        if (pid != GetCurrentProcessId())   /* switch to another task? */
        {
            if ((wParam == WA_ACTIVE) || (wParam == WA_CLICKACTIVE))
            {
                if (SetWindowPos(hFloatingWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE) == FALSE)
                {
                    /* handle error */
                }
            }
            else
            {
                if (SetWindowPos(hFloatingWindow, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE) == FALSE)
                {
                    /* handle error */
                }
            }
        }
        return (0);  /* message processed */

    /* ... */
}

Note that you have to add the code of WM_ACTIVATE to all window procedures of your application. This is neccessary since the user can switch to another task from any of your windows being active. And if the currently active window does not handle the WM_ACTIVATE message as shown above the topmost flag will not be removed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.