windows 如何在不激活它的情况下将其他应用程序窗口置于前面?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5257977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:23:46  来源:igfitidea点击:

How to bring other app window to front without activating it?

c++windowswinapiz-order

提问by SathOkh

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_NOTOPMOSTin the place of GetForegroundWindow()but it doesn't work—the window stays under other (not always on top) windows.

它工作正常,但在某些(我不知道)的情况下,它使窗口始终位于顶部。根据 MSDN,我应该使用HWND_NOTOPMOST代替GetForegroundWindow()但它不起作用 - 窗口停留在其他(并非总是在顶部)窗口下方。

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

如何在不激活窗口的情况下将窗口置于前面?

回答by Sertac Akyuz

The other application's window can be made temporarily 'top-most' to bring it to front without activating it, first by specifying HWND_TOPMOSTas 'hWndInsertAfter' in a SetWindowPoscall and then by specifying HWND_NOTOPMOSTin a second call (both calls with SWP_NOACTIVATEin '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_TOPMOSTex-style can be tested beforehand with a call to GetWindowLong[Ptr].

另一个应用程序的窗口可以暂时设置为“最顶层”以将其置于最前面而不激活它,首先通过HWND_TOPMOSTSetWindowPos调用中指定为“hWndInsertAfter” ,然后HWND_NOTOPMOST在第二次调用中指定(两个调用都SWP_NOACTIVATE在“uFlags”中)。如果由于操作而存在删除已经位于最顶部的窗口的最顶部样式的风险,则WS_EX_TOPMOST可以通过调用 预先测试 ex 样式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_HWNDPARENTcan 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 SetWindowPoswith HWND_TOP, and then restoring the original owner with again SetWindowLong[Ptr].

如果有一个特定的窗口需要其他应用程序的窗口在前面(而不是在所有窗口的前面),则该窗口的所有者可以再次临时设置为它需要在前面的窗口。GetWindowLong[Ptr]withGWL_HWNDPARENT可用于存储窗口的原始所有者,然后调用 以SetWindowLong[Ptr]设置临时所有者,然后调用SetWindowPoswith HWND_TOP,然后再次使用 恢复原始所有者SetWindowLong[Ptr]