C# 窗體縮放的時候同步改變控件的大小和字體


最新在寫個小程序,需要窗體填滿各種尺寸的顯示器,同時需要同步縮放控件的大小。於是就寫了個類,簡單的調用一下即可解決問題。

這個類可以同步縮放控件的位置,寬度高度,字體大小。

 

使用的時候在FormLoad里面綁定一下即可:

    public partial class Form1 : Form
    {
        private ControlResizer Resizer; //定義縮放類

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //綁定
            Resizer=new ControlResizer(this);
        }

    }

 

 

控件縮放類的代碼:

  1     /// <summary>
  2     /// 同步縮放窗體上控件的大小和字體
  3     /// </summary>
  4     public class ControlResizer
  5     {
  6         class ControlPosAndSize
  7         {
  8             public float FrmWidth { get; set; }
  9             public float FrmHeight { get; set; }
 10             public int Left { get; set; }
 11             public int Top { get; set; }
 12             public int Width { get; set; }
 13             public int Height { get; set; }
 14             public float FontSize { get; set; }
 15 
 16         }
 17 
 18         private Form _form;
 19 
 20         //句柄,大小信息
 21         private Dictionary<int, ControlPosAndSize> _dic = new Dictionary<int, ControlPosAndSize>();
 22         public ControlResizer(Form form)
 23         {
 24             _form = form;
 25             _form.Resize += _form_Resize;//綁定窗體大小改變事件
 26 
 27             _form.ControlAdded += form_ControlAdded;  //窗體上新增控件的處理
 28             _form.ControlRemoved += form_ControlRemoved;
 29 
 30             SnapControlSize(_form);//記錄控件和窗體大小
 31         }
 32 
 33         void form_ControlRemoved(object sender, ControlEventArgs e)
 34         {
 35             var key = e.Control.Handle.ToInt32();
 36             _dic.Remove(key);
 37         }
 38 
 39         //綁定控件添加事件
 40         private void form_ControlAdded(object sender, ControlEventArgs e)
 41         {
 42             var ctl = e.Control;
 43             var ps = new ControlPosAndSize
 44             {
 45                 FrmHeight = _form.Height,
 46                 FrmWidth = _form.Width,
 47                 Width = ctl.Width,
 48                 Height = ctl.Height,
 49                 Left = ctl.Left,
 50                 Top = ctl.Top,
 51                 FontSize = ctl.Font.Size
 52             };
 53             var key = ctl.Handle.ToInt32();
 54             _dic[key] = ps;
 55         }
 56 
 57         void _form_Resize(object sender, EventArgs e)
 58         {
 59             ResizeControl(_form);
 60         }
 61 
 62         private void ResizeControl(Control control)
 63         {
 64             foreach (Control ctl in control.Controls)
 65             {
 66                 var key = ctl.Handle.ToInt32();
 67                 if (_dic.ContainsKey(key))
 68                 {
 69                     var ps = _dic[key];
 70                     var newx = _form.Width / ps.FrmWidth;
 71                     var newy = _form.Height / ps.FrmHeight;
 72 
 73                     ctl.Top = (int)(ps.Top * newy);
 74                     ctl.Height = (int)(ps.Height * newy);
 75 
 76                     ctl.Left = (int)(ps.Left * newx);
 77                     ctl.Width = (int)(ps.Width * newx);
 78 
 79                     ctl.Font = new Font(ctl.Font.Name, ps.FontSize * newy, ctl.Font.Style, ctl.Font.Unit);
 80 
 81                     if (ctl.Controls.Count > 0)
 82                     {
 83                         ResizeControl(ctl);
 84                     }
 85 
 86                 }
 87 
 88             }
 89         }
 90 
 91         /// <summary>
 92         /// 創建控件的大小快照,參數為需要記錄大小控件的 容器
 93         /// </summary>
 94         private void SnapControlSize(Control control)
 95         {
 96             foreach (Control ctl in control.Controls)
 97             {
 98                 var ps = new ControlPosAndSize
 99                 {
100                     FrmHeight = _form.Height,
101                     FrmWidth = _form.Width,
102                     Width = ctl.Width,
103                     Height = ctl.Height,
104                     Left = ctl.Left,
105                     Top = ctl.Top,
106                     FontSize = ctl.Font.Size
107                 };
108 
109                 var key = ctl.Handle.ToInt32();
110 
111                 _dic[key] = ps;
112 
113                 //綁定添加事件
114                 ctl.ControlAdded += form_ControlAdded;
115                 ctl.ControlRemoved += form_ControlRemoved;
116 
117                 if (ctl.Controls.Count > 0)
118                 {
119                     SnapControlSize(ctl);
120                 }
121 
122             }
123 
124         }
125 
126     }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM