3

我的 Winforms 应用程序中的窗口位置存在问题。

我需要在启动过程中将窗口定位在屏幕中央。我尝试了以下但没有奏效。表单始终在左上角打开。

我在加载事件中尝试了这些:

this.CenterToScreen();
this.Location = new Point((Screen.PrimaryScreen.WorkingArea.Width - this.Width) / 2,
                          (Screen.PrimaryScreen.WorkingArea.Height - this.Height) / 2);

更新:我已将表格的最大和最小尺寸设为 1090,726。

我的设计器文件有这个代码:

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.GrayText;
        this.ClientSize = new System.Drawing.Size(1074, 688);
        this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
        this.MaximizeBox = false;
        this.MaximumSize = new System.Drawing.Size(1090, 726);
        this.MinimumSize = new System.Drawing.Size(1090, 726);
        this.Name = "Mail_Remainder";
        this.RightToLeftLayout = true;
        this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Remainder";
        this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        this.Load += new System.EventHandler(this.Mail_Remainder_Load);
        this.Resize += new System.EventHandler(this.Mail_Remainder_Resize);
        this.ResumeLayout(false);
        this.PerformLayout();

还有其他方法可以做到这一点吗?

请帮忙。

4

3 回答 3

7

看看以下内容:

 Form1.StartPosition = FormStartPosition.CenterScreen;
 Form1.Show();

编辑:

线

 this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

是导致偏心的原因。

如果你评论它:

    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // Form1
        // 
                    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.BackColor = System.Drawing.SystemColors.GrayText;
        this.ClientSize = new System.Drawing.Size(1074, 688);
        this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
        this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
        this.MaximizeBox = false;
        this.MaximumSize = new System.Drawing.Size(1090, 726);
        this.MinimumSize = new System.Drawing.Size(1090, 726);
        this.Name = "Mail_Remainder";
        this.RightToLeftLayout = true;
        this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Remainder";
//            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        this.Load += new System.EventHandler(this.Mail_Remainder_Load);
        this.Resize += new System.EventHandler(this.Mail_Remainder_Resize);
        this.ResumeLayout(false);
        this.PerformLayout();

    }

窗口出现在屏幕中央。

于 2012-11-22T10:27:56.407 回答
4

只需将StartPosition表单的属性设置为CenterScreen......

在此处输入图像描述

于 2012-11-22T10:28:29.807 回答
1

我有类似的问题;在 CenterToScreen () 之后,表单偏离中心。然后我注意到我在 InitializeComponent () 之前这样做了。我在 InitializeComponent 之后移动了它,它工作正常。

于 2016-06-02T19:55:41.890 回答