Winform form control + font adaptive screen size

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace GTVision
{
    public class AutoSizeFormClass
    {
        ///  <summary> 
        /// Declare structure record control position and size
         ///  </summary> 
        public  struct ControlRect
        {
            public int Left;
            public int Top;
            public int Width;
            public int Height;
            public float Size;
        }

        public List<ControlRect> _oldCtrl = new List<ControlRect>();
        private int _ctrlNo = 0;

        ///  <summary> 
        /// Record the initial position and size of the form and its controls
         ///  </summary> 
        ///  <param name="mForm"></param> 
        public  void ControlInitializeSize(Control mForm)
        {
            ControlRect cR;
            cR.Left = mForm.Left;
            cR.Top = mForm.Top;
            cR.Width = mForm.Width;
            cR.Height = mForm.Height;
            cR.Size = mForm.Font.Size;
            _oldCtrl.Add(cR);

            AddControl(mForm);
        }

        private void AddControl(Control ctrl)
        {
            foreach (Control c in ctrl.Controls)
            {
                ControlRect cR;
                cR.Left = c.Left;
                cR.Top = c.Top;
                cR.Width = c.Width;
                cR.Height = c.Height;
                cR.Size = c.Font.Size;
                _oldCtrl.Add(cR);
                // Controls may have nested child controls 
                if (c.Controls.Count > 0 )
                    AddControl(c);
            }
        }

        public void ControlAutoSize(Control mForm)
        {
            if (_oldCtrl.Count > 0)
            {
                _ctrlNo = 1;
                float wScale = (float)mForm.Width / _oldCtrl[0].Width;
                float hScale = (float)mForm.Height / _oldCtrl[0].Height;
                AutoScaleControl (mForm, wScale, hScale);
            }
        }

        private void AutoScaleControl(Control mForm, float wScale, float hScale)
        {
            int ctrlLeft, ctrlTop, ctrlWidth, ctrlHeight;
            float ctrlFontSize, hSize, wSize;
            foreach (Control c in mForm.Controls)
            {
                ctrlLeft = _oldCtrl[_ctrlNo].Left;
                ctrlTop = _oldCtrl[_ctrlNo].Top;
                ctrlWidth = _oldCtrl[_ctrlNo].Width;
                ctrlHeight = _oldCtrl[_ctrlNo].Height;
                ctrlFontSize = _oldCtrl[_ctrlNo].Size;

                c.Left = (int)(ctrlLeft * wScale);
                c.Top = (int)(ctrlTop * hScale);
                c.Width = (int)(ctrlWidth * wScale);
                c.Height = (int)(ctrlHeight * hScale);

                // Take the minimum scale to set the font size 
                hSize = ctrlFontSize * hScale;
                wSize = ctrlFontSize * wScale;
                c.Font = new Font(c.Font.Name, Math.Min(hSize, wSize), c.Font.Style, c.Font.Unit);
                

                _ctrlNo ++ ;

                // First scale the control itself and then scale the child controls 
                if (c.Controls.Count > 0 )
                {
                    AutoScaleControl (c, wScale, hScale);
                }
            }
        }
    }
}

Add to the main form class:

        AutoSizeFormClass asc = new AutoSizeFormClass();
        
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            asc.ControlInitializeSize(this);
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            asc.ControlAutoSize(this);
        }

 

Guess you like

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