C # Winform controls adaptive form size

Demand: When the window size dynamically changes, the form of the various controls (including the Panel Panel and a sub-control) may dynamically adjust its size to accommodate Forms content ratio.

method:

The first step, a new category, as follows:

class Resize
    {
        private Form _form;

        public Resize(Form form)
        {
            int count = form.Controls.Count * 2 + 2;
            float[] factor = new float[count];
            int i = 0;
            factor[i++] = form.Size.Width;
            factor[i++] = form.Size.Height;
            foreach (Control ctrl in form.Controls)
            {
                factor[i++] = ctrl.Location.X / (float)form.Size.Width;
                factor[i++] = ctrl.Location.Y / (float)form.Size.Height;
                ctrl.Tag = ctrl.Size;
            }
            form.Tag = factor;
            this._form = form;
        }

        public void Form1_Resize(object sender, EventArgs e)
        {
            float[] scale = (float[])this._form.Tag;
            int i = 2;
            foreach (Control ctrl in the this ._form.Controls) // length and width of the panel to increase a fixed value no longer increased, reasons: Panel width and height of an upper limit of 65535 pixels ( https://blog.csdn.net/dufangfeilong/article ? / Details / 41,805,073 = utm_source blogxgwz5 ) 
            { 
                ctrl.Left = ( int ) ( the this ._form.Size.Width * Scale [I ++ ]); 
                ctrl.Top = ( int ) ( the this ._form.Size.Height * Scale [ ++ I ]); 
                ctrl.Width = ( int ) ( the this ._form.Size.Width / ( a float ) Scale [ 0 ] *  ((Size) ctrl.Tag) .Width);
                ctrl.Height = (int)(this._form.Size.Height / (float)scale[1] * ((Size)ctrl.Tag).Height);
            }
        }
    }

The second step, using the class initialization function Form:

 public Form_StockCount () 
        { 
            the InitializeComponent (); 

             the this .SizeChanged + = new new the Resize ( the this ) .Form1_Resize;   // window adaptive codebook 
         }

 

Guess you like

Origin www.cnblogs.com/PER10/p/11541568.html