C # WinFrom adaptive

1. build an adaptive class Project form.

 

2. The class code is as follows

 public  class AutoSizeForm 
    { 
        // (. 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 the Width;
             public  int the Height; 
        } 
        // . (2) a statement objects
         // Note that the recording can not be used to control the list List nCtrl ;, because the association of control, record is always current size.
        // public List oldCtrl = new new List (); // here will be less than the number is greater than Western are filtered out, can only be replaced by Chinese, and use to change back to the Western 
        public List <controlRect> oldCtrl = new newList <controlRect> ();
         int ctrlNo = 0 ; // . 1;
         // . (. 3) to create two functions
         // (3.1) and record the initial size of the form and the position of its controls, 
        ///  <Summary> 
        / // the control window size initialization method
         ///  </ Summary> 
        ///  <param name = "the mForm"> </ param> 
        public  void controllInitializeSize (control the mForm) 
        { 
                controlRect cR; 
                cR.Left = mForm.Left; cR. mForm.Top = Top; cR.Width = mForm.Width; cR.Height = mForm.Height; 
                oldCtrl.Add (cR); // The first is "form itself", was added only once
                The AddControl (the mForm); // the rest of the window is also possible to control in vivo nested controls (such as Panel), to separate out, due to the recursive call
              // this.WindowState = (System.Windows.Forms.FormWindowState) (2); / / after recording the initial position and size of the control, and then maximize
             // 0 - the Normalize,. 1 - the Minimize, the maximize 2- 
        }
         Private  void the AddControl (control CTL) 
        { 
            the foreach (control C in ctl.Controls) 
            {   // ** on here, is the first recording sub-control control, the recording control itself
               // IF (c.Controls.Count> 0)
               //     the AddControl (C); // the rest of the window is also possible to control in vivo nested controls (such as panel ), to extract alone, due to the recursive call
                objCtrl controlRect; 
                objCtrl.Left = c.Left; objCtrl.Top = c.Top; objCtrl.Width = c.Width; objCtrl.Height = c.Height; 
                oldCtrl.Add (objCtrl); 
                // ** on here, a first recording control itself, the child controls the recording control 
                IF (c.Controls.Count> 0 ) 
                    the AddControl (C); // the rest of the window is also possible to control in vivo nested controls (such as Panel), to separate out, due to the recursively 
            } 
        } 
        // (3.2) the size of adaptive control, 
        public  void controlAutoSize (the mForm control) 
        { 
            IF (== ctrlNo 0 ) 
            { //* If the Form1_Load the form, record the original size and position controls, normal no problem, but to join the skin problem arises because some controls such as dataGridView of child controls is not yet complete, a small number of
               // * To Form1_SizeChanged in the form, the size of the first time change, the recording original size and position control, where the child controls all controls have been formed 
                controlRect cR;
               //   cR.Left = mForm.Left; = cR.Top the 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 "form itself", only added once 
                the AddControl (the mForm); // the rest of the window may be nested within other controls controls (such as Panel), so that it is separately withdrawn recursive call 
            }
             float= wScale ( a float ) mForm.Width / ( a float ) oldCtrl [ 0 ] .Width; // the ratio between the old and the new form, and the first old form 
            a float hscale = ( a float ) mForm.Height / ( a float ) oldCtrl [ 0 ] .Height; // .Height; 
            ctrlNo = 1 ; // enter = 1, 0 is the form itself, the body of the window control, a serial number beginning from 
            AutoScaleControl (the mForm, wScale, hscale); // window remaining nested controls may control in vivo (Panel for example), to separate out, due to the recursive call 
        }
         Private  void AutoScaleControl (control CTL, a float wScale, a float hscale) 
        {
            int ctrLeft0, ctrTop0, ctrWidth0, ctrHeight0;
             // int ctrlNo = 1; // 1st form itself is Left, Top, Width, Height, form controls so ctrlNo = 1 starts from 
            the foreach (Control C in CTL. controls) 
            { // ** on here, is the first child controls the zoom control, the control itself scaled
                  // IF (c.Controls.Count> 0)
                  //    AutoScaleControl (C, wScale, hscale); // form the remaining controls may also be nested controls (such as Panel), to separate out, 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 old and new control
                 // c.Top = (int) (( ctrTop0 - wTop0) * H) + wTop1; 
                c.Left = ( int ) ((ctrLeft0) * wScale); // linear proportion between old and new controls. Position with respect to the form control only, it can not add wLeft1 + 
                c.Top = ( int ) ((ctrTop0) * hscale); //
                 c.Width = ( int ) (* ctrWidth0 wScale); // only the size of the original related, and can not now multiplied by the width (int) (* c.Width W); 
                c.Height = ( int ) (* ctrHeight0 hscale); //
                 ctrlNo ++;// accumulated number
                 // ** on here, is the first scaling control itself, the child controls scaled 
                IF (c.Controls.Count> 0 ) 
                    AutoScaleControl (C, wScale, hscale); // Window control body to rest may also be nested controls (such as Panel), to separate out, due to the recursive call 

                IF (CTL IS the DataGridView) 
                { 
                    the DataGridView DGV = CTL aS the 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;    // cell columns calculating the adjusted width of a column and                        
                    }
                     IF (wIDTHS> = ctl.Size.Width)   // if the column width is larger than the set adjustment column width   
                        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;   // adjust the column automatic mode   
                    the else 
                        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;   / / is less than the filling  
 
                    Cursor.Current = Cursors.Default;
                }
            }
        }
    }
View Code

3. adaptive in the form

= Asc AutoSizeForm new new AutoSizeForm (); // create an adaptive class of objects 
asc.controllInitializeSize ( the this ); // at load Load function 
asc.controlAutoSize ( the this ); // write SizeChanged From the event
View Code

According to the above three operations can be realized adaptively C # WinFrom.

 

Guess you like

Origin www.cnblogs.com/cdjbolg/p/11736104.html