Adaptive size form winform

1. Add a class 
class AutoSizeFormClass { // (1) Statement structure, only the recording position and size of the initial forms and their controls. public struct controlRect { public int Left; public int Top; public int Width; public int Height; } // (2). Declare a target // Note that you can not use control list entries List nCtrl ;, because the association of control, records are always current size. // public List oldCtrl = new List (); // here will be less than the number is greater than Western are filtered out, can only be replaced by Chinese, the use of text you want to change back to the West public List<controlRect> oldCtrl = new List<controlRect>(); int ctrlNo = 0;//1; // (3) Create two functions //(3.1) recorded initial position and size of the window and its controls, public void controllInitializeSize(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 is "the form itself", adding only once AddControl (mForm); // rest of the body control window may also be nested controls (such as panel), to extract alone, due to the recursive call //this.WindowState = (System.Windows.Forms.FormWindowState) (2); // After completion of the initial position and size of the control record, and then maximize //0 - Normalize , 1 - Minimize,2- Maximize } private void AddControl(Control ctl) { foreach (Control c in ctl.Controls) {// ** on here, it is to record the child controls, after recording control itself //if (c.Controls.Count > 0) // AddControl (c); // window rest the body controls may also be nested controls (such as panel), to extract alone, due to the recursive call controlRect objCtrl; objCtrl.Left = c.Left; objCtrl.Top = c.Top; objCtrl.Width = c.Width; objCtrl.Height = c.Height; oldCtrl.Add(objCtrl); // ** on here, is the first recording control itself, the child controls after recording controls if (c.Controls.Count > 0) AddControl (c); // window rest the body controls may also be nested controls (such as panel), to extract alone, due to the recursive call } } //(3.2) adaptive control size, public void controlAutoSize(Control mForm) { if (ctrlNo == 0) {// Form1_Load * If the form, size and location of the original recording controls, normally there is no problem, but there will be added to skin problems, because some controls such as dataGridView of child controls is not yet complete, a small number of // * To Form1_SizeChanged the form, the first time you change the size, recording control of the original size and position, where all the controls of the child controls have formed controlRect 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 is "the form itself", adding only once AddControl (mForm); // the rest of the window may be nested within other controls controls (such as panel), so that the recursive call to separate out } float wScale = (float) mForm.Width / (float) oldCtrl [0] .Width; // the ratio between the new form, the first form of the old float hScale = (float)mForm.Height / (float)oldCtrl[0].Height;//.Height; ctrlNo = 1; // enter = 1, 0 is the form itself, the body of the control window, starting from the number 1 AutoScaleControl (mForm, wScale, hScale); // window rest the body controls may also be nested controls (such as panel), to extract alone, due to the recursive call } private void AutoScaleControl(Control ctl, float wScale, float hScale) { int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0; // int ctrlNo = 1; // The first one is a form of self-Left, Top, Width, Height, so the form controls from ctrlNo = 1 start foreach (Control c in ctl.Controls) {// ** on here, is the first child controls the zoom control, the zoom control itself //if (c.Controls.Count > 0) // AutoScaleControl (c, wScale, hScale); // window rest the body controls may also be nested controls (such as panel), to extract alone, due to the recursive call ctrLeft0 = oldCtrl[ctrlNo].Left; ctrTop0 = oldCtrl[ctrlNo].Top; ctrWidth0 = oldCtrl[ctrlNo].Width; ctrHeight0 = oldCtrl[ctrlNo].Height; //c.Left = (int) ((ctrLeft0 - wLeft0) * wScale) + wLeft1; // linear proportion between the new control //c.Top = (int)((ctrTop0 - wTop0) * h) + wTop1; c.Left = (int) ((ctrLeft0) * wScale); // linear proportion between old and new controls. Control position relative to the form only, so you can not add + wLeft1 c.Top = (int)((ctrTop0) * hScale);// c.Width = (int) (ctrWidth0 * wScale); // only associated with the size of the original, and now it is not multiplied by the width (int) (c.Width * w); c.Height = (int)(ctrHeight0 * hScale);// ctrlNo ++; // accumulated number // ** on here, it is the first zoom control itself, after scaling of child controls controls if (c.Controls.Count > 0) AutoScaleControl (c, wScale, hScale); // window rest the body controls may also be nested controls (such as panel), to extract alone, due to the recursive call if (ctl is DataGridView) { DataGridView dgv = ctl as DataGridView; Cursor.Current = Cursors.WaitCursor; int widths = 0; for (int i = 0; i < dgv.Columns.Count; i++) { dgv.AutoResizeColumn (i, DataGridViewAutoSizeColumnMode.AllCells); // column automatically widths + = dgv.Columns [i] .Width; // calculate the cell row and column width adjustment } if (widths> = ctl.Size.Width) // if the column width is larger than the set adjustment column width dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; // automatically adjust the column mode else dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // if the filling is less than Cursor.Current = Cursors.Default; } } } }

2. instantiated

     AutoSize as = new AutoSize();

3.Form1_Load

     as.controllInitializeSize(this);

4. Form1 _SizeChanged event

     as.controlAutoSize(this);

This realization of adaptive control the size of the window form

Guess you like

Origin www.cnblogs.com/xfcode/p/11769307.html