C++ 将窗口设置为最顶层

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14989062/
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-08-27 18:51:54  来源:igfitidea点击:

Set a window to be topmost

c++winapiwindowtopmost

提问by Victor

I am trying to keep my window on top of the all others. I am new to C++ Win32 programming. This is my initialization of my window in WinMain:

我试图让我的窗口在所有其他窗口之上。我是 C++ Win32 编程的新手。这是我的窗口初始化WinMain

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

I previously worked with dialogs, so the topmost property was really easy to use. But here, on a window I don't know how to set it. I also want to be able to trigger it. Can anybody help me?

我以前使用过对话框,所以最上面的属性真的很容易使用。但是在这里,在一个窗口上我不知道如何设置它。我也希望能够触发它。有谁能够帮助我?

回答by Amir

SetWindowPos(hwnd01, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

Note:SWP_NOMOVE | SWP_NOSIZEare for ignoring 3rd, 4th, 5th, 6th parameters of the SetWindowPosfunction.

注:SWP_NOMOVE | SWP_NOSIZE用于忽略SetWindowPos函数的第 3、4、5、6 个参数。

The second parameter can be:

第二个参数可以是:

  • HWND_BOTTOM

  • HWND_NOTOPMOST(set window to be a normal window)

  • HWND_TOP

  • HWND_TOPMOST(set window to be always on top)

  • HWND_BOTTOM

  • HWND_NOTOPMOST(设置窗口为普通窗口)

  • HWND_TOP

  • HWND_TOPMOST(设置窗口总是在最上面)

回答by Cheers and hth. - Alf

Use CreateWindowExwith (extended) window style WS_EX_TOPMOST.

使用CreateWindowEx与(扩展)窗口风格WS_EX_TOPMOST

Disclaimer: it's about 15 years or so since I touched that stuff.

免责声明:我接触这些东西大约有 15 年了。

回答by Hayri U?ur Koltuk

see SetWindowPos, hWndInsertAfterparameter. passing HWND_TOPMOSTshould do what you want.

请参阅SetWindowPoshWndInsertAfter参数。通过HWND_TOPMOST应该做你想做的。

additionally, you may want to pass SWP_NOMOVE | SWP_NOSIZEto uFlagsparameter if you want to keep position and size unchanged.

此外,如果您想保持位置和大小不变,您可能需要传递SWP_NOMOVE | SWP_NOSIZEuFlags参数。

回答by vik_78

SWP_NOMOVE Retains the current position (ignores X and Y parameters). SWP_NOSIZE Retains the current size (ignores the cx and cy parameters). If you don't set these flags you should specify position and size instead of passing 0, 0, 0, 0

SWP_NOMOVE 保留当前位置(忽略 X 和 Y 参数)。SWP_NOSIZE 保留当前大小(忽略 cx 和 cy 参数)。如果你不设置这些标志,你应该指定位置和大小而不是传递 0, 0, 0, 0