C # WinForm forms and Controls adaptive size

Source: Internet
Author: User

1. Create the class Autosizeform in the project

AutoSizeForm.cs File Code:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacecsharpformapplication{classAutoresizeform {//(1). Declares a structure that records only the initial position and size of the form and its controls.              Public structControlrect { Public intLeft ;  Public intTop;  Public intWidth;  Public intHeight; }            //(2). Declaring 1 Objects//Note that you cannot use the control list to record list Nctrl; Because of the control's associativity, the record is always the current size. //Public list oldctrl= new list ();//here will be the Latin greater than less than the number is filtered out, can only be changed to Chinese, use to change back to Latin             PublicList<controlrect> Oldctrl =NewList<controlrect>(); intCtrlno =0;//1; //(3). Create two functions//(3.1) record the initial position and size of the form and its controls,             Public voidcontrollinitializesize (Control mform) {controlrect CR; Cr.left= Mform.left; Cr.top = Mform.top; Cr.width = Mform.width; Cr.height =Mform.height; Oldctrl.add (CR);//The first one is "form itself", only one timeAddControl (Mform);//The rest of the control in the window may also nest controls (such as a panel) and be pulled out separately, because recursive calls//This . WindowState = (System.Windows.Forms.FormWindowState) (2);//after you have recorded the initial position and size of the control, maximize//0-normalize, 1-minimize,2-maximize            }            Private voidAddControl (Control ctl) {foreach(Control Cinchctl. Controls) {//* * Here, the control's child controls are recorded first, and then the control itself is recorded//if (C.controls.count > 0)//AddControl (c);//The rest of the control in the window may also nest controls (such as a panel) and be pulled out separately, because recursive callsControlrect Objctrl; Objctrl.left= C.left; Objctrl.top = C.top; Objctrl.width = C.width; Objctrl.height =C.height;                    Oldctrl.add (Objctrl); //* * Here, the control itself is recorded before the control's child controls are recorded                    if(C.controls.count >0) AddControl (c);//The rest of the control in the window may also nest controls (such as a panel) and be pulled out separately, because recursive calls                }            }            //(3.2) Control adaptive size,             Public voidcontrolautosize (Control mform) {if(Ctrlno = =0)                { //* If the original size and position of the control are recorded in the Form1_Load of the form, there is no problem, but there is a problem with adding skins because some controls such as DataGridView's child controls are not yet complete and the number is small//* To record the original size and position of the control in the form's form1_sizechanged, the first time the size is changed, all the control's child controls have been formedControlrect CR; //cr.left = mform.left; cr.top = mform.top; cr.width = mform.width; cr.height = mform.height;Cr.left =0; Cr.top =0; Cr.width = MForm.PreferredSize.Width; Cr.height =MForm.PreferredSize.Height; Oldctrl.add (CR);//The first one is "form itself", only one timeAddControl (Mform);//the rest of the control within the window may nest other controls (such as a panel), so it is pulled out separately for recursive invocation                }                floatWscale = (float) Mform.width/(float) oldctrl[0]. Width;//the proportions between the old and new forms, with the oldest old form                floatHscale = (float) Mform.height/(float) oldctrl[0]. Height;//. Height;Ctrlno =1;//Enter = 1, the No. 0 is the form itself, the control in the window, starting with the ordinal 1Autoscalecontrol (Mform, Wscale, Hscale);//The rest of the control in the window may also nest controls (such as a panel) and be pulled out separately, because recursive calls            }            Private voidAutoscalecontrol (Control ctl,floatWscale,floatHscale) {                intctrLeft0, CtrTop0, CtrWidth0, ctrHeight0; //int ctrlno = 1;//The 1th One is the left,top,width,height of the form itself, so the form control starts with Ctrlno=1                foreach(Control Cinchctl. Controls) {//* * Here, the control's child controls are scaled first, and then the control itself//if (C.controls.count > 0)//Autoscalecontrol (c, Wscale, Hscale);//The rest of the control in the window may also nest controls (such as a panel) and be pulled out separately, because recursive callsCtrLeft0 =Oldctrl[ctrlno].                    Left; CtrTop0=Oldctrl[ctrlno].                    Top; CtrWidth0=Oldctrl[ctrlno].                    Width; CtrHeight0=Oldctrl[ctrlno].                    Height; //c.left = (int) ((ctrleft0-wleft0) * Wscale) + wLeft1;//linear proportions between old and new controls//c.top = (int) ((ctrtop0-wtop0) * h) + WTop1;C.left = (int) ((ctrLeft0) * Wscale);//the linear scale between the old and new controls. The control position is relative to the form, so you cannot add + wLeft1C.top = (int) ((ctrTop0) * Hscale);//C.width = (int) (CtrWidth0 * wscale);//only relevant to the initial size, so it cannot be multiplied by the current width (int) (C.width * W);C.height = (int) (CTRHEIGHT0 * hscale);//ctrlno++;//Cumulative Number//* * Here, it is the child controls that scale the control itself before scaling the control                    if(C.controls.count >0) Autoscalecontrol (c, Wscale, Hscale);//The rest of the control in the window may also nest controls (such as a panel) and be pulled out separately, because recursive calls                    if(CTL isDataGridView) {DataGridView DGV= CTL asDataGridView; Cursor.current=Cursors.waitcursor; intWidths =0;  for(inti =0; I < DGV. Columns.count; i++) {DGV.  AutoResizeColumn (i, datagridviewautosizecolumnmode.allcells); //automatically adjust column widthsWidths + = DGV. Columns[i]. Width;//calculates the width of the cell column after the adjustment column and                        }                        if(Widths >= ctl.) Size.width)//if the width of the adjustment column is greater than the set column widthDGV. autoSizeColumnsMode = Datagridviewautosizecolumnsmode.displayedcells;//Adjust the mode of a column automatically                        ElseDGV. autoSizeColumnsMode= Datagridviewautosizecolumnsmode.fill;//if it is less than the fillcursor.current=Cursors.Default; }                }            }        }    }

2. Customize the Global class object in a form that you want to adapt to size

New Autoresizeform ();

3. Execute the object method in the Load event and Sizechange event of the form to be adaptive size

        Private void Widgetautoresizeform_load (object  sender, EventArgs e)        {            asc.controllinitializesize (  this);        }         Private void Widgetautoresizeform_sizechanged (object  sender, EventArgs e)        {            asc.controlautosize (  this);        }

From form code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacecsharpformapplication{ Public Partial classwidgetautoresizeform:form {autoresizeform ASC=NewAutoresizeform ();  PublicWidgetautoresizeform () {InitializeComponent (); }        Private voidWidgetautoresizeform_load (Objectsender, EventArgs e) {Asc.controllinitializesize ( This); }        Private voidWidgetautoresizeform_sizechanged (Objectsender, EventArgs e) {Asc.controlautosize ( This); }    }}

C # WinForm forms and Controls adaptive 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.