启动时居中显示窗体

13

我正在用C#编写一个Web浏览器,因此我为它制作了一个启动画面。然而,在启动时,启动画面并不位于屏幕中心。那么,有什么方法可以在启动时将窗体居中显示吗?

图片描述

代码如下:

public splash()
    {
        InitializeComponent();

        StartPosition = FormStartPosition.CenterScreen;
    }

这实际上不是存储GUI自定义的最佳位置。考虑使用Form1.Designer.cs代替。 - Allan Spreys
在您的情况下,splash.Designer.cs 很可能是这样的。 - Allan Spreys
4个回答

29

StartPosition = FormStartPosition.CenterScreen;

这段代码是设置窗体的启动位置为屏幕中央。更多信息请查看MSDN FormStartPosition文档

很好。请确保接受您认为最有帮助的答案(点击空白复选框使其变绿并接受它)。 - debracey

6
form.StartPosition = FormStartPosition.CenterScreen;

请看 MSDN

3
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;

    }
  }
}

3

如果您想通过GUI执行此操作并且使用Visual Studio,则请按照以下步骤进行操作:
1. 在表单设计器中打开您的表单
2. 导航到表单属性
3. 将“StartPosition”更改为“CenterScreen”


1
我简直不能相信我竟然错过了那个!它一直在属性面板里。 - pcnThird

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接