简体   繁体   English

Form.TopMost有时会工作

[英]Form.TopMost works sometimes

It appears that the topmost property sometimes puts my application above all others, but throughout my testing it has been very weird in that sometimes it works and the window remains above all other (external application) windows, but sometimes it does nothing at all. 似乎最顶层的属性有时会使我的应用程序高于其他所有属性,但在我的测试中,它有点奇怪,因为它有时会工作并且窗口仍然高于所有其他(外部应用程序)窗口,但有时它根本不做任何事情。 I have even tried using the WS_EX_TOPMOST flag by setting it with the Win32 API call to setWindowLong(). 我甚至尝试使用WS_EX_TOPMOST标志,通过调用setWindowLong()的Win32 API来设置它。 None of them keep the window on top. 他们都没有将窗户保持在最顶层。 Is there another way to keep a window on top of every open window besides using topmost? 除了使用最顶层窗口之外,还有另一种方法可以将窗口保持在每个打开的窗口之上吗? Or is there something else I should know about topmost? 或者还有其他我应该知道的关于最顶层的东西?

I simply use this: 我只是用这个:

form.TopLevel = true;
form.TopMost  = true;

which makes the window top-level (ie it has no parent and behaves as the main form of the application), then ensures that it is topmost (ie displays above all other non-topmost windows). 这使得窗口处于顶层(即它没有父级并且表现为应用程序的主要形式),然后确保它位于最顶层(即显示在所有其他非最顶层窗口之上)。 It has always worked like a charm. 它总是像魅力一样工作。

Note that I do this before I show the window. 请注意,我在显示窗口之前执行此操作。

I've had luck with the following win32 api calls: 我对以下win32 api调用很幸运:

const int SW_SHOW = 5;
BringWindowToTop(form.Handle);
ShowWindow(form.Handle, SW_SHOW);

In addition to Form.TopMost you can try the Win32 API SetForegroundWindow . 除了Form.TopMost您还可以尝试Win32 API SetForegroundWindow

[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32", CharSet=CharSet.Ansi, SetLastError=true, ExactSpelling=true)]
public static extern bool SetForegroundWindow(IntPtr hwnd);

Then call SetForegroundWindow(this.Handle) . 然后调用SetForegroundWindow(this.Handle)

Works 100%! 100%工作!

User32.AllowSetForegroundWindow((uint)Process.GetCurrentProcess().Id);
User32.SetForegroundWindow(Handle);
User32.ShowWindow(Handle, User32.SW_SHOWNORMAL);

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

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