C# 启动时的中心表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14007458/
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
Center Form on Startup
提问by pcnThird
I'm working on a web browser in C#, so I made a splash screen for it. However, the splash screen isn't located at the center of the screen when it starts up. So is there a way to center the form on startup?
我正在使用 C# 开发 Web 浏览器,因此我为它制作了启动画面。但是,启动画面在启动时并不位于屏幕中央。那么有没有办法在启动时将表单居中?
Working code:
工作代码:
public splash()
{
InitializeComponent();
StartPosition = FormStartPosition.CenterScreen;
}
采纳答案by debracey
StartPosition = FormStartPosition.CenterScreen;
StartPosition = FormStartPosition.CenterScreen;
回答by Vlad Spreys
If you would like to do it from GUI and you work with Visual Studio, then use these steps:
1. Open your form in the form designer
2. Navigate to form properties
3. Change the "StartPosition" to "CenterScreen"
如果您想从 GUI 执行此操作并使用 Visual Studio,请使用以下步骤:
1. 在表单设计器中打开您的表单
2. 导航到表单属性
3. 将“StartPosition”更改为“CenterScreen”
回答by M Alam Telot
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Method 1. center at initilization
this.StartPosition = FormStartPosition.CenterScreen;
//Method 2. The manual way
this.StartPosition = FormStartPosition.Manual;
this.Top = (Screen.PrimaryScreen.Bounds.Height - this.Height)/2;
this.Left = (Screen.PrimaryScreen.Bounds.Width - this.Width)/2;
}
}
}