简体   繁体   English

当此顶级值发生变化时创建一个事件

[英]Create an Event for when this,toplevel value changes

I want to be able to have my application to always be on top. 我希望能够使我的应用程序始终排在最前面。 so when I open a new program and that becomes on top even though I have this.TopLevel value set to true, the application will see it is not on top no more and then go back on top. 因此,当我打开一个新程序时,即使将this.TopLevel值设置为true,该程序也将位于顶部,应用程序将看到它不再位于顶部,然后返回顶部。 I know I can do this with a timer, but I am hoping their is a better way. 我知道我可以使用计时器来完成此操作,但我希望它们是更好的方法。

You can use a method located in user32.dll . 您可以使用位于user32.dll的方法。

using System.Runtime.InteropServices;

You are going to need these variables: 您将需要以下变量:

private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;

Import the method from the DLL... 从DLL导入方法...

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);

Then in your code, use this to set the window position to the topmost window. 然后在您的代码中,使用它来将窗口位置设置为最上面的窗口。

SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);

I hope this helps you! 我希望这可以帮助你!

I suggest you catch the event for new windows and see if you are on top after the new window opens: 我建议您在新窗口中捕获事件,并在新窗口打开后查看您是否位于顶部:

https://stackoverflow.com/a/40698254/2557128 https://stackoverflow.com/a/40698254/2557128

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

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