The size of the winform control adapts to the form

3 methods:

#region Change control size
        //Get the original information of the control
        protected void GetAllInitInfo(Control ctrlContainer)
        {
            //int tempWidth = Screen.PrimaryScreen.Bounds.Width / 5 * 4;
            //int tempHeight = Screen.PrimaryScreen.Bounds.Height / 5 * 4;
            if (ctrlContainer.Parent == this)//Get the height and width of the form
            {
                formWidth = Convert.ToDouble(ctrlContainer.Width);
                formHeight = Convert.ToDouble(ctrlContainer.Height);
            }
            foreach (Control item in ctrlContainer.Controls)
            {
                if (item.Name.Trim() != "")
                {
                    //Add information: key value: control name, content: according to the left distance, distance from the top, control width, control height, control font.
                    ControlsInfo.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2) + "," + item.Width + "," + item.Height + "," + item.Font.Size);
                }
                if ((item as UserControl) == null && item.Controls.Count > 0)
                {
                    GetAllInitInfo (item);
                }
            }
        }

        //Get the form zoom ratio
        private void ControlsChangeInit(Control ctrlContainer)
        {
            //scaleX = (double)4 / 5;
            //scaleY = (double)4 / 5;
            scaleX = (Convert.ToDouble(ctrlContainer.Width) / formWidth);
            scaleY = (Convert.ToDouble(ctrlContainer.Height) / formHeight);
        }

        //Modify the size of the control when the form changes
        private void ControlsChange(Control ctrlContainer)
        {
            double[] pos = new double[5];//The pos array saves the current control center Left, Top, Control Width, Control Height, Control Font Size

            foreach (Control item in ctrlContainer.Controls)//traverse controls
            {
                if (item.Name.Trim() != "")//If the control name is not empty, execute
                {
                    if ((item as UserControl) == null && item.Controls.Count > 0)//If it is not a custom control
                    {
                        ControlsChange(item);//loop execution
                    }
                    string[] strs = ControlsInfo[item.Name].Split(',');//The data found in the dictionary is divided into string groups by ','

                    for (int i = 0; i < 5; i++)
                    {
                        pos[i] = Convert.ToDouble(strs[i]);//Add to temporary array
                    }
                    double itemWidth = pos[2] * scaleX; //Calculate the width of the control, double type
                    double itemHeight = pos[3] * scaleY; //Calculate the height of the control
                    item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);//Calculate the distance from the control to the left
                    item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);//Calculate the distance from the top of the control
                    item.Width = Convert.ToInt32(itemWidth);//Control width, int type
                    item.Height = Convert.ToInt32(itemHeight);//Control height
                    item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));//字体
                }
            }
        }

        private void FormNewInfraredPicture_SizeChanged(object sender, EventArgs e)
        {
            if (sizeBool2)
            {
                if (ControlsInfo.Count > 0)//If there is data in the dictionary, that is, the form changes
                {
                    ControlsChangeInit(this.Controls[0]);//Indicates the pannel control
                    ControlsChange(this.Controls[0]);
                }
            }
            //if (ControlsInfo.Count > 0)//If there is data in the dictionary, that is, the form changes
            //{
            // ControlsChangeInit(this.Controls[0]);//Indicates the pannel control
            //    ControlsChange(this.Controls[0]);
            //}
        }
        #endregion

 Event calls and constructors start recording control information.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324796539&siteId=291194637