1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace UAS_MES_NEW.PublicMethod
- {
- class AutoSizeControl
- {
- /// <summary>
- /// 记录所有控件
- /// </summary>
- List<Control> ctl = new List<Control>();
- /// <summary>
- /// 记录控件的初始化位置
- /// </summary>
- Dictionary<string, Point> CtlPoint = new Dictionary<string, Point>();
- /// <summary>
- /// 记录控件的初始化大小
- /// </summary>
- Dictionary<string, Size> CtlSize = new Dictionary<string, Size>();
- /// <summary>
- /// 初始化的窗体宽度
- /// </summary>
- int InitFormWidth = 0;
- /// <summary>
- /// 初始化的窗体高度
- /// </summary>
- int InitFormHeight = 0;
- /// <summary>
- /// 窗体调整时的宽度
- /// </summary>
- int AutoSizeFormWidth = 0;
- /// <summary>
- /// 窗体调整时的高度
- /// </summary>
- int AutoSizeFormHeight = 0;
- /// <summary>
- /// 自适应宽度比例
- /// </summary>
- float AutoSizeWidthRate;
- /// <summary>
- ///
- /// </summary>
- float AutoSizeHeigthRate;
- public void InitControl(Control control)
- {
- InitFormWidth = control.Width;
- InitFormHeight = control.Height;
- foreach (Control item in control.Controls)
- {
- ctl.Add(item);
- CtlPoint.Add(item.Name, item.Location);
- CtlSize.Add(item.Name, item.Size);
- if (item.Controls.Count > 0)
- {
- AddControl(item);
- }
- }
- }
- public void AddControl(Control control)
- {
- foreach (Control item in control.Controls)
- {
- ctl.Add(item);
- if (!CtlPoint.ContainsKey(item.Name))
- CtlPoint.Add(item.Name, item.Location);
- if (!CtlSize.ContainsKey(item.Name))
- CtlSize.Add(item.Name, item.Size);
- if (item.Controls.Count > 0)
- {
- AddControl(item);
- }
- }
- }
- public void AutoSize(Control control)
- {
- AutoSizeFormHeight = control.Height;
- AutoSizeFormWidth = control.Width;
- AutoSizeWidthRate = (float)(AutoSizeFormWidth * 1.0 / InitFormWidth);
- AutoSizeHeigthRate = (float)(AutoSizeFormHeight * 1.0 / InitFormHeight);
- for (int i = 0; i < ctl.Count; i++)
- {
- ctl[i].Location = new Point((int)(CtlPoint[ctl[i].Name].X * AutoSizeWidthRate), (int)(CtlPoint[ctl[i].Name].Y * AutoSizeHeigthRate));
- ctl[i].Size = new Size((int)(CtlSize[ctl[i].Name].Width * AutoSizeWidthRate), (int)(CtlSize[ctl[i].Name].Height * AutoSizeHeigthRate));
- }
- }
- }
- }
|