【问题标题】:How to keep form centered to the middle of the screen after resize调整大小后如何保持表单居中在屏幕中间
【发布时间】:2018-02-03 07:08:59
【问题描述】:

我有一个表格,它根据我在加载时按字体大小调整大小的屏幕位置居中。调整大小后,位置与调整大小之前保持一致,因此表单不再像我想要的那样位于中心。

让我给你画个草图:

我试过打电话

        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.ResumeLayout(false);
        this.PerformLayout();

再次调整大小后(我相信,这是在开始时居中表单的代码部分)。它没有用。我还发现了一些类似的问题: "Keeping winform control centered after window resize " 但他们总是只处理控件的居中,而不是表单本身。

【问题讨论】:

    标签: c# forms winforms centering


    【解决方案1】:

    ResizeEnd 事件添加方法。在方法中,当ResizeEnd 被触发时,获取当前屏幕大小(在多个监视器上,包含当前表单的屏幕),然后计算表单的位置。看看这个例子

    private void Form1_ResizeEnd(object sender, EventArgs e)
    {
        Screen myScreen = Screen.FromControl(this);
        Rectangle area = myScreen.WorkingArea;
    
        this.Top = (area.Height - this.Height) / 2;
        this.Left = (area.Width - this.Width) / 2;
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-17
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-09
      相关资源
      最近更新 更多