winfrom adaptive form

the using the System.Runtime.InteropServices; 

public  class the Win32 
        { 
            public  const Int32 AW_HOR_POSITIVE = 0x00000001 ;     // left to right, the window opens 
            public  const Int32 AW_HOR_NEGATIVE = 0x00000002 ;     // right to left open window 
            public  const Int32 AW_VER_POSITIVE = 0x00000004 ;     // window open from top to bottom 
            public  const Int32 AW_VER_NEGATIVE = 0x00000008 ;     // open the window from the lower to the 
            public  const Int32 AW_CENTER = 0x00000010;
             Public  const Int32 AW_HIDE = 0x00010000 ;         // To use this function must add this constant when the form is unloaded 
            public  const Int32 AW_ACTIVATE = 0x00020000 ;     // After the form is opened by this function, lose focus by default unless present together with a constant 
            public  const Int32 AW_SLIDE = 0x00040000 ;
             public  const Int32 AW_BLEND = 0x00080000 ;        // fade effect 
            [the DllImport ( " User32.dll " , the CharSet = CharSet.Auto)]
             public  static  extern  BOOL AnimateWindow(
            IntPtr hwnd, // handle to window    
            int dwTime, // duration of animation    
            int dwFlags // animation type    
            );
        }

/*淡入窗体*/
private void Form_Load(object sender, EventArgs e)
{
        Win32.AnimateWindow(this.Handle, 2000, Win32.AW_BLEND);
}
/*淡出窗体*/
private void Form_FormClosing(object sender, FormClosingEventArgs e)
{
    Win32.AnimateWindow ( This .Handle, 2000 , Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND); 
} 
// Https://blog.csdn.net/a237428367/article/details/5933565
 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 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 new List < 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 control, 
        public  void controllInitializeSize (the mForm Control) 
        { 
            controlRect cR; 
            cR.Left = mForm.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height; 
            oldCtrl.Add (cR); // The first is "form itself" adding only once 
            the AddControl (mForm); // rest of the body window controls may also be nested controls (such as panel), to extract alone, 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) 
            {   // ** discharge here, a 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 separate out, 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 the recording control 
                IF (c.Controls.Count> 0 ) 
                    the AddControl (C); // the rest of the body control window may nested controls (such as panel ), to separate out, due to the recursive call 
            } 
        } 
        // adaptive size (3.2) control, 
        public  void controlAutoSize (the mForm control) 
        { 
            IF (== ctrlNo 0 ) 
            { // * If Form1_Load form, the recording control of the original size and position, 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 the form, for the first time and changing the size, the recording original size and position control, where the child controls all controls have been formed
                cR controlRect;
                 //  = mForm.Left cR.Left; cR.Top = mForm.Top; cR.Width = mForm.Width; cR.Height = mForm.Height; 
                cR.Left = 0 ; cR.Top = 0 ; = cR.Width the mForm. PreferredSize.Width; cR.Height = mForm.PreferredSize.Height; 

                oldCtrl.Add (cR); // The first is "the form itself", adding only once 
                the AddControl (mForm); // rest of the body control window other controls may be nested (such as Panel), so in order to separate out recursively 
            }
             a float wScale = ( a float ) mForm.Width / ( a float ) oldCtrl [ 0 ] .Width; // between the new and form a ratio with the first old forms 
            float hscale = ( 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 (mForm, wScale, hScale) ; // 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 
        }
         Private  void AutoScaleControl (control CTL, a float wScale, a 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 the start ctrlNo = 1 
            foreach (control c inctl.Controls) 
            { // ** on here, is the first child controls the zoom control, the control itself scaled
               // IF (c.Controls.Count> 0)
               //    AutoScaleControl (C, wScale, hscale); // window control body may rest 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 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 zoom control itself, after scaling of child controls control 
                IF (c.Controls.Count> 0 )
                    AutoScaleControl (C, wScale, hscale); // 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 

                IF (CTL IS the DataGridView) 
                { 
                    the DataGridView DGV = CTL AS the DataGridView; 
                    the 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;    // width of the cell row and column calculating the adjusted                        
                    }
                     IF (WIDTHS> = ctl.Size.Width)   // if the column width is greater than the set adjustment column width   
                        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;   // adjust the column automatic mode   
                    the else 
                        dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;   // if the filling is less than   

                    Cursor.Current = Cursors.Default; 
                } 
            } 


        } 
    }
  private readonly AutoSizeFormClass asc = new AutoSizeFormClass();
#region Override Functions
        /// <summary>
        /// OnLoad
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            DataBinding();

            asc.controllInitializeSize(this);
            SizeChanged += _SizeChanged;
            int x = (SystemInformation.WorkingArea.Width - Size.Width) / 2;
             Int Y = (SystemInformation.WorkingArea.Height - Size.Height) / 2 ; 
            the StartPosition = FormStartPosition.Manual; // position determined by the form of the attribute Location 
            Location = (Point) new new Size (X, Y);          // Window is the starting position of the object (X, Y) 
        }
         Private  void _SizeChanged ( Object SENDER, EventArgs E) 
        { 
            asc.controlAutoSize ( the this ); 
        } 
        ///  <Summary> 
        /// OnClosed
         ///  </ Summary> 
        // /  <param name = "E"> </ param>
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
        }

 

Guess you like

Origin www.cnblogs.com/WeiYongZhi/p/10948046.html