6

Visual Studio 2012を使用しています。フォームを開いたときに、フォームが画面の中央に表示されません。フォームをにStartPosition設定しCenterScreenていますが、常に左側のモニターの左上隅から始まります(2つのモニターがあります)。

何か案は?ありがとう

4

1 に答える 1

7

この方法を試してください!

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;

        }
    }
}

アプリケーションのコンストラクターで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); 
        }
    }
}
于 2013-01-12T12:32:55.200 に答える