|
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
|
using System.Drawing.Drawing2D;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
|
|
namespace GeoSigma.SigmaDrawerUtil
|
|
|
|
|
|
{
|
|
|
|
|
|
public class AutoResizeForm
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取windows缩放比
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>缩放比例</returns>
|
|
|
|
|
|
public static float GetWindowsZoomRatio(Graphics graphics)
|
|
|
|
|
|
{
|
|
|
|
|
|
float dpiX = graphics.DpiX;
|
|
|
|
|
|
|
|
|
|
|
|
float zoomRatio = 1.0f;
|
|
|
|
|
|
|
|
|
|
|
|
switch (dpiX)
|
|
|
|
|
|
{
|
|
|
|
|
|
case 96f:
|
|
|
|
|
|
zoomRatio = 1.0f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 120f:
|
|
|
|
|
|
zoomRatio = 1.25f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 144f:
|
|
|
|
|
|
zoomRatio = 1.5f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 168f:
|
|
|
|
|
|
zoomRatio = 1.75f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 192f:
|
|
|
|
|
|
zoomRatio = 2.0f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 216f:
|
|
|
|
|
|
zoomRatio = 2.25f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 240f:
|
|
|
|
|
|
zoomRatio = 2.5f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 264f:
|
|
|
|
|
|
zoomRatio = 2.75f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 288f:
|
|
|
|
|
|
zoomRatio = 3.0f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 312f:
|
|
|
|
|
|
zoomRatio = 3.25f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 336f:
|
|
|
|
|
|
zoomRatio = 3.5f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 360f:
|
|
|
|
|
|
zoomRatio = 3.75f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 384f:
|
|
|
|
|
|
zoomRatio = 4.0f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 408f:
|
|
|
|
|
|
zoomRatio = 4.25f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 432f:
|
|
|
|
|
|
zoomRatio = 4.5f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 456f:
|
|
|
|
|
|
zoomRatio = 4.75f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
case 480f:
|
|
|
|
|
|
zoomRatio = 5.0f;
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return zoomRatio;
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void ChangeUIByDpi(Control control, float dpixRatio)
|
|
|
|
|
|
{
|
|
|
|
|
|
control.SuspendLayout();
|
|
|
|
|
|
foreach (Control ctrl in control.Controls)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ctrl is Panel)
|
|
|
|
|
|
{
|
|
|
|
|
|
ChangeUIByDpi((Panel)control, dpixRatio);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (ctrl is TreeView treeView)
|
|
|
|
|
|
{
|
|
|
|
|
|
treeView.ItemHeight = (int)(treeView.ItemHeight * dpixRatio);
|
|
|
|
|
|
//treeView.Indent = (int)(treeView.Indent * dpixRatio);
|
|
|
|
|
|
//ImageList imgs = new ImageList();
|
|
|
|
|
|
//imgs.ImageSize = new Size(
|
|
|
|
|
|
// (int)(treeView.ImageList.ImageSize.Width * dpixRatio)
|
|
|
|
|
|
// , (int)(treeView.ImageList.ImageSize.Height * dpixRatio));
|
|
|
|
|
|
//foreach (Image img in treeView.ImageList.Images)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// Bitmap enlargedImage = new Bitmap(img.Width * 2, img.Height * 2);
|
|
|
|
|
|
// using (Graphics g = Graphics.FromImage(enlargedImage))
|
|
|
|
|
|
// {
|
|
|
|
|
|
// g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
|
|
|
|
|
// g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
|
|
|
|
|
|
// }
|
|
|
|
|
|
// imgs.Images.Add(enlargedImage);
|
|
|
|
|
|
//}
|
|
|
|
|
|
//treeView.ImageList = imgs;
|
|
|
|
|
|
//treeView.ImageList.ImageSize = new Size(
|
|
|
|
|
|
// (int)(treeView.ImageList.ImageSize.Width * dpixRatio)
|
|
|
|
|
|
// , (int)(treeView.ImageList.ImageSize.Height * dpixRatio));
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (ctrl is ToolStrip toolStrip)
|
|
|
|
|
|
{
|
|
|
|
|
|
toolStrip.Size = new Size(toolStrip.Size.Width
|
|
|
|
|
|
, (int)(toolStrip.Size.Height * dpixRatio));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
control.ResumeLayout();
|
|
|
|
|
|
}
|
|
|
|
|
|
public static void ChangeUIFontByDpi(Control control, float dpixRatio)
|
|
|
|
|
|
{
|
|
|
|
|
|
control.SuspendLayout();
|
|
|
|
|
|
foreach (Control ctrl in control.Controls)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
//else
|
|
|
|
|
|
{
|
|
|
|
|
|
float fsize = (float)(ctrl.Font.Size * (float)dpixRatio);
|
|
|
|
|
|
if (dpixRatio == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
fsize = ctrl.Font.Size;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ctrl.Font = new Font(ctrl.Font.Name, fsize, ctrl.Font.Style);
|
|
|
|
|
|
//if (bNonZoomsys) // 非缩放状态,要自己定义窗口和控件大小
|
|
|
|
|
|
//{
|
|
|
|
|
|
if (ctrl.Anchor != AnchorStyles.None)
|
|
|
|
|
|
{
|
|
|
|
|
|
ctrl.Bounds = new Rectangle((int)(ctrl.Location.X), (int)(ctrl.Location.Y),
|
|
|
|
|
|
(int)(ctrl.Width * dpixRatio), (int)(ctrl.Height * dpixRatio));
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
ctrl.Bounds = new Rectangle((int)(ctrl.Location.X * dpixRatio), (int)(ctrl.Location.Y * dpixRatio),
|
|
|
|
|
|
(int)(ctrl.Width * dpixRatio), (int)(ctrl.Height * dpixRatio));
|
|
|
|
|
|
}
|
|
|
|
|
|
//}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (ctrl.Controls.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
ChangeUIFontByDpi((Panel)control, dpixRatio);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
control.ResumeLayout();
|
|
|
|
|
|
}
|
|
|
|
|
|
public static Image ImageBicubicResize(Image sourceImage, float ratio)
|
|
|
|
|
|
{
|
|
|
|
|
|
int nNewWidth = (int)(sourceImage.Size.Width * ratio);
|
|
|
|
|
|
int nNewHeight = (int)(sourceImage.Size.Height * ratio);
|
|
|
|
|
|
Bitmap resizedImage = new Bitmap(nNewWidth, nNewHeight);
|
|
|
|
|
|
|
|
|
|
|
|
using (Graphics graphics = Graphics.FromImage(resizedImage))
|
|
|
|
|
|
{
|
|
|
|
|
|
graphics.InterpolationMode = InterpolationMode.Bicubic;
|
|
|
|
|
|
graphics.DrawImage(sourceImage, 0, 0, nNewWidth, nNewHeight);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return resizedImage;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void AutoScaleControl(Control ctl, float ratio)
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (Control c in ctl.Controls)
|
|
|
|
|
|
{
|
|
|
|
|
|
c.Bounds = new Rectangle((int)(c.Location.X * ratio), (int)(c.Location.Y * ratio),
|
|
|
|
|
|
(int)(c.Width * ratio), (int)(c.Height * ratio));
|
|
|
|
|
|
// 设置字体
|
|
|
|
|
|
//c.Font = new Font(c.Font.Name, (int)(c.Font.Size * ratio), c.Font.Style);
|
|
|
|
|
|
//}
|
|
|
|
|
|
//c.Location = new Point((int)((ctrLeft0) * ratio), (int)((ctrTop0) * ratio));
|
|
|
|
|
|
//c.Width = (int)(ctrWidth0 * ratio); // 只与最初的大小相关,所以不能与现在的宽度相乘 (int)(c.Width * w);
|
|
|
|
|
|
//c.Height = (int)(ctrHeight0 * ratio); //
|
|
|
|
|
|
//**放在这里,是先缩放控件本身,后缩放控件的子控件
|
|
|
|
|
|
if (c.Controls.Count > 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
AutoScaleControl(c, ratio); // 窗体内其余控件还可能嵌套控件(比如panel),要单独抽出,因为要递归调用
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (ctl is DataGridView)
|
|
|
|
|
|
{
|
|
|
|
|
|
DataGridView dgv = ctl as DataGridView;
|
|
|
|
|
|
Cursor.Current = Cursors.WaitCursor;
|
|
|
|
|
|
|
|
|
|
|
|
int widths = 0;
|
|
|
|
|
|
for (int i = 0; i < dgv.Columns.Count; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
dgv.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCells); // 自动调整列宽
|
|
|
|
|
|
widths += dgv.Columns[i].Width; // 计算调整列后单元列的宽度和
|
|
|
|
|
|
}
|
|
|
|
|
|
if (widths >= ctl.Size.Width) // 如果调整列的宽度大于设定列宽
|
|
|
|
|
|
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; // 调整列的模式 自动
|
|
|
|
|
|
else
|
|
|
|
|
|
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; // 如果小于 则填充
|
|
|
|
|
|
|
|
|
|
|
|
Cursor.Current = Cursors.Default;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|