Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a window be always on top of just one other window?

Tags:

windows

winapi

In Windows, is it possible to set window A such that it is always on top of window B, yet allow other windows to work as normal and appear over the top of both, when active.

In other words, I want a parent-child relationship between two windows. Can this be done without making window A a child of window B, MDI-style? Window B isn't mine (Internet Explorer), and screws my dialog A's graphics up when I try to achieve this with SetParent.

I thought I'd cracked it with this idea from an MSDN forum post, but alas windows A is still always on top of everything, not just window B.

// Place window A on top
SetWindowPos(hWndWindow, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE);
// Place window B underneath it
SetWindowPos(hWndParent, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE|SWP_NOMOVE);

Is it possible?

like image 449
Mat Avatar asked May 01 '09 18:05

Mat


People also ask

How do I pin a window always on top?

In order to pin a window, right-click on the icon in your tray again and enter Pin Mode. Your cursor will change to a pin – click on the title bar of the window you want to always keep on top, and a pin will appear on that bar. It'll be the color you set in the options menu earlier.

How do you put a window on top of each other?

You can now press Ctrl+Space to set any currently active window to be always on top. Press Ctrl+Space again set the window to no longer be always on top. And if you don't like the Ctrl+Space combination, you can change the ^SPACE part of the script to set a new keyboard shortcut.

How do I keep a window always on top Windows 11?

There are also keyboard shortcuts available here. Press “Ctrl +F11” to pin a window on top and press “Ctrl + F12” to disable it. You can customize the shortcuts by right-clicking on the app and navigating to “Options -> Hotkeys”.

How do I create a floating window in Windows 10?

To make a floating window or a docked window active, click its title bar. To make a docked window in a tabbed collection active, click its tab. You can also make a window active by using the WinDbg menu or toolbar.


2 Answers

Wouldn't creating an ownership relationship do the trick?

SetWindowLong(hwndChild, GWL_HWNDPARENT, hwndOwner)

The windows can be in different processes and you can call this from any process. This will ensure that the child window is always above the owner window. This is different than SetParent which actually creates a Parent / Child relationship. Read through this article (its from 1993 but still mostly correct) to see the distinction between ownership and parenting.

like image 149
Maurice Flanagan Avatar answered Sep 18 '22 15:09

Maurice Flanagan


When your window's Z-order (or size or position) is changing, it should receive a WM_WINDOWPOSCHANGING message. If you process that message, you have an opportunity to modify the final Z-order (or size or position) to which the window is moved.

To illustrate, in hWndA's window procedure:

case WM_WINDOWPOSCHANGING:
    DefWindowProc(hWnd, msg, wParam, lParam);
    WINDOWPOS *p = (WINDOWPOS*)lParam;
    p->hwndInsertAfter = hWndB;
    p->flags &= ~SWP_NOZORDER;
    return 0;

should insert hWndA after hWndB in the Z-order any time hWndA's position changes.

like image 42
Matthew Xavier Avatar answered Sep 18 '22 15:09

Matthew Xavier