我在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();
还有其他方法吗?
请帮忙。
答案 0 :(得分:6)
看看以下内容:
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();
}
窗口出现在屏幕中央。
答案 1 :(得分:4)
只需将表单的StartPosition
属性设置为CenterScreen
.....
答案 2 :(得分:0)
我有类似的问题;在CenterToScreen()之后,表单偏离中心。然后我注意到我正在使用InitializeComponent()进行此操作。我在InitializeComponent之后移动它并且它工作正常。