C# 形成最顶层?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/324463/
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
Form top most?
提问by Pokus
How can I display something over all other application. I want to to display something over all form of my program and all other programs open on my desktop (not mine).
如何在所有其他应用程序上显示某些内容。我想在我的程序的所有形式以及我的桌面上打开的所有其他程序(不是我的)上显示一些内容。
*Top Most doesn't work I have tested and my browser can go OVER my application :S
*Top Most 不起作用我已经测试过了,我的浏览器可以超过我的应用程序:S
Here is an image of when I use TopMost to TRUE. You can see my browser is over it...
这是我使用 TopMost 为 TRUE 时的图像。你可以看到我的浏览器已经结束了......
采纳答案by Patrick Desjardins
You can use the form instance and set the property TopMostto True.
您可以使用表单实例并将属性TopMost设置为 True。
如果您想浏览所有 Windows,还有另一种方法 Win32 ApiWin32 Api调用。
Here is what you could do:
您可以这样做:
In your form class add :
在您的表单类中添加:
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
In the form load you can add :
在表单加载中,您可以添加:
SetForegroundWindow(this.Handle);
This should do the trick.
这应该可以解决问题。
Update
更新
TopMostshould do the job BUT: Top most OR/AND the Win32 Api call will only work not inside Visual Studio (well for Vista and with VS2008 I tested it... I can't tell for other). Try running the program with the .Exe from the /bin directory, it will works.
TopMost应该完成这项工作但是:最重要的 OR/AND Win32 Api 调用只能在 Visual Studio 中工作(对于 Vista 和 VS2008 我测试过它......我无法告诉其他人)。尝试使用 /bin 目录中的 .Exe 运行该程序,它将起作用。
回答by Yona
The Form.TopMost property will set your form the top form above all other running windows applications (not just your forms.)
Form.TopMost 属性会将您的表单设置为所有其他正在运行的 Windows 应用程序(不仅仅是您的表单)之上的顶级表单。
myForm.TopMost = true; // This will do the job
回答by Bobby Z
TopMost property is what you need (never had a problem with that)
TopMost 属性就是你所需要的(从来没有遇到过这个问题)
On MSDN it says:
在 MSDN 上它说:
A topmost form is a form that overlaps all the other (non-topmost) forms even if it is not the active or foreground form. Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop.
最顶层窗体是与所有其他(非最顶层)窗体重叠的窗体,即使它不是活动窗体或前景窗体。最顶层的窗体始终显示在桌面上窗口的 z 顺序中的最高点。
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost.aspx
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost.aspx
回答by Bobby Z
In my team we had a internal tool that keeps on running after Windows startup. It is a WinForm with TopMost set to ture. On Vista, sometimes we had the same problem. At very random times, the form will lost TopMost property and other non-topmost window can appear above it. I had a log of researches but found no answer and many other people had this same problem, it seems that on Vista at very low level there is a bug about TopMost property.
在我的团队中,我们有一个内部工具,可以在 Windows 启动后继续运行。它是一个 TopMost 设置为 ture 的 WinForm。在 Vista 上,有时我们会遇到同样的问题。在非常随机的时间,窗体将失去 TopMost 属性,并且其他非最顶层窗口可以出现在其上方。我有一份研究日志,但没有找到答案,许多其他人也遇到了同样的问题,似乎在非常低级别的 Vista 上存在关于 TopMost 属性的错误。