How to keep form centered to the middle of the screen after resize

前端 未结 1 1274
天涯浪人
天涯浪人 2021-01-19 13:30

I have a form that is centered according to the screen position that I resize by fontsize when loading. After resizing the location remains the same as it was before resizin

相关标签:
1条回答
  • 2021-01-19 14:17

    Add method for ResizeEnd event. In method, when ResizeEnd is fired, get current screen size (on multiple monitors, screen that contains current form) and then calculate form's position. Take a look at this example

    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;
    }
    
    0 讨论(0)
提交回复
热议问题