I want to bring to front a window(from other application). Currently I'm using:
::SetWindowPos(hwnd, GetForegroundWindow(), 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
It works fine, but in some (unknown to me) cases, it makes the window always on top. According to MSDN, I should use HWND_NOTOPMOST
in the place of GetForegroundWindow()
but it doesn't work—the window stays under other (not always on top) windows.
How can I bring a window to the front without activating it?
The other application's window can be made temporarily 'top-most' to bring it to front without activating it, first by specifying HWND_TOPMOST
as 'hWndInsertAfter' in a SetWindowPos
call and then by specifying HWND_NOTOPMOST
in a second call (both calls with SWP_NOACTIVATE
in 'uFlags'). If there's a risk of removing the top-most style of a window which is already top-most as a consequence of the operation, the WS_EX_TOPMOST
ex-style can be tested beforehand with a call to GetWindowLong[Ptr]
.
If there's a particular window that the other application's window need to be in front (as opposed to being in front of all windows), that window's owner can be set, again temporarily, to the window it needs to be in front. GetWindowLong[Ptr]
with GWL_HWNDPARENT
can be used to store the window's original owner, then a call to SetWindowLong[Ptr]
to set the temporary owner, followed by a call to SetWindowPos
with HWND_TOP
, and then restoring the original owner with again SetWindowLong[Ptr]
.