3 steps:
1. Instantiate global variables in a form that needs to be adaptive AutoSizeFormClass.cs source is below
Autosizeformclass ASC = new Autosizeformclass ();
In the 2.form_load event
Asc.controllinitializesize (this);
In the 3.page_sizechanged event
Asc.controlautosize (this);
The complete code is as follows:
First, the code of the Adaptive form:
usingSystem; usingSystem.Collections.Generic; usingSystem.ComponentModel; usingSystem.Data; usingSystem.Drawing; usingSystem.Text; usingSystem.Windows.Forms; namespaceimageprocessing{ Public Partial classForm1:form {//1. Declaring an adaptive class instanceAutosizeformclass ASC =NewAutosizeformclass (); PublicForm1 () {InitializeComponent (); } //2. Add the Load event to the form, and in its method Form1_Load, invoke the initialization method of the class, recording the initial position and size of the form and its controls Private voidForm1_Load (Objectsender, EventArgs e) {Asc.controllinitializesize ( This); } //3. Add the SizeChanged event to the form, and in its method form1_sizechanged, invoke the adaptive method of the class to complete the adaptive Private voidForm1_sizechanged (Objectsender, EventArgs e) {Asc.controlautosize ( This); } } }
Ii. Code for Adaptive Classes
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Windows.Forms;namespaceimageprocessing{classAutosizeformclass {//(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; } } } }}
WinForm self-adaptation of forms and their controls