StartPosition设置为CenterPosition,但是我的表单未居中 [英] StartPosition is set to CenterPosition but my form is not centered

查看:279
本文介绍了StartPosition设置为CenterPosition,但是我的表单未居中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio2012.我的窗体在打开时不会居中显示在屏幕上.我已将窗体的StartPosition设置为CenterScreen,但是它总是从我的左监视器(我有2个监视器)的左上角开始.

I'm using Visual Studio 2012. My form, when it opens doesn't center to the screen. I have the form's StartPosition set to CenterScreen, but it always starts in the top left corner of my left monitor (I have 2 monitors).

有什么想法吗?谢谢

推荐答案

尝试这种方式!

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;

        }
    }
}

在应用程序的构造函数中调用了两个虚拟成员.

Two virtual members are called in constructor of the application.

this.Text; 
this.MaximumSize;

请勿在构造函数中调用虚拟成员,否则可能导致异常行为

do not call virtual member in constructor it may lead to abnormal behaviour

固定代码

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();



            this.Location = new System.Drawing.Point(100, 100);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            // to see if form is being centered, disable maximization
            //this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "Convertor";
            this.MaximumSize = new System.Drawing.Size(620, 420); 
        }
    }
}

这篇关于StartPosition设置为CenterPosition,但是我的表单未居中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆