How to Keep My Topmost Window on Top

How to keep my topmost window on top?

TopMost, is always a tricky thing. There is no way to override another window that specifies itself as TopMost.

Raymond Chen has a good article on this.

Also a duplicate of this.

How to set window top of any topmost window?

It's not very elegant, but I've solved this before by constantly setting the window to HWND_TOPMOST on a one-second timer.

Placing a window after TOPMOST window

HWND_TOP will put your window at the top of the z-order behind any topmost windows.

Set a window to be topmost

Use CreateWindowEx with (extended) window style WS_EX_TOPMOST.

Disclaimer: it's about 15 years or so since I touched that stuff.

Stop being topmost window

The only automatic method to make a window permanently on top of another one whether the target window is top-most or not is an owner/owned relationship. You could try using SetParent to create this relationship but note that Raymond Chen does say it's not recommended.

Assuming you're tracking window activations somehow, I think your SetWindowPos idea (the first one) is the way to do it, with the following modification:

  • When the target window is active, set your window to HWND_TOPMOST
  • When the target loses activation, insert your window after the target window's immediate predecessor in the z-order (i.e. effectively still on top of the target window, but not top-most)

Something like this psuedo-code:

if (foregroundwindow == targetwindow)
SetWindowPos(my_window, HWND_TOPMOST, ...);
else
{
HWND hwndPred = GetWindow(targetwindow, GW_HWNDPREV);
if (!hwndPred)
{
// no predecessor so my_window will still be on top, just not top-most any more
if (GetWindowLong(targetwindow, GWL_EXSTYLE) & WS_EX_TOPMOST)
hwndPred = HWND_NOTOPMOST;
}
SetWindowPos(my_window, hwndPred, ...);
}

How to keep window always on top

It should be possible by setting the Focus on window, from OnFocusLost event handler.

How to keep a window on top of all other windows in my application only?

If you pass your main form into the Show method of the status form, it will stay on top of the main form, but not on top of other applications. So, in the main form you can have code like so:

StatusForm statusForm = new StatusForm();
statusForm.Show(this);

However, this will only point out one single window of your application as the owner.

How fix window on top

You can make your window top-most by setting the WS_EX_TOPMOST extended style when you create the window, or afterwards by calling SetWindowPos with HWND_TOPMOST as the second parameter.

Note however there's no way to make your window stay on top of other top-most windows (i.e. there's no "on top of absolutely everything" flag).



Related Topics



Leave a reply



Submit