C# 如何使 Windows 窗体始终显示在顶部?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1147149/
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
how to make a Windows Form always display on the top?
提问by George2
I am using VSTS 2008 + C# + .Net 2.0 to develop a Windows Forms application. In the default Form1 I have a button, and click the button will invoke another Form -- Form2.
我正在使用 VSTS 2008 + C# + .Net 2.0 来开发 Windows 窗体应用程序。在默认的 Form1 中,我有一个按钮,单击该按钮将调用另一个 Form——Form2。
My question is, I want to make Form2 always on the top, i.e. user must response Form2 (fill in informaiton in Form2 and close it) until the user could continue to deal with Form1. How to implement this feature?
我的问题是,我想让Form2始终在顶部,即用户必须响应Form2(在Form2中填写信息并关闭它),直到用户可以继续处理Form1。如何实现这个功能?
Here is my current code.
这是我当前的代码。
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Visible = true;
}
}
采纳答案by Alex McBride
The best option to do exactly what you want is to make form2 a dialog box. You do this by calling its
做你想做的最好的选择是让 form2 成为一个对话框。你可以通过调用它来做到这一点
form2.ShowDialog()
method.
方法。
回答by Micha? Ziober
try this
尝试这个
this.TopMost = true;
回答by Bevan
Assuming that you want to prevent the user from interacting with Form1 until they're finished with Form2, you want the ShowDialog()
method.
假设您想阻止用户在完成 Form2 之前与 Form1 交互,那么您需要该ShowDialog()
方法。
回答by Oliver Friedrich
With native .NET theres no way to put a form on top and hold it there.
使用本机 .NET 无法将表单放在顶部并保持在那里。
Form.TopMost sets the form on top only on create. Form.ShowDialog() sets the form on top of all forms of that application, but can then be thrown backwards behind other applications as well.
Form.TopMost 仅在创建时将表单设置在顶部。Form.ShowDialog() 将表单设置在该应用程序的所有表单之上,但随后也可以被抛到其他应用程序的后面。
I remember that we used some P/Invoke-Calls to native Win32 to handle that case, but do not remember what calls exactly. Anyway 100% were never reached, spreaded on Win2000 to WinXP nothing worked everywhere.
我记得我们使用了一些对原生 Win32 的 P/Invoke-Calls 来处理这种情况,但不记得究竟是什么调用。无论如何 100% 从来没有达到过,在 Win2000 上传播到 WinXP 没有任何效果。
回答by Oliver
You can use the Win32 ::SetWindowPos() method and set the HWND hWndInsertAfter to HWND_TOPMOST so that it stays on top.
您可以使用 Win32 ::SetWindowPos() 方法并将 HWND hWndInsertAfter 设置为 HWND_TOPMOST 以使其保持在顶部。
Look here for the SetWindowPos documentation: http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx
在此处查看 SetWindowPos 文档:http: //msdn.microsoft.com/en-us/library/ms633545(VS.85) .aspx
[DllImport("user32.dll")]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
Here are some examples: http://www.pinvoke.net/default.aspx/user32/SetWindowPos.html
以下是一些示例:http: //www.pinvoke.net/default.aspx/user32/SetWindowPos.html