简体   繁体   English

将 window 设置为最顶层

[英]Set a window to be topmost

I am trying to keep my window on top of the all others.我试图将我的 window 放在所有其他人之上。 I am new to C++ Win32 programming.我是 C++ Win32 编程的新手。 This is my initialization of my window in WinMain :这是我在WinMain中对 window 的初始化:

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.但是在这里,在 window 上,我不知道如何设置它。 I also want to be able to trigger it.我也希望能够触发它。 Can anybody help me?有谁能够帮我?

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

Note: SWP_NOMOVE | SWP_NOSIZE注意: SWP_NOMOVE | SWP_NOSIZE SWP_NOMOVE | SWP_NOSIZE are for ignoring 3rd, 4th, 5th, 6th parameters of the SetWindowPos function. SWP_NOMOVE | SWP_NOSIZE用于忽略SetWindowPos函数的第SetWindowPos个参数。

The second parameter can be:第二个参数可以是:

  • HWND_BOTTOM

  • HWND_NOTOPMOST (set window to be a normal window) HWND_NOTOPMOST (设置窗口为普通窗口)

  • HWND_TOP

  • HWND_TOPMOST (set window to be always on top) HWND_TOPMOST (设置窗口总是在顶部)

Use CreateWindowEx with (extended) window style WS_EX_TOPMOST .CreateWindowEx与(扩展的)窗口样式WS_EX_TOPMOST

Disclaimer: it's about 15 years or so since I touched that stuff.免责声明:我接触这些东西大约有 15 年了。

see SetWindowPos , hWndInsertAfter parameter.请参阅SetWindowPoshWndInsertAfter参数。 passing HWND_TOPMOST should do what you want.通过HWND_TOPMOST应该做你想做的。

additionally, you may want to pass SWP_NOMOVE | SWP_NOSIZE此外,您可能希望通过SWP_NOMOVE | SWP_NOSIZE SWP_NOMOVE | SWP_NOSIZE to uFlags parameter if you want to keep position and size unchanged. SWP_NOMOVE | SWP_NOSIZEuFlags参数,如果你想保持位置和大小不变。

SWP_NOMOVE Retains the current position (ignores X and Y parameters). SWP_NOMOVE 保留当前位置(忽略 X 和 Y 参数)。 SWP_NOSIZE Retains the current size (ignores the cx and cy parameters). SWP_NOSIZE 保留当前大小(忽略 cx 和 cy 参数)。 If you don't set these flags you should specify position and size instead of passing 0, 0, 0, 0如果你不设置这些标志,你应该指定位置和大小而不是传递 0, 0, 0, 0

I did something i little bit differnt kinda found it much easier我做了一些我有点不同的事情发现它更容易

so first on Form Load所以首先在表单加载

private void Form1_Load(object sender, EventArgs e)
{       
   this.Shown += new EventHandler(Form1_Shown);//let your form show up here
}

private void Form1_Shown(Object sender, EventArgs e)
{
  Form1.ActiveForm.TopMost = true //and then do your TopMost logic 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM