using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace UAS_MES.PublicMethod { class AutoSizeControl { /// /// 记录所有控件 /// List ctl = new List(); /// /// 记录控件的初始化位置 /// Dictionary CtlPoint = new Dictionary(); /// /// 记录控件的初始化大小 /// Dictionary CtlSize = new Dictionary(); /// /// 初始化的窗体宽度 /// int InitFormWidth = 0; /// /// 初始化的窗体高度 /// int InitFormHeight = 0; /// /// 窗体调整时的宽度 /// int AutoSizeFormWidth = 0; /// /// 窗体调整时的高度 /// int AutoSizeFormHeight = 0; /// /// 自适应宽度比例 /// float AutoSizeWidthRate; /// /// /// 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)); } } } }