C # Form controls automatically adjust following form size

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.