gpt4 book ai didi

c# - StartPosition 设置为 CenterPosition 但我的表格没有居中

转载 作者:太空狗 更新时间:2023-10-29 23:08:58 25 4
gpt4 key购买 nike

我使用的是 Visual Studio 2012。我的表单在打开时不在屏幕中央。我将表单的 StartPosition 设置为 CenterScreen,但它始终从我的左显示器的左上角开始(我有 2 个显示器)。

有什么想法吗?谢谢

最佳答案

试试这个方法!

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;

}
}
}

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

this.Text; 
this.MaximumSize;

不要在构造函数中调用虚拟成员,这可能会导致异常行为

固定代码

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);
}
}
}

关于c# - StartPosition 设置为 CenterPosition 但我的表格没有居中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14293241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com