• 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     }
  • 相关阅读:
    加班的价值
    webApp 阅读器项目实践
    Oak Seeds 网站项目回顾
    [Echarts]用Echarts绘制饼状图
    [转载] 编程每一天(Write Code Every Day)
    对杀毒软件技术的浅浅理解
    记我的第二次自动化尝试——selenium+pageobject+pagefactory实现自动化下单、退款、撤销回归测试
    学习Selenium遇到的问题和解决方案
    记我的第一次自动化尝试
    jmeter环境配置、使用以及参数化之CSV Data Set Config
  • 原文地址:https://www.cnblogs.com/ssboy/p/4963546.html
Copyright © 2020-2023  润新知