How to bring another application window to the forefront without activating it?

I want to bring a window to the foreground (from another application). I am currently using:

::SetWindowPos(hwnd, GetForegroundWindow(), 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE); 

It works fine, but in some cases (unknown to me) it makes the window always on top. According to MSDN, I should use HWND_NOTOPMOST instead of GetForegroundWindow() , but it does not work - the window remains under other (not always top) windows.

How can I bring a window to the fore without activating it?

+7
source share
1 answer

Another application window may be temporarily “topmost” to bring it to the forefront without activating it, first specifying HWND_TOPMOST as “hWndInsertAfter” in the SetWindowPos call and then specifying HWND_NOTOPMOST in the second call (both calls from SWP_NOACTIVATE to 'uFlags' ) If there is a risk of removing the topmost window style, which is already the most recent due to the operation, ex-style WS_EX_TOPMOST can be pre-checked by calling GetWindowLong[Ptr] .

If there is a special window in which another application window should be located (as opposed to being in front of all windows), this window owner can be set again in the window that should be in front. GetWindowLong[Ptr] can use GetWindowLong[Ptr] with GWL_HWNDPARENT to store the original owner of the window, and then to call the temporary owner of SetWindowLong[Ptr] , followed by calling SetWindowPos with HWND_TOP , and then restoring the original owner using SetWindowLong[Ptr] again.

+11
source

All Articles