方法一 调用窗体缩放类库
1. 首先在窗体上放上一个Panel容器(置于底层),并将容器的Dock属性设为Fill,即所有的控件都放在了这个容器里。
2 .窗体新建时调用类库 classAutoAdaptWindowsSize;
用字典记录这个窗体的控件信息InitControlsInfo,FormSizeChanged中调用
if (autoAdaptSize != null) { autoAdaptSize.FormSizeChanged(); };
using System; using System.Collections.Generic; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApp1//改成自己的app项目名字 { classAutoAdaptWindowsSize { 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 inctrlContainer.Controls)