如何在不激活其他应用程序窗口的情况下将其置于前面?
我想将一个窗口带到前面(来自其他应用程序)。目前我正在使用:
::SetWindowPos(hwnd, GetForegroundWindow(), 0, 0, 0, 0, SWP_ASYNCWINDOWPOS | SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE);
它工作正常,但在某些(我不知道的)情况下,它使窗口始终位于顶部。根据 MSDN,我应该使用 HWND_NOTOPMOST
代替 GetForegroundWindow()
,但它不起作用 - 窗口保留在其他窗口下方(并不总是在顶部)。
如何在不激活窗口的情况下将其置于前面?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以将其他应用程序的窗口临时设置为“最顶层”,以将其置于最前面而不激活它,首先在
SetWindowPos
调用中将HWND_TOPMOST
指定为“hWndInsertAfter”,然后通过在第二次调用中指定HWND_NOTOPMOST
(两次调用都在“uFlags”中使用SWP_NOACTIVATE
)。如果由于操作而存在删除已经位于最顶层的窗口的最顶层样式的风险,则可以通过调用预先测试
。WS_EX_TOPMOST
ex-style GetWindowLong[Ptr]如果存在另一个应用程序的窗口需要位于前面的特定窗口(而不是位于所有窗口的前面),则可以再次临时将该窗口的所有者设置为它需要位于前面的窗口。
GetWindowLong[Ptr]
和GWL_HWNDPARENT
可用于存储窗口的原始所有者,然后调用SetWindowLong[Ptr]
来设置临时所有者,然后使用HWND_TOP
调用SetWindowPos
,然后再次使用SetWindowLong[Ptr]
恢复原始所有者。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 aSetWindowPos
call and then by specifyingHWND_NOTOPMOST
in a second call (both calls withSWP_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, theWS_EX_TOPMOST
ex-style can be tested beforehand with a call toGetWindowLong[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]
withGWL_HWNDPARENT
can be used to store the window's original owner, then a call toSetWindowLong[Ptr]
to set the temporary owner, followed by a call toSetWindowPos
withHWND_TOP
, and then restoring the original owner with againSetWindowLong[Ptr]
.