How fix window on top

243 views Asked by At

I use SetForegroundWindow to set window of other application on the top. How I can fix this window on the top? It means, once some other application calls SetForegroundWindow, my window will be below my fixed window.

2

There are 2 answers

0
Jonathan Potter On BEST ANSWER

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).

0
Anton K On

There are two ways to make sure your window is always on top:

  1. Constantly check in a loop if your window is still TOPMOST and set it TOPMOST if not:

    if (::GetWindowLong(hwnd, GWL_EXSTYLE) & WS_EX_TOPMOST) { // restore topmost }

  2. Set a system wide hook for Z-order change via SetWindowsHookEx(WH_CBT or WH_CALLWNDPROC) , and then take appropriate action when your window gets under another.