icp网站 是什么意思全网营销一站式推广
问题
当屏幕分辨率提高或降低时,原分辨率显示正常的控件,将变得很小或很大,字体也变得太大或太小。
解决办法
当分辨率变化时,采用递归的方法,对所有的控件放大或缩小。
public static void MainForm_Load(object sender, EventArgs e){// 获取当前屏幕分辨率Screen screen = Screen.PrimaryScreen;int currentWidth = screen.Bounds.Width;int currentHeight = screen.Bounds.Height;Control t =(Control) sender;if (t.Width > 1700|| t.Width<1000){// 计算宽度和高度的缩放因子float widthScale = (float)currentWidth / 1920;float heightScale = (float)currentHeight / 1080;// 应用缩放因子ScaleControls((Control)sender, widthScale, heightScale);}}public static void ScaleControls(Control parentControl, float widthScale, float heightScale){foreach (Control control in parentControl.Controls){// 调整控件的大小和位置control.Left = (int)(control.Left * widthScale);control.Top = (int)(control.Top * heightScale);control.Width = (int)(control.Width * widthScale);control.Height = (int)(control.Height * heightScale);if (control.Width > 1700){// 调整字体大小control.Font = new Font(control.Font.FontFamily, control.Font.Size * Math.Min(widthScale, heightScale));}elsecontrol.Font = new Font("宋体", 9);// 递归处理子控件if (control.HasChildren){ScaleControls(control, widthScale, heightScale);}}