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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:22:16  来源:igfitidea点击:

Center Form on Startup

c#.netwinforms

提问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 浏览器,因此我为它制作了启动画面。但是,启动画面在启动时并不位于屏幕中央。那么有没有办法在启动时将表单居中?

enter image description here

在此处输入图片说明

Working code:

工作代码:

public splash()
    {
        InitializeComponent();

        StartPosition = FormStartPosition.CenterScreen;
    }

采纳答案by debracey

StartPosition = FormStartPosition.CenterScreen;

StartPosition = FormStartPosition.CenterScreen;

MSDN FormStartPosition Documentation

MSDN FormStartPosition 文档

回答by abatishchev

form.StartPosition = FormStartPosition.CenterScreen;

See MSDN.

请参阅MSDN

回答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;

    }
  }
}