From 1ec80d250ca6f316db0904b5e85db69fb98c6af5 Mon Sep 17 00:00:00 2001 From: Sunny Date: Tue, 29 Aug 2023 10:18:11 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E9=87=8D=E6=9E=84=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=AD=97=E4=BD=93=E8=AE=BE=E7=BD=AE=E9=80=BB=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E4=BB=A5=E6=9C=9F=E5=8F=AF=E4=BB=A5=E5=9C=A8=E7=A8=8B=E5=BA=8F?= =?UTF-8?q?=E8=BF=90=E8=A1=8C=E6=97=B6=E8=AE=BE=E7=BD=AE=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=AD=97=E4=BD=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Common/UMessageTip.cs | 5 +-- SunnyUI/Controls/Color/UIColorWheel.cs | 12 +++---- SunnyUI/Controls/Color/UILabelRotate.cs | 12 +++---- SunnyUI/Controls/UICheckBoxGroup.cs | 2 +- SunnyUI/Controls/UIControl.cs | 10 +++--- SunnyUI/Controls/UITabControlMenu.cs | 12 +++---- SunnyUI/Controls/UITableLayoutPanel.cs | 12 +++---- SunnyUI/Controls/UITreeView.cs | 12 +++---- SunnyUI/Controls/UIUserControl.cs | 13 +++---- SunnyUI/Controls/UIVerificationCode.cs | 2 +- SunnyUI/Forms/UIForm.cs | 41 +++++++-------------- SunnyUI/Frames/UIPage.cs | 39 ++++++-------------- SunnyUI/Style/UIDPIScale.cs | 48 +++++++++---------------- SunnyUI/Style/UIStyle.cs | 22 +++--------- SunnyUI/Style/UIStyles.cs | 2 -- 15 files changed, 81 insertions(+), 163 deletions(-) diff --git a/SunnyUI/Common/UMessageTip.cs b/SunnyUI/Common/UMessageTip.cs index 7f7ff2ca..e9c0a365 100644 --- a/SunnyUI/Common/UMessageTip.cs +++ b/SunnyUI/Common/UMessageTip.cs @@ -305,8 +305,6 @@ namespace Sunny.UI return point; } - static Font tmpFont; - /// /// 创建消息窗图像,同时输出内容区,用于外部定位 /// @@ -317,8 +315,7 @@ namespace Sunny.UI var iconBounds = Rectangle.Empty; var textBounds = Rectangle.Empty; Font font = style.TextFont ?? DefaultFont; - if (tmpFont == null || !tmpFont.Size.EqualsFloat(font.DPIScaleFontSize())) - tmpFont = font.DPIScaleFont(); + using Font tmpFont = font.DPIScaleFont(font.Size); if (style.Icon != null) { diff --git a/SunnyUI/Controls/Color/UIColorWheel.cs b/SunnyUI/Controls/Color/UIColorWheel.cs index 9047fbd4..3592626e 100644 --- a/SunnyUI/Controls/Color/UIColorWheel.cs +++ b/SunnyUI/Controls/Color/UIColorWheel.cs @@ -61,16 +61,14 @@ namespace Sunny.UI } - [Browsable(false)] - public bool IsScaled { get; private set; } + private float DefaultFontSize = -1; public void SetDPIScale() { - if (!IsScaled) - { - this.SetDPIScaleFont(); - IsScaled = true; - } + if (DesignMode) return; + if (!UIDPIScale.NeedSetDPIFont()) return; + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + this.SetDPIScaleFont(DefaultFontSize); } public void SetLightness(double lightness) diff --git a/SunnyUI/Controls/Color/UILabelRotate.cs b/SunnyUI/Controls/Color/UILabelRotate.cs index db33049f..0f921a8b 100644 --- a/SunnyUI/Controls/Color/UILabelRotate.cs +++ b/SunnyUI/Controls/Color/UILabelRotate.cs @@ -33,16 +33,14 @@ namespace Sunny.UI } - [Browsable(false)] - public bool IsScaled { get; private set; } + private float DefaultFontSize = -1; public void SetDPIScale() { - if (!IsScaled) - { - this.SetDPIScaleFont(); - IsScaled = true; - } + if (DesignMode) return; + if (!UIDPIScale.NeedSetDPIFont()) return; + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + this.SetDPIScaleFont(DefaultFontSize); } public new string Text diff --git a/SunnyUI/Controls/UICheckBoxGroup.cs b/SunnyUI/Controls/UICheckBoxGroup.cs index e0894266..665298c6 100644 --- a/SunnyUI/Controls/UICheckBoxGroup.cs +++ b/SunnyUI/Controls/UICheckBoxGroup.cs @@ -144,7 +144,7 @@ namespace Sunny.UI box.Parent = this; box.TagString = i.ToString(); box.Style = Style; - box.IsScaled = IsScaled; + //box.IsScaled = IsScaled; box.ValueChanged += Box_ValueChanged; box.Text = Items[i]?.ToString(); box.StyleCustomMode = StyleCustomMode; diff --git a/SunnyUI/Controls/UIControl.cs b/SunnyUI/Controls/UIControl.cs index 38b9bde2..eb941476 100644 --- a/SunnyUI/Controls/UIControl.cs +++ b/SunnyUI/Controls/UIControl.cs @@ -81,13 +81,13 @@ namespace Sunny.UI [Browsable(false), DefaultValue(false)] public bool IsScaled { get; set; } + private float DefaultFontSize = -1; + public virtual void SetDPIScale() { - if (!IsScaled) - { - this.SetDPIScaleFont(); - IsScaled = true; - } + if (!UIDPIScale.NeedSetDPIFont()) return; + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + this.SetDPIScaleFont(DefaultFontSize); } protected void SetStyleFlags(bool supportTransparent = true, bool selectable = true, bool resizeRedraw = false) diff --git a/SunnyUI/Controls/UITabControlMenu.cs b/SunnyUI/Controls/UITabControlMenu.cs index 0a4349fd..8906bba6 100644 --- a/SunnyUI/Controls/UITabControlMenu.cs +++ b/SunnyUI/Controls/UITabControlMenu.cs @@ -84,16 +84,14 @@ namespace Sunny.UI } - [Browsable(false)] - public bool IsScaled { get; private set; } + private float DefaultFontSize = -1; public void SetDPIScale() { - if (!IsScaled) - { - this.SetDPIScaleFont(); - IsScaled = true; - } + if (DesignMode) return; + if (!UIDPIScale.NeedSetDPIFont()) return; + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + this.SetDPIScaleFont(DefaultFontSize); } /// diff --git a/SunnyUI/Controls/UITableLayoutPanel.cs b/SunnyUI/Controls/UITableLayoutPanel.cs index b46d25c8..5c214115 100644 --- a/SunnyUI/Controls/UITableLayoutPanel.cs +++ b/SunnyUI/Controls/UITableLayoutPanel.cs @@ -32,16 +32,14 @@ namespace Sunny.UI Version = UIGlobal.Version; } - [Browsable(false)] - public bool IsScaled { get; private set; } + private float DefaultFontSize = -1; public void SetDPIScale() { - if (!IsScaled) - { - this.SetDPIScaleFont(); - IsScaled = true; - } + if (DesignMode) return; + if (!UIDPIScale.NeedSetDPIFont()) return; + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + this.SetDPIScaleFont(DefaultFontSize); } protected override void OnControlAdded(ControlEventArgs e) diff --git a/SunnyUI/Controls/UITreeView.cs b/SunnyUI/Controls/UITreeView.cs index 8970235f..f0ff0912 100644 --- a/SunnyUI/Controls/UITreeView.cs +++ b/SunnyUI/Controls/UITreeView.cs @@ -918,16 +918,14 @@ namespace Sunny.UI base.DoubleBuffered = true; } - [Browsable(false), DefaultValue(false)] - public bool IsScaled { get; set; } + private float DefaultFontSize = -1; public void SetDPIScale() { - if (!IsScaled) - { - this.SetDPIScaleFont(); - IsScaled = true; - } + if (DesignMode) return; + if (!UIDPIScale.NeedSetDPIFont()) return; + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + this.SetDPIScaleFont(DefaultFontSize); } [DefaultValue(typeof(Color), "155, 200, 255")] diff --git a/SunnyUI/Controls/UIUserControl.cs b/SunnyUI/Controls/UIUserControl.cs index 260b1b6d..d159b2fa 100644 --- a/SunnyUI/Controls/UIUserControl.cs +++ b/SunnyUI/Controls/UIUserControl.cs @@ -82,17 +82,14 @@ namespace Sunny.UI } - [Browsable(false), DefaultValue(false)] - public bool IsScaled { get; set; } + private float DefaultFontSize = -1; - public virtual void SetDPIScale() + public void SetDPIScale() { if (DesignMode) return; - if (!IsScaled) - { - this.SetDPIScaleFont(); - IsScaled = true; - } + if (!UIDPIScale.NeedSetDPIFont()) return; + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + this.SetDPIScaleFont(DefaultFontSize); } protected bool isReadOnly; diff --git a/SunnyUI/Controls/UIVerificationCode.cs b/SunnyUI/Controls/UIVerificationCode.cs index ab735320..efe2f41d 100644 --- a/SunnyUI/Controls/UIVerificationCode.cs +++ b/SunnyUI/Controls/UIVerificationCode.cs @@ -107,7 +107,7 @@ namespace Sunny.UI { byte gdiCharSet = UIStyles.GetGdiCharSet(Font.Name); using Font font = new Font(Font.Name, CodeFontSize, FontStyle.Bold, GraphicsUnit.Point, gdiCharSet); - using Font fontex = font.DPIScaleFont(); + using Font fontex = font.DPIScaleFont(font.Size); Code = code; Size sf = TextRenderer.MeasureText(code, fontex); Bitmap image = new Bitmap((int)sf.Width + 16, Height - 2); diff --git a/SunnyUI/Forms/UIForm.cs b/SunnyUI/Forms/UIForm.cs index 35bde7cc..181231a3 100644 --- a/SunnyUI/Forms/UIForm.cs +++ b/SunnyUI/Forms/UIForm.cs @@ -119,34 +119,23 @@ namespace Sunny.UI [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] public bool ZoomScaleDisabled { get; set; } - [Browsable(false)] - public bool IsScaled { get; private set; } + private float DefaultFontSize = -1; + private float TitleFontSize = -1; + public void SetDPIScale() { if (DesignMode) return; - if (!IsScaled && UIStyles.DPIScale) + if (!UIDPIScale.NeedSetDPIFont()) return; + + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + if (TitleFontSize < 0) TitleFontSize = this.TitleFont.Size; + + this.SetDPIScaleFont(DefaultFontSize); + TitleFont = TitleFont.DPIScaleFont(TitleFontSize); + foreach (var control in this.GetAllDPIScaleControls()) { - this.SetDPIScaleFont(); - - if (UIDPIScale.NeedSetDPIFont()) - { - TitleFont = TitleFont.DPIScaleFont(); - } - - foreach (Control control in this.GetAllDPIScaleControls()) - { - if (control is UIDataGridView dgv) - { - dgv.SetDPIScale(); - } - else - { - control.SetDPIScaleFont(); - } - } - - IsScaled = true; + control.SetDPIScale(); } } @@ -204,12 +193,6 @@ namespace Sunny.UI public event OnZoomScaleChanged ZoomScaleChanged; - public void ResetDPIScale() - { - IsScaled = false; - SetDPIScale(); - } - [DefaultValue(true)] [Description("是否点击标题栏可以移动窗体"), Category("SunnyUI")] public bool Movable { get; set; } = true; diff --git a/SunnyUI/Frames/UIPage.cs b/SunnyUI/Frames/UIPage.cs index 7364e27a..f584fcf5 100644 --- a/SunnyUI/Frames/UIPage.cs +++ b/SunnyUI/Frames/UIPage.cs @@ -267,42 +267,25 @@ namespace Sunny.UI } - [Browsable(false)] - public bool IsScaled { get; private set; } + private float DefaultFontSize = -1; + private float TitleFontSize = -1; public void SetDPIScale() { if (DesignMode) return; - if (!IsScaled) + if (!UIDPIScale.NeedSetDPIFont()) return; + + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + if (TitleFontSize < 0) TitleFontSize = this.TitleFont.Size; + + this.SetDPIScaleFont(DefaultFontSize); + TitleFont = TitleFont.DPIScaleFont(TitleFontSize); + foreach (var control in this.GetAllDPIScaleControls()) { - this.SetDPIScaleFont(); - if (UIDPIScale.NeedSetDPIFont()) - { - this.TitleFont = TitleFont.DPIScaleFont(); - } - - foreach (Control control in this.GetAllDPIScaleControls()) - { - if (control is UIDataGridView dgv) - { - dgv.SetDPIScale(); - } - else - { - control.SetDPIScaleFont(); - } - } - - IsScaled = true; + control.SetDPIScale(); } } - public void ResetDPIScale() - { - IsScaled = false; - SetDPIScale(); - } - public void Render() { SetStyle(UIStyles.Style); diff --git a/SunnyUI/Style/UIDPIScale.cs b/SunnyUI/Style/UIDPIScale.cs index 2bc96bbb..25a32172 100644 --- a/SunnyUI/Style/UIDPIScale.cs +++ b/SunnyUI/Style/UIDPIScale.cs @@ -31,38 +31,24 @@ namespace Sunny.UI public static float DPIScale() { - if (dpiScale > 0) + if (dpiScale < 0) { - return dpiScale; + using Bitmap bmp = new Bitmap(1, 1); + using Graphics g = bmp.Graphics(); + dpiScale = g.DpiX / 96.0f; } - using Bitmap bmp = new Bitmap(1, 1); - using Graphics g = bmp.Graphics(); - dpiScale = g.DpiX / 96.0f; - if (UIStyles.GlobalFont) dpiScale = dpiScale / (UIStyles.GlobalFontScale / 100.0f); - return dpiScale; + return UIStyles.GlobalFont ? dpiScale * 100.0f / UIStyles.GlobalFontScale : dpiScale; } public static bool NeedSetDPIFont() { - return DPIScale() > 1 || UIStyles.GlobalFont; - } - - internal static float DPIScaleFontSize(this Font font) - { - if (UIStyles.DPIScale) - return font.Size / DPIScale(); - else - return font.Size; - } - - internal static Font DPIScaleFont(this Font font) - { - return DPIScaleFont(font, font.Size); + return UIStyles.DPIScale && (DPIScale() > 1 || UIStyles.GlobalFont); } internal static Font DPIScaleFont(this Font font, float fontSize) { + if (fontSize <= 0) return font; if (UIStyles.DPIScale) { if (UIStyles.GlobalFont) @@ -81,25 +67,23 @@ namespace Sunny.UI } } - internal static void SetDPIScaleFont(this Control control) + internal static void SetDPIScaleFont(this Control control, float fontSize) { - if (!UIStyles.DPIScale) return; - if (UIDPIScale.NeedSetDPIFont()) + if (!UIDPIScale.NeedSetDPIFont()) return; + if (control is IStyleInterface ctrl) { - if (control is IStyleInterface ctrl) - { - if (!ctrl.IsScaled) - control.Font = control.Font.DPIScaleFont(); - } + control.Font = SetDPIScaleFont(control.Font, fontSize); } } - internal static List GetAllDPIScaleControls(this Control control) + internal static Font SetDPIScaleFont(this Font font, float fontSize) => UIDPIScale.NeedSetDPIFont() ? font.DPIScaleFont(fontSize) : font; + + internal static List GetAllDPIScaleControls(this Control control) { - var list = new List(); + var list = new List(); foreach (Control con in control.Controls) { - list.Add(con); + if (con is IStyleInterface ctrl) list.Add(ctrl); if (con is UITextBox) continue; if (con is UIDropControl) continue; diff --git a/SunnyUI/Style/UIStyle.cs b/SunnyUI/Style/UIStyle.cs index 708b8073..d1766db3 100644 --- a/SunnyUI/Style/UIStyle.cs +++ b/SunnyUI/Style/UIStyle.cs @@ -32,32 +32,18 @@ namespace Sunny.UI { public interface IStyleInterface { - UIStyle Style - { - get; set; - } + UIStyle Style { get; set; } - bool StyleCustomMode - { - get; set; - } + bool StyleCustomMode { get; set; } - string Version - { - get; - } + string Version { get; } - string TagString - { - get; set; - } + string TagString { get; set; } void SetStyleColor(UIBaseStyle uiColor); void SetStyle(UIStyle style); - bool IsScaled { get; } - void SetDPIScale(); } diff --git a/SunnyUI/Style/UIStyles.cs b/SunnyUI/Style/UIStyles.cs index 21572f82..260785ed 100644 --- a/SunnyUI/Style/UIStyles.cs +++ b/SunnyUI/Style/UIStyles.cs @@ -405,8 +405,6 @@ namespace Sunny.UI public static void SetDPIScale() { - if (!DPIScale) return; - foreach (var form in Forms.Values) { if (UIDPIScale.NeedSetDPIFont())