diff --git a/Bin/net40/SunnyUI.dll b/Bin/net40/SunnyUI.dll index 95bb8435..b6be6340 100644 Binary files a/Bin/net40/SunnyUI.dll and b/Bin/net40/SunnyUI.dll differ diff --git a/SunnyUI/Controls/UINavMenu.cs b/SunnyUI/Controls/UINavMenu.cs index 7279bce8..a638a8d8 100644 --- a/SunnyUI/Controls/UINavMenu.cs +++ b/SunnyUI/Controls/UINavMenu.cs @@ -80,6 +80,12 @@ namespace Sunny.UI SetScrollInfo(); } + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + tmpFont?.Dispose(); + } + [Browsable(false)] public bool IsScaled { get; private set; } @@ -623,8 +629,7 @@ namespace Sunny.UI if (ShowTips && MenuHelper.GetTipsText(e.Node).IsValid() && TreeNodeSymbols.NotContainsKey(e.Node)) { - var tmpFont = this.DPIScaleFont(TipsFont); - SizeF tipsSize = e.Graphics.MeasureString(MenuHelper.GetTipsText(e.Node), tmpFont); + SizeF tipsSize = e.Graphics.MeasureString(MenuHelper.GetTipsText(e.Node), TempFont); float sfMax = Math.Max(tipsSize.Width, tipsSize.Height) + 1; float tipsLeft = Width - (ScrollBarVisible ? ScrollBarInfo.VerticalScrollBarWidth() : 0) - sfMax - sfMax; float tipsTop = e.Bounds.Y + (ItemHeight - sfMax) / 2; @@ -635,20 +640,34 @@ namespace Sunny.UI if (MenuHelper[e.Node].TipsCustom) { e.Graphics.FillEllipse(MenuHelper[e.Node].TipsBackColor, tipsLeft, tipsTop, sfMax, sfMax); - e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), tmpFont, MenuHelper[e.Node].TipsForeColor, new RectangleF(tipsLeft, tipsTop, sfMax, sfMax), alignment); + e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, MenuHelper[e.Node].TipsForeColor, new RectangleF(tipsLeft, tipsTop, sfMax, sfMax), alignment); } else { e.Graphics.FillEllipse(TipsColor, tipsLeft, tipsTop, sfMax, sfMax); - e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), tmpFont, TipsForeColor, new RectangleF(tipsLeft, tipsTop, sfMax, sfMax), alignment); + e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, TipsForeColor, new RectangleF(tipsLeft, tipsTop, sfMax, sfMax), alignment); } } - - tmpFont.Dispose(); } } } + Font tmpFont; + + private Font TempFont + { + get + { + if (tmpFont == null || !tmpFont.Size.EqualsFloat(TipsFont.DPIScaleFontSize())) + { + tmpFont?.Dispose(); + tmpFont = this.DPIScaleFont(TipsFont); + } + + return tmpFont; + } + } + private Color tipsColor = Color.Red; [DefaultValue(typeof(Color), "Red"), Category("SunnyUI"), Description("节点提示圆点背景颜色")] diff --git a/SunnyUI/Style/UIDPIScale.cs b/SunnyUI/Style/UIDPIScale.cs index 448dc0e9..84a274ec 100644 --- a/SunnyUI/Style/UIDPIScale.cs +++ b/SunnyUI/Style/UIDPIScale.cs @@ -32,24 +32,6 @@ namespace Sunny.UI return control.CreateGraphics().DpiX / 96.0f; } - public static Font DPIScaleFont(this Control control, Font font) - { - if (UIStyles.DPIScale) - { - if (font.GdiCharSet == 134) - return new Font(font.FontFamily, font.Size / control.DPIScale(), font.Style, font.Unit, font.GdiCharSet); - else - return new Font(font.FontFamily, font.Size / control.DPIScale()); - } - else - { - if (font.GdiCharSet == 134) - return new Font(font.FontFamily, font.Size, font.Style, font.Unit, font.GdiCharSet); - else - return new Font(font.FontFamily, font.Size); - } - } - public static float DPIScaleFontSize(this Control control, Font font) { if (UIStyles.DPIScale) @@ -59,12 +41,22 @@ namespace Sunny.UI } public static float DPIScaleFontSize(this Font font) + { + return font.Size.DPIScaleFontSize(); + } + + public static float DPIScaleFontSize(this float fontSize) { using Control control = new(); if (UIStyles.DPIScale) - return font.Size / control.DPIScale(); + return fontSize / control.DPIScale(); else - return font.Size; + return fontSize; + } + + public static Font DPIScaleFont(this Control control, Font font) + { + return control.DPIScaleFont(font, font.Size); } public static Font DPIScaleFont(this Control control, Font font, float fontSize)