using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Linq; using System.Text; using System.Windows.Forms; namespace Lskj.Main.Model { class AutoSizeChange { private static double _FormHeight = (double)(Screen.PrimaryScreen.Bounds.Height) * 1.2 / (double)1080; private static double _FormWidth = (double)(Screen.PrimaryScreen.Bounds.Width) * 1.2 / (double)1920; public static void ControllInitializeSize(System.Windows.Forms.Control Control) { foreach (System.Windows.Forms.Control ctrol in Control.Controls) { if (ctrol.Controls.Count > 0) { if (ctrol is Panel || ctrol is DevExpress.XtraEditors.PanelControl || ctrol is UserControl) { ctrol.Width = Convert.ToInt32(ctrol.Width * _FormWidth); ctrol.Height = Convert.ToInt32(ctrol.Height * _FormHeight); ctrol.Location = new Point(Convert.ToInt32(ctrol.Location.X * _FormWidth), Convert.ToInt32(ctrol.Location.Y * _FormHeight)); //ctrol.Font = new Font(ctrol.Font.OriginalFontName, Convert.ToSingle(ctrol.Font.Size * _FormHeight * _FormWidth * 1.3)); } if (ctrol is Lskj.Control.AutoGridLookUp) { ctrol.Width = Convert.ToInt32(ctrol.Width * _FormWidth); ctrol.Height = Convert.ToInt32(ctrol.Height * _FormHeight); ctrol.Location = new Point(Convert.ToInt32(ctrol.Location.X * _FormWidth), Convert.ToInt32(ctrol.Location.Y * _FormHeight)); ctrol.Font = new Font(ctrol.Font.OriginalFontName, Convert.ToSingle(ctrol.Font.SizeInPoints * _FormHeight * _FormWidth)); } ControllInitializeSize(ctrol); } else { SizeChange(ctrol); } } } public static void SizeChange(System.Windows.Forms.Control Control) { if (Control.BackgroundImage != null) { Image image = Control.BackgroundImage; Size size = new Size(Convert.ToInt32(Control.BackgroundImage.Width * _FormWidth), Convert.ToInt32(Control.BackgroundImage.Height * _FormHeight)); Control.BackgroundImage = resizeImage(image, size); } Control.Width = Convert.ToInt32(Control.Width * _FormWidth); Control.Height = Convert.ToInt32(Control.Height * _FormHeight); Control.Location = new Point(Convert.ToInt32(Control.Location.X * _FormWidth), Convert.ToInt32(Control.Location.Y * _FormHeight)); Control.Font = new Font(Control.Font.OriginalFontName, Convert.ToSingle(Control.Font.SizeInPoints * _FormHeight * _FormWidth)); } private static System.Drawing.Image resizeImage(System.Drawing.Image imgToResize, Size size) { //获取图片宽度 int sourceWidth = imgToResize.Width; //获取图片高度 int sourceHeight = imgToResize.Height; float nPercent = 0; float nPercentW = 0; float nPercentH = 0; //计算宽度的缩放比例 nPercentW = ((float)size.Width / (float)sourceWidth); //计算高度的缩放比例 nPercentH = ((float)size.Height / (float)sourceHeight); if (nPercentH < nPercentW) nPercent = nPercentH; else nPercent = nPercentW; //期望的宽度 int destWidth = (int)(sourceWidth * nPercent); //期望的高度 int destHeight = (int)(sourceHeight * nPercent); Bitmap b = new Bitmap(destWidth, destHeight); Graphics g = Graphics.FromImage((System.Drawing.Image)b); g.InterpolationMode = InterpolationMode.HighQualityBicubic; //绘制图像 g.DrawImage(imgToResize, 0, 0, destWidth, destHeight); g.Dispose(); return (System.Drawing.Image)b; } } }