Test purpose: How to make the controls in the window automatically adjust to the size of the form
Test environment: WIN10 ENT + VS2015
Method principle: Record the initial window size, control size and other information. Whenever the window size changes, the window size is calculated relative to the initial scale, and then the position (location), size, and font size (font.size) of the control are adjusted based on the scale.
C # main test code:
using system;using system.drawing;using system.windows.forms;namespace test1111{ public partial class Form1 : Form { int igformwidth = new int (); //window width int igformheight = new int (); //window height float fgwidthscaling = new float (); / /width Scaling float fgheightscaling = new float () ; //Height scale public form1 () { InitializeComponent (); igformwidth = this. width; igformheight = this. Height; initcontag (This); } private void Form1_Load (object sender, eventargs e) { } private void form1_resize (object sender, eventargs e) { if (igformwidth == 0 | | igformheight == 0) return; fgWidthScaling = (float) this. width / (float) igformwidth; fgheightscaling = (float) this. height / (float) igformheight; resizecon (fgwidthscaling, fgheightscaling, this); } // Records the initial position, size, font size information for the control set private void initcontag ( Control cons) { foreach (control con in cons. Controls) //traversal control set { con. Tag = con. left + "," + con. top + "," + con. width + "," + con. height + "," + con. Font.size; if (Con. controls.count > 0) //Handling child controls { initcontag (Con); } } } //Resize Position, size, font size private void resizecon (Float widthscaling, float heightscaling, control cons) { &nbsP; float ftmp = new float (); foreach (control con in cons. Controls) //traversal control set { string[] conTag = con. Tag.tostring (). Split (new char[] {', '}); ftmp = convert.tosingle (contag[0]) * widthScaling; con. left = (int) ftmp; ftmp = convert.tosingle (contag[1]) * heightScaling; con. top = (int) ftmp; ftmp = convert.tosingle (contag[2]) * widthScaling; con. width = (int) ftmp; ftmp = convert.tosingle (contag[3]) * heightScaling; con. height = (int) ftmp; ftmp = convert.tosingle (Contag[4]) * widthScaling * heightscaling; Con. Font = new font ("", ftmp); if (Con. controls.count > 0) //Handling child controls { resizecon (Widthscaling, heightscaling, con); } } } }}
To run the initial diagram:
650) this.width=650; "Src=" Https://s4.51cto.com/oss/201711/11/8460ef1492be77d33e48ab9c01e8e97f.png-wh_500x0-wm_3 -wmp_4-s_1597195286.png "title=" Img_01.png "alt=" 8460ef1492be77d33e48ab9c01e8e97f.png-wh_ "/>
Window resizing after Image:
650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/11/7ba30d431d6dee9d0584f43be9ceecf3.png-wh_500x0-wm_3 -wmp_4-s_4294428054.png "title=" Img_02.png "alt=" 7ba30d431d6dee9d0584f43be9ceecf3.png-wh_ "/>
You can see that when the size of the form changes, the position, size, and font size of the controls in the body have changed slightly.
Thanks for reading!
This article is from the "firswof" blog, make sure to keep this source http://firswof.blog.51cto.com/9362961/1980896
C # Form controls automatically adjust following form size