C # WinForm Control Adaptive Form Size

Requirement: When the size of the form changes dynamically, various controls in the form (including Panel and sub-controls in the Panel) can dynamically adjust their size to adapt to the proportion of the form content.

Method:

The first step is to create a new class, the code is as follows:

class Resize

{
private Form _form;

public Resize(Form form)
{
int count = form.Controls.Count * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i
++] = form.Size.Width;
factor[i
++] = form.Size.Height;
foreach (Control ctrl in form.Controls)
{
factor[i
++] = ctrl.Location.X / (float)form.Size.Width;
factor[i
++] = ctrl.Location.Y / (float)form.Size.Height;
ctrl.Tag
= ctrl.Size;
}
form.Tag
= factor;
this._form = form;
}

public void Form1_Resize(object sender, EventArgs e)
{
float[] scale = (float[]) this._form.Tag;
int i = 2;
foreach (Control ctrl in this._form.Controls) //panel length When the width grows to a fixed value, it will not grow again. Reason: The upper limit of the width and height of the Panel is 65535 pixels (https: //blog.csdn.net/dufangfeilong/article/details/41805073?utm_source=blogxgwz5)
{
ctrl.Left
= (int)(this. _form.Size.Width * scale[i++]);
ctrl.Top
= (int)(this. _form.Size.Height * scale[i++]);
ctrl.Width
= (int)(this. _form.Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
ctrl.Height
= (int)(this. _form.Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
}
}
}

The second step is to use this class in the initialization function of the Form:

 < span style="color: #0000ff;">public Form_StockCount()

{
InitializeComponent();

this.SizeChanged += new Resize(this).Form1_Resize; //window Responsive code
}

class Resize

{
private Form _form;

public Resize(Form form)
{
int count = form.Controls.Count * 2 + 2;
float[] factor = new float[count];
int i = 0;
factor[i
++] = form.Size.Width;
factor[i
++] = form.Size.Height;
foreach (Control ctrl in form.Controls)
{
factor[i
++] = ctrl.Location.X / (float)form.Size.Width;
factor[i
++] = ctrl.Location.Y / (float)form.Size.Height;
ctrl.Tag
= ctrl.Size;
}
form.Tag
= factor;
this._form = form;
}

public void Form1_Resize(object sender, EventArgs e)
{
float[] scale = (float[]) this._form.Tag;
int i = 2;
foreach (Control ctrl in this._form.Controls) //panel length When the width grows to a fixed value, it will not grow again. Reason: The upper limit of the width and height of the Panel is 65535 pixels (https: //blog.csdn.net/dufangfeilong/article/details/41805073?utm_source=blogxgwz5)
{
ctrl.Left
= (int)(this. _form.Size.Width * scale[i++]);
ctrl.Top
= (int)(this. _form.Size.Height * scale[i++]);
ctrl.Width
= (int)(this. _form.Size.Width / (float)scale[0] * ((Size)ctrl.Tag).Width);
ctrl.Height
= (int)(this. _form.Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
}
}
}

 public Form_StockCount()

{
InitializeComponent();

this.SizeChanged += new Resize(this).Form1_Resize; //window Responsive code
}

Leave a Comment

Your email address will not be published.