From d759f41ad82ce708cc8e8f214f1afe90e04cc87a Mon Sep 17 00:00:00 2001 From: Sunny Date: Wed, 30 Aug 2023 15:43:13 +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.Demo/Bin/SunnyUI.Demo.exe | Bin 1315328 -> 1315328 bytes SunnyUI/Charts/UIBarChart.cs | 4 +- SunnyUI/Charts/UIChart.cs | 48 ++---------------- SunnyUI/Charts/UIDoughnutChart.cs | 2 +- SunnyUI/Charts/UILineChart.cs | 9 ++++ SunnyUI/Charts/UIPieChart.cs | 1 + .../Controls/DropItem/UIComboTreeViewItem.cs | 1 + SunnyUI/Controls/DropItem/UIDateItem.cs | 10 ++-- SunnyUI/Controls/DropItem/UIDateTimeItem.cs | 10 ++-- SunnyUI/Controls/DropItem/UIDropControl.cs | 13 ++++- SunnyUI/Controls/DropItem/UINumPadItem.cs | 9 ++++ SunnyUI/Controls/UIEdit.cs | 2 +- SunnyUI/Controls/UITextBox.cs | 14 ++++- SunnyUI/Controls/UITreeView.cs | 2 +- SunnyUI/Controls/UIUserControl.cs | 2 +- SunnyUI/Forms/UINotifier.cs | 2 +- SunnyUI/Style/UIDPIScale.cs | 15 +++--- 17 files changed, 74 insertions(+), 70 deletions(-) diff --git a/SunnyUI.Demo/Bin/SunnyUI.Demo.exe b/SunnyUI.Demo/Bin/SunnyUI.Demo.exe index 3c420e651712b1575e386eb5ffc1a665dc6d3f6c..2083eea99e5dde702144864cc9a6dd0eb83a6b14 100644 GIT binary patch delta 223 zcmZoz640绘制图面 protected virtual void DrawAxis(Graphics g) { + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); g.FillRectangle(FillColor, Option.Grid.Left, 1, Width - Option.Grid.Left - Option.Grid.Right, Option.Grid.Top); g.FillRectangle(FillColor, Option.Grid.Left, Height - Option.Grid.Bottom, Width - Option.Grid.Left - Option.Grid.Right, Option.Grid.Bottom - 1); @@ -579,6 +580,7 @@ namespace Sunny.UI private void DrawAxisScales(Graphics g) { + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); foreach (var line in Option.YAxisScaleLines) { double ymin = YAxisStart * YAxisInterval; @@ -599,7 +601,7 @@ namespace Sunny.UI protected virtual void DrawSeries(Graphics g, List series) { if (series == null || series.Count == 0) return; - + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); for (int i = 0; i < Bars.Count; i++) { var bars = Bars[i]; diff --git a/SunnyUI/Charts/UIChart.cs b/SunnyUI/Charts/UIChart.cs index 87d7c90f..c294725a 100644 --- a/SunnyUI/Charts/UIChart.cs +++ b/SunnyUI/Charts/UIChart.cs @@ -68,13 +68,6 @@ namespace Sunny.UI tip.Font = this.Font.DPIScaleFont(UIStyles.DefaultSubFontSize); } - protected override void Dispose(bool disposing) - { - base.Dispose(disposing); - tmpFont?.Dispose(); - tmpLegendFont?.Dispose(); - } - private void Tip_MouseEnter(object sender, EventArgs e) { tip.Visible = false; @@ -199,42 +192,6 @@ namespace Sunny.UI DrawOption(e.Graphics); } - Font tmpFont; - - protected Font TempFont - { - get - { - float size = UIStyles.DefaultSubFontSize; - - if (tmpFont == null || !tmpFont.Size.EqualsFloat(size / UIDPIScale.DPIScale())) - { - tmpFont?.Dispose(); - tmpFont = this.Font.DPIScaleFont(size); - } - - return tmpFont; - } - } - - Font tmpLegendFont; - - protected Font TempLegendFont - { - get - { - float size = UIStyles.DefaultSubFontSize; - - if (tmpLegendFont == null || !tmpLegendFont.Size.EqualsFloat(size / UIDPIScale.DPIScale())) - { - tmpLegendFont?.Dispose(); - tmpLegendFont = this.Font.DPIScaleFont(size); - } - - return tmpLegendFont; - } - } - protected virtual void DrawOption(Graphics g) { } @@ -267,8 +224,12 @@ namespace Sunny.UI protected void DrawTitle(Graphics g, UITitle title) { if (title == null) return; + if (!title.Text.IsValid()) return; Size sf = TextRenderer.MeasureText(title.Text, Font); g.DrawString(title.Text, Font, ChartStyle.ForeColor, new Rectangle(TextInterval, TextInterval, Width - TextInterval * 2, Height - TextInterval * 2), (StringAlignment)((int)title.Left), (StringAlignment)((int)title.Top)); + + if (!title.SubText.IsValid()) return; + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); g.DrawString(title.SubText, TempFont, ChartStyle.ForeColor, new Rectangle(TextInterval, TextInterval, Width - TextInterval * 2, Height - TextInterval * 2), (StringAlignment)((int)title.Left), (StringAlignment)((int)title.Top), 0, title.Top == UITopAlignment.Bottom ? -sf.Height : sf.Height); } @@ -282,6 +243,7 @@ namespace Sunny.UI float maxWidth = 0; float oneHeight = 0; + using var TempLegendFont = this.Font.DPIScaleFont(UIStyles.DefaultSubFontSize); foreach (var data in legend.Data) { Size sf = TextRenderer.MeasureText(data, TempLegendFont); diff --git a/SunnyUI/Charts/UIDoughnutChart.cs b/SunnyUI/Charts/UIDoughnutChart.cs index ebd83e2a..9e4daf34 100644 --- a/SunnyUI/Charts/UIDoughnutChart.cs +++ b/SunnyUI/Charts/UIDoughnutChart.cs @@ -162,7 +162,7 @@ namespace Sunny.UI private void DrawSeries(Graphics g, List series) { if (series == null || series.Count == 0) return; - + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); for (int pieIndex = 0; pieIndex < series.Count; pieIndex++) { var pie = series[pieIndex]; diff --git a/SunnyUI/Charts/UILineChart.cs b/SunnyUI/Charts/UILineChart.cs index d22e22cc..01465f0e 100644 --- a/SunnyUI/Charts/UILineChart.cs +++ b/SunnyUI/Charts/UILineChart.cs @@ -297,6 +297,7 @@ namespace Sunny.UI if (Option.Grid.BottomShow) g.DrawLine(ForeColor, Option.Grid.Left, Height - Option.Grid.Bottom, Width - Option.Grid.Right, Height - Option.Grid.Bottom); + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); float zeroPos = YScale.CalcYPixel(0, DrawOrigin.Y, DrawSize.Height); if (zeroPos > Option.Grid.Top && zeroPos < Height - Option.Grid.Bottom) { @@ -764,7 +765,9 @@ namespace Sunny.UI private void DrawAxisScales(Graphics g) { + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); if (YScale != null) + { foreach (var line in Option.YAxisScaleLines) { float pos = YScale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height); @@ -785,8 +788,10 @@ namespace Sunny.UI g.DrawString(line.Name, TempFont, line.Color, new Rectangle(DrawOrigin.X + 4, (int)pos - 2 - Height, DrawSize.Width - 8, Height), (StringAlignment)((int)line.Left), StringAlignment.Far); } + } if (Y2Scale != null) + { foreach (var line in Option.Y2AxisScaleLines) { float pos = Y2Scale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height); @@ -805,9 +810,11 @@ namespace Sunny.UI g.DrawString(line.Name, TempFont, line.Color, new Rectangle(DrawOrigin.X + 4, (int)pos - 2 - Height, DrawSize.Width - 8, Height), (StringAlignment)((int)line.Left), StringAlignment.Far); } + } int idx = 0; if (XScale != null) + { foreach (var line in Option.XAxisScaleLines) { float pos = XScale.CalcXPixel(line.Value, DrawOrigin.X, DrawSize.Width); @@ -837,6 +844,7 @@ namespace Sunny.UI idx++; g.DrawString(line.Name, TempFont, line.Color, new Rectangle((int)x, (int)y, Width, Height), ContentAlignment.TopLeft); } + } } private readonly List selectPoints = new List(); @@ -948,6 +956,7 @@ namespace Sunny.UI { using (Graphics g = this.CreateGraphics()) { + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); Size sf = TextRenderer.MeasureText(sb.ToString(), TempFont); tip.Size = new Size(sf.Width + 4, sf.Height + 4); } diff --git a/SunnyUI/Charts/UIPieChart.cs b/SunnyUI/Charts/UIPieChart.cs index 33f1beb3..68ad3c37 100644 --- a/SunnyUI/Charts/UIPieChart.cs +++ b/SunnyUI/Charts/UIPieChart.cs @@ -174,6 +174,7 @@ namespace Sunny.UI return; } + using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize); for (int pieIndex = 0; pieIndex < series.Count; pieIndex++) { var pie = series[pieIndex]; diff --git a/SunnyUI/Controls/DropItem/UIComboTreeViewItem.cs b/SunnyUI/Controls/DropItem/UIComboTreeViewItem.cs index 7497f9c1..0d379e7b 100644 --- a/SunnyUI/Controls/DropItem/UIComboTreeViewItem.cs +++ b/SunnyUI/Controls/DropItem/UIComboTreeViewItem.cs @@ -20,6 +20,7 @@ namespace Sunny.UI btnOK.SetDPIScale(); btnCancel.SetDPIScale(); + uiCheckBox1.SetDPIScale(); } public bool ShowSelectedAllCheckBox diff --git a/SunnyUI/Controls/DropItem/UIDateItem.cs b/SunnyUI/Controls/DropItem/UIDateItem.cs index 530ace02..2ea27426 100644 --- a/SunnyUI/Controls/DropItem/UIDateItem.cs +++ b/SunnyUI/Controls/DropItem/UIDateItem.cs @@ -777,12 +777,10 @@ namespace Sunny.UI if (ShowToday) { - using (Font SubFont = new Font("宋体", 10.5f / UIDPIScale.DPIScale())) - { - e.Graphics.FillRectangle(p3.FillColor, p3.Width - width * 4 + 1, p3.Height - height + 1, width * 4 - 2, height - 2); - e.Graphics.FillRoundRectangle(PrimaryColor, new Rectangle(p3.Width - width * 4 + 6, p3.Height - height + 3, 8, height - 10), 3); - e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, new Rectangle(p3.Width - width * 4 + 17, p3.Height - height - 1, Width, height), ContentAlignment.MiddleLeft); - } + using Font SubFont = this.Font.DPIScaleFont(10.5f); + e.Graphics.FillRectangle(p3.FillColor, p3.Width - width * 4 + 1, p3.Height - height + 1, width * 4 - 2, height - 2); + e.Graphics.FillRoundRectangle(PrimaryColor, new Rectangle(p3.Width - width * 4 + 6, p3.Height - height + 3, 8, height - 10), 3); + e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, new Rectangle(p3.Width - width * 4 + 17, p3.Height - height - 1, Width, height), ContentAlignment.MiddleLeft); } } diff --git a/SunnyUI/Controls/DropItem/UIDateTimeItem.cs b/SunnyUI/Controls/DropItem/UIDateTimeItem.cs index 6929ebdf..703b5ecd 100644 --- a/SunnyUI/Controls/DropItem/UIDateTimeItem.cs +++ b/SunnyUI/Controls/DropItem/UIDateTimeItem.cs @@ -1265,12 +1265,10 @@ namespace Sunny.UI if (ShowToday) { - using (Font SubFont = new Font("宋体", 10.5f / UIDPIScale.DPIScale())) - { - e.Graphics.FillRectangle(p3.FillColor, p3.Width - width * 4 + 1, p3.Height - height + 1, width * 4 - 2, height - 2); - e.Graphics.FillRoundRectangle(PrimaryColor, new Rectangle((int)(p3.Width - width * 4 + 6), p3.Height - height + 3, 8, height - 10), 3); - e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, new Rectangle(p3.Width - width * 4 + 17, p3.Height - height - 1, Width, height), ContentAlignment.MiddleLeft); - } + using Font SubFont = this.Font.DPIScaleFont(10.5f); + e.Graphics.FillRectangle(p3.FillColor, p3.Width - width * 4 + 1, p3.Height - height + 1, width * 4 - 2, height - 2); + e.Graphics.FillRoundRectangle(PrimaryColor, new Rectangle((int)(p3.Width - width * 4 + 6), p3.Height - height + 3, 8, height - 10), 3); + e.Graphics.DrawString(UILocalize.Today + ": " + DateTime.Now.DateString(), SubFont, isToday ? PrimaryColor : Color.DarkGray, new Rectangle(p3.Width - width * 4 + 17, p3.Height - height - 1, Width, height), ContentAlignment.MiddleLeft); } } diff --git a/SunnyUI/Controls/DropItem/UIDropControl.cs b/SunnyUI/Controls/DropItem/UIDropControl.cs index a7d48912..a053682d 100644 --- a/SunnyUI/Controls/DropItem/UIDropControl.cs +++ b/SunnyUI/Controls/DropItem/UIDropControl.cs @@ -70,6 +70,15 @@ namespace Sunny.UI ControlBoxRect = new Rectangle(Width - 24, 0, 24, Height); } + public override void SetDPIScale() + { + base.SetDPIScale(); + if (DesignMode) return; + if (!UIDPIScale.NeedSetDPIFont()) return; + + edit.SetDPIScale(); + } + /// /// 重载字体变更 /// @@ -77,8 +86,8 @@ namespace Sunny.UI protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); - edit.Font = Font; - edit.SetDPIScale(); + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + edit.Font = this.Font.Clone(DefaultFontSize); SizeChange(); Invalidate(); } diff --git a/SunnyUI/Controls/DropItem/UINumPadItem.cs b/SunnyUI/Controls/DropItem/UINumPadItem.cs index 3f70fcd3..dda06bfd 100644 --- a/SunnyUI/Controls/DropItem/UINumPadItem.cs +++ b/SunnyUI/Controls/DropItem/UINumPadItem.cs @@ -328,5 +328,14 @@ namespace Sunny.UI item.SetStyleColor(uiColor); } } + + public override void SetDPIScale() + { + base.SetDPIScale(); + if (DesignMode) return; + if (!UIDPIScale.NeedSetDPIFont()) return; + + foreach (var label in this.GetControls()) label.SetDPIScale(); + } } } diff --git a/SunnyUI/Controls/UIEdit.cs b/SunnyUI/Controls/UIEdit.cs index 104b23c2..422d89b0 100644 --- a/SunnyUI/Controls/UIEdit.cs +++ b/SunnyUI/Controls/UIEdit.cs @@ -179,7 +179,7 @@ namespace Sunny.UI { if (!UIDPIScale.NeedSetDPIFont()) return; if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; - Font = UIDPIScale.SetDPIScaleFont(Font, DefaultFontSize); + Font = UIDPIScale.DPIScaleFont(Font, DefaultFontSize); } [Description("开启后可响应某些触屏的点击事件"), Category("SunnyUI")] diff --git a/SunnyUI/Controls/UITextBox.cs b/SunnyUI/Controls/UITextBox.cs index 2ce4fda7..823e9604 100644 --- a/SunnyUI/Controls/UITextBox.cs +++ b/SunnyUI/Controls/UITextBox.cs @@ -138,6 +138,15 @@ namespace Sunny.UI TextAlignmentChange += UITextBox_TextAlignmentChange; } + public override void SetDPIScale() + { + base.SetDPIScale(); + if (DesignMode) return; + if (!UIDPIScale.NeedSetDPIFont()) return; + + edit.SetDPIScale(); + } + [Description("开启后可响应某些触屏的点击事件"), Category("SunnyUI")] [DefaultValue(false)] public bool TouchPressClick @@ -650,7 +659,10 @@ namespace Sunny.UI protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); - edit.Font = Font; + + if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; + edit.Font = this.Font.Clone(DefaultFontSize); + SizeChange(); Invalidate(); } diff --git a/SunnyUI/Controls/UITreeView.cs b/SunnyUI/Controls/UITreeView.cs index 31208074..85c21664 100644 --- a/SunnyUI/Controls/UITreeView.cs +++ b/SunnyUI/Controls/UITreeView.cs @@ -924,7 +924,7 @@ namespace Sunny.UI if (DesignMode) return; if (!UIDPIScale.NeedSetDPIFont()) return; if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; - this.Font = UIDPIScale.SetDPIScaleFont(this.Font, DefaultFontSize); + this.Font = UIDPIScale.DPIScaleFont(this.Font, DefaultFontSize); } [DefaultValue(typeof(Color), "155, 200, 255")] diff --git a/SunnyUI/Controls/UIUserControl.cs b/SunnyUI/Controls/UIUserControl.cs index 5f81f043..f8f6b398 100644 --- a/SunnyUI/Controls/UIUserControl.cs +++ b/SunnyUI/Controls/UIUserControl.cs @@ -82,7 +82,7 @@ namespace Sunny.UI } - private float DefaultFontSize = -1; + protected float DefaultFontSize = -1; public virtual void SetDPIScale() { diff --git a/SunnyUI/Forms/UINotifier.cs b/SunnyUI/Forms/UINotifier.cs index 74b4d3e0..55a3f02d 100644 --- a/SunnyUI/Forms/UINotifier.cs +++ b/SunnyUI/Forms/UINotifier.cs @@ -41,7 +41,7 @@ namespace Sunny.UI { public void SetDPIScale() { - Font = UIDPIScale.SetDPIScaleFont(Font, Font.Size); + Font = UIDPIScale.DPIScaleFont(Font, Font.Size); noteTitle.Font = noteTitle.Font.DPIScaleFont(noteTitle.Font.Size); noteContent.Font = noteContent.Font.DPIScaleFont(noteContent.Font.Size); diff --git a/SunnyUI/Style/UIDPIScale.cs b/SunnyUI/Style/UIDPIScale.cs index 387c5998..42477c6e 100644 --- a/SunnyUI/Style/UIDPIScale.cs +++ b/SunnyUI/Style/UIDPIScale.cs @@ -29,7 +29,7 @@ namespace Sunny.UI { private static float dpiScale = -1; - public static float DPIScale() => UIStyles.GlobalFont ? SystemDPIScale * 100.0f / UIStyles.GlobalFontScale : SystemDPIScale; + public static float DPIScale => UIStyles.GlobalFont ? SystemDPIScale * 100.0f / UIStyles.GlobalFontScale : SystemDPIScale; private static float SystemDPIScale { @@ -56,11 +56,11 @@ namespace Sunny.UI if (UIStyles.GlobalFont) { byte gdiCharSet = UIStyles.GetGdiCharSet(UIStyles.GlobalFontName); - return new Font(UIStyles.GlobalFontName, fontSize / DPIScale(), font.Style, font.Unit, gdiCharSet); + return new Font(UIStyles.GlobalFontName, fontSize / DPIScale, font.Style, font.Unit, gdiCharSet); } else { - return new Font(font.FontFamily, fontSize / DPIScale(), font.Style, font.Unit, font.GdiCharSet); + return new Font(font.FontFamily, fontSize / DPIScale, font.Style, font.Unit, font.GdiCharSet); } } else @@ -69,14 +69,17 @@ namespace Sunny.UI } } + internal static Font Clone(this Font font, float fontSize) + { + return new Font(font.FontFamily, fontSize, font.Style, font.Unit, font.GdiCharSet); + } + internal static void SetDPIScaleFont(this T control, float fontSize) where T : Control, IStyleInterface { if (!UIDPIScale.NeedSetDPIFont()) return; - control.Font = SetDPIScaleFont(control.Font, fontSize); + control.Font = DPIScaleFont(control.Font, fontSize); } - 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();