How to make a winform window adapt to screen size?

In WinForms, you can use the following method to make the form adapt to the screen size:

  1. Set the Size property of the form to match the screen dimensions.
this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
  1. In the Load event of the window, set the Size property of the window to the size of the screen’s working area.
private void Form1_Load(object sender, EventArgs e)
{
    this.Size = Screen.PrimaryScreen.WorkingArea.Size;
}
  1. You can utilize the Anchor and Dock properties to adjust the position and size of controls, allowing them to adapt to the size of the window.

When using the Anchor property, set the control’s Anchor property to Top Left, Top Right, Bottom Left, and Bottom Right so that the control will adjust its size with the form and maintain a fixed margin.

When using the Dock property, setting the control’s Dock property to Fill will automatically fill the entire space of the parent container.

By combining the Anchor and Dock properties, a more flexible layout can be achieved.

  1. To ensure that the controls adjust their size accordingly when the form size changes, you can set the position and size of the controls in the form’s Resize event.

For example, keeping a Label control consistently positioned in the bottom right corner of the window:

private void Form1_Resize(object sender, EventArgs e)
{
    label1.Left = this.ClientSize.Width - label1.Width;
    label1.Top = this.ClientSize.Height - label1.Height;
}

The above are some commonly used methods, choose the appropriate one based on your actual needs to achieve the form adapting to the screen size.

bannerAds