1. 首先在窗體上放上一個Panel容器,並將容器的Dock屬性設為Fill,即所有的控件都放在了這個容器里。
using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApp2 { class AutoAdaptWindowsSize { double formOriginalWidth;//窗體原始寬度 double formOriginalHeight;//窗體原始高度 double scaleX;//水平縮放比例 double scaleY;//垂直縮放比例 Dictionary<string, string> ControlsInfo = new Dictionary<string, string>();//控件中心Left,Top,控件Width,控件Height,控件字體Size private Form _form; public AutoAdaptWindowsSize(Form form) { _form = form; } /// <summary> /// /// </summary> /// <param name="ctrlContainer">panel 控件</param> public void InitControlsInfo(Control ctrlContainer) { if (ctrlContainer.Parent == _form)//獲取窗體的高度和寬度 { formOriginalWidth = Convert.ToDouble(ctrlContainer.Width); formOriginalHeight = Convert.ToDouble(ctrlContainer.Height); } foreach (Control item in ctrlContainer.Controls) { if (item.Name.Trim() != "") { //添加信息:鍵值:控件名,內容:據左邊距離,距頂部距離,控件寬度,控件高度,控件字體。 ControlsInfo.Add(item.Name, (item.Left + item.Width / 2) + "," + (item.Top + item.Height / 2) + "," + item.Width + "," + item.Height + "," + item.Font.Size); } if ((item as UserControl) == null && item.Controls.Count > 0) { InitControlsInfo(item); } } } public void FormSizeChanged() { if (ControlsInfo.Count > 0)//如果字典中有數據,即窗體改變 { ControlsZoomScale(_form.Controls[0]);//表示pannel控件 ControlsChange(_form.Controls[0]); } } private void ControlsZoomScale(Control ctrlContainer) { scaleX = (Convert.ToDouble(ctrlContainer.Width) / formOriginalWidth); scaleY = (Convert.ToDouble(ctrlContainer.Height) / formOriginalHeight); } /// <summary> /// 改變控件大小 /// </summary> /// <param name="ctrlContainer"></param> private void ControlsChange(Control ctrlContainer) { double[] pos = new double[5];//pos數組保存當前控件中心Left,Top,控件Width,控件Height,控件字體Size foreach (Control item in ctrlContainer.Controls)//遍歷控件 { if (item.Name.Trim() != "")//如果控件名不是空,則執行 { if ((item as UserControl) == null && item.Controls.Count > 0)//如果不是自定義控件 { ControlsChange(item);//循環執行 } string[] strs = ControlsInfo[item.Name].Split(',');//從字典中查出的數據,以‘,’分割成字符串組 for (int i = 0; i < 5; i++) { pos[i] = Convert.ToDouble(strs[i]);//添加到臨時數組 } double itemWidth = pos[2] * scaleX; //計算控件寬度,double類型 double itemHeight = pos[3] * scaleY; //計算控件高度 item.Left = Convert.ToInt32(pos[0] * scaleX - itemWidth / 2);//計算控件距離左邊距離 item.Top = Convert.ToInt32(pos[1] * scaleY - itemHeight / 2);//計算控件距離頂部距離 item.Width = Convert.ToInt32(itemWidth);//控件寬度,int類型 item.Height = Convert.ToInt32(itemHeight);//控件高度 item.Font = new Font(item.Font.Name, float.Parse((pos[4] * Math.Min(scaleX, scaleY)).ToString()));//字體 } } } } }
使用:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class Form1 : Form { AutoAdaptWindowsSize autoAdaptSize; public Form1() { InitializeComponent(); #region 窗體縮放 autoAdaptSize = new AutoAdaptWindowsSize(this); autoAdaptSize.InitControlsInfo(this.Controls[0]); #endregion } private void Form1_Load(object sender, EventArgs e) { } #region 窗體縮放 private void Form1_SizeChanged(object sender, EventArgs e) { if (autoAdaptSize != null) { autoAdaptSize.FormSizeChanged(); } } #endregion } }
