[英]How to remove TOPMOST attribute from a window
I have a Windows C++ app that creates two separate windows 我有一个Windows C ++应用程序,它创建两个单独的窗口
I need to be able to make one window topmost temporarily, and then later remove that attribute so that other windows can then overlay it. 我需要能够暂时将一个窗口放在最顶层,然后删除该属性,以便其他窗口可以覆盖它。
I've tried this code: 我试过这段代码:
void setWindowAlwaysOnTop(const std::string& windowTitle, bool onTop) {
HWND hwnd = FindWindowA(NULL, windowTitle.c_str());
HWND insertAfter;
if (onTop) insertAfter = HWND_TOPMOST; //set the window always-on-top
else insertAfter = HWND_BOTTOM;
SetWindowPos(hwnd, insertAfter, NULL, NULL, NULL, NULL, SWP_NOMOVE | SWP_NOSIZE);
}
But if I have previously called this code with onTop true it doesn't re-allow other windows to overlay the target window after I call it with onTop false. 但是,如果我之前使用onTop调用此代码,则在使用onTop false调用后,它不会重新允许其他窗口覆盖目标窗口。
I've also tried calling the function from the target window itself rather that from a separate window but it still doesn't work. 我也尝试从目标窗口本身调用该函数,而不是从单独的窗口调用该函数,但它仍然不起作用。
As far as I can see at https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowpos it should work? 据我所知,在https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-setwindowpos它可以工作吗?
Is that correct, or is there another way of achieving this? 这是正确的,还是有另一种方法来实现这一目标?
The documentation link that you provide in the question indicates that you should pass HWND_NOTOPMOST
to hWndInsertAfter
. 您在问题中提供的文档链接表明您应该将
HWND_NOTOPMOST
传递给hWndInsertAfter
。 Of this flag it says: 这面旗帜说:
Places the window above all non-topmost windows (that is, behind all topmost windows).
将窗口放在所有非最顶层窗口(即所有最顶层窗口后面)上方。 This flag has no effect if the window is already a non-topmost window.
如果窗口已经是非最顶层窗口,则此标志无效。
Use SetWindowLongPtr
. 使用
SetWindowLongPtr
。 This function will discard your window styles, but u can restore them, like the example below: 此函数将丢弃您的窗口样式,但您可以恢复它们,如下例所示:
SetWindowLongPtr(hWnd, GWL_STYLE, WS_OVERLAPPEDWINDOW); //Discard WS_EX_TOPMOST
IMPORTANT NOTE:
重要的提示:
To write code that is compatible with both 32-bit and 64-bit versions of Windows, use SetWindowLongPtr.要编写与32位和64位版本的Windows兼容的代码,请使用SetWindowLongPtr。 When compiling for 32-bit Windows, SetWindowLongPtr is defined as a call to the SetWindowLong function.
在编译32位Windows时,SetWindowLongPtr被定义为对SetWindowLong函数的调用。
Hope this works. 希望这有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.