* 重构全局字体设置逻辑,以期可以在程序运行时设置全局字体

This commit is contained in:
Sunny 2023-08-30 15:43:13 +08:00
parent 82f4074ba5
commit d759f41ad8
17 changed files with 74 additions and 70 deletions

Binary file not shown.

View File

@ -486,6 +486,7 @@ namespace Sunny.UI
/// <param name="g">绘制图面</param> /// <param name="g">绘制图面</param>
protected virtual void DrawAxis(Graphics g) 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, 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); 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) private void DrawAxisScales(Graphics g)
{ {
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
foreach (var line in Option.YAxisScaleLines) foreach (var line in Option.YAxisScaleLines)
{ {
double ymin = YAxisStart * YAxisInterval; double ymin = YAxisStart * YAxisInterval;
@ -599,7 +601,7 @@ namespace Sunny.UI
protected virtual void DrawSeries(Graphics g, List<UIBarSeries> series) protected virtual void DrawSeries(Graphics g, List<UIBarSeries> series)
{ {
if (series == null || series.Count == 0) return; if (series == null || series.Count == 0) return;
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
for (int i = 0; i < Bars.Count; i++) for (int i = 0; i < Bars.Count; i++)
{ {
var bars = Bars[i]; var bars = Bars[i];

View File

@ -68,13 +68,6 @@ namespace Sunny.UI
tip.Font = this.Font.DPIScaleFont(UIStyles.DefaultSubFontSize); 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) private void Tip_MouseEnter(object sender, EventArgs e)
{ {
tip.Visible = false; tip.Visible = false;
@ -199,42 +192,6 @@ namespace Sunny.UI
DrawOption(e.Graphics); 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) protected virtual void DrawOption(Graphics g)
{ {
} }
@ -267,8 +224,12 @@ namespace Sunny.UI
protected void DrawTitle(Graphics g, UITitle title) protected void DrawTitle(Graphics g, UITitle title)
{ {
if (title == null) return; if (title == null) return;
if (!title.Text.IsValid()) return;
Size sf = TextRenderer.MeasureText(title.Text, Font); 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)); 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), 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); (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 maxWidth = 0;
float oneHeight = 0; float oneHeight = 0;
using var TempLegendFont = this.Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
foreach (var data in legend.Data) foreach (var data in legend.Data)
{ {
Size sf = TextRenderer.MeasureText(data, TempLegendFont); Size sf = TextRenderer.MeasureText(data, TempLegendFont);

View File

@ -162,7 +162,7 @@ namespace Sunny.UI
private void DrawSeries(Graphics g, List<UIDoughnutSeries> series) private void DrawSeries(Graphics g, List<UIDoughnutSeries> series)
{ {
if (series == null || series.Count == 0) return; if (series == null || series.Count == 0) return;
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
for (int pieIndex = 0; pieIndex < series.Count; pieIndex++) for (int pieIndex = 0; pieIndex < series.Count; pieIndex++)
{ {
var pie = series[pieIndex]; var pie = series[pieIndex];

View File

@ -297,6 +297,7 @@ namespace Sunny.UI
if (Option.Grid.BottomShow) if (Option.Grid.BottomShow)
g.DrawLine(ForeColor, Option.Grid.Left, Height - Option.Grid.Bottom, Width - Option.Grid.Right, Height - Option.Grid.Bottom); 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); float zeroPos = YScale.CalcYPixel(0, DrawOrigin.Y, DrawSize.Height);
if (zeroPos > Option.Grid.Top && zeroPos < Height - Option.Grid.Bottom) if (zeroPos > Option.Grid.Top && zeroPos < Height - Option.Grid.Bottom)
{ {
@ -764,7 +765,9 @@ namespace Sunny.UI
private void DrawAxisScales(Graphics g) private void DrawAxisScales(Graphics g)
{ {
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
if (YScale != null) if (YScale != null)
{
foreach (var line in Option.YAxisScaleLines) foreach (var line in Option.YAxisScaleLines)
{ {
float pos = YScale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height); 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); 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) if (Y2Scale != null)
{
foreach (var line in Option.Y2AxisScaleLines) foreach (var line in Option.Y2AxisScaleLines)
{ {
float pos = Y2Scale.CalcYPixel(line.Value, DrawOrigin.Y, DrawSize.Height); 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); 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; int idx = 0;
if (XScale != null) if (XScale != null)
{
foreach (var line in Option.XAxisScaleLines) foreach (var line in Option.XAxisScaleLines)
{ {
float pos = XScale.CalcXPixel(line.Value, DrawOrigin.X, DrawSize.Width); float pos = XScale.CalcXPixel(line.Value, DrawOrigin.X, DrawSize.Width);
@ -837,6 +844,7 @@ namespace Sunny.UI
idx++; idx++;
g.DrawString(line.Name, TempFont, line.Color, new Rectangle((int)x, (int)y, Width, Height), ContentAlignment.TopLeft); g.DrawString(line.Name, TempFont, line.Color, new Rectangle((int)x, (int)y, Width, Height), ContentAlignment.TopLeft);
} }
}
} }
private readonly List<UILineSelectPoint> selectPoints = new List<UILineSelectPoint>(); private readonly List<UILineSelectPoint> selectPoints = new List<UILineSelectPoint>();
@ -948,6 +956,7 @@ namespace Sunny.UI
{ {
using (Graphics g = this.CreateGraphics()) using (Graphics g = this.CreateGraphics())
{ {
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
Size sf = TextRenderer.MeasureText(sb.ToString(), TempFont); Size sf = TextRenderer.MeasureText(sb.ToString(), TempFont);
tip.Size = new Size(sf.Width + 4, sf.Height + 4); tip.Size = new Size(sf.Width + 4, sf.Height + 4);
} }

View File

@ -174,6 +174,7 @@ namespace Sunny.UI
return; return;
} }
using var TempFont = Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
for (int pieIndex = 0; pieIndex < series.Count; pieIndex++) for (int pieIndex = 0; pieIndex < series.Count; pieIndex++)
{ {
var pie = series[pieIndex]; var pie = series[pieIndex];

View File

@ -20,6 +20,7 @@ namespace Sunny.UI
btnOK.SetDPIScale(); btnOK.SetDPIScale();
btnCancel.SetDPIScale(); btnCancel.SetDPIScale();
uiCheckBox1.SetDPIScale();
} }
public bool ShowSelectedAllCheckBox public bool ShowSelectedAllCheckBox

View File

@ -777,12 +777,10 @@ namespace Sunny.UI
if (ShowToday) if (ShowToday)
{ {
using (Font SubFont = new Font("宋体", 10.5f / UIDPIScale.DPIScale())) 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.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.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);
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);
}
} }
} }

View File

@ -1265,12 +1265,10 @@ namespace Sunny.UI
if (ShowToday) if (ShowToday)
{ {
using (Font SubFont = new Font("宋体", 10.5f / UIDPIScale.DPIScale())) 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.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.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);
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);
}
} }
} }

View File

@ -70,6 +70,15 @@ namespace Sunny.UI
ControlBoxRect = new Rectangle(Width - 24, 0, 24, Height); ControlBoxRect = new Rectangle(Width - 24, 0, 24, Height);
} }
public override void SetDPIScale()
{
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
edit.SetDPIScale();
}
/// <summary> /// <summary>
/// 重载字体变更 /// 重载字体变更
/// </summary> /// </summary>
@ -77,8 +86,8 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e) protected override void OnFontChanged(EventArgs e)
{ {
base.OnFontChanged(e); base.OnFontChanged(e);
edit.Font = Font; if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
edit.SetDPIScale(); edit.Font = this.Font.Clone(DefaultFontSize);
SizeChange(); SizeChange();
Invalidate(); Invalidate();
} }

View File

@ -328,5 +328,14 @@ namespace Sunny.UI
item.SetStyleColor(uiColor); item.SetStyleColor(uiColor);
} }
} }
public override void SetDPIScale()
{
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
foreach (var label in this.GetControls<UISymbolButton>()) label.SetDPIScale();
}
} }
} }

View File

@ -179,7 +179,7 @@ namespace Sunny.UI
{ {
if (!UIDPIScale.NeedSetDPIFont()) return; if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
Font = UIDPIScale.SetDPIScaleFont(Font, DefaultFontSize); Font = UIDPIScale.DPIScaleFont(Font, DefaultFontSize);
} }
[Description("开启后可响应某些触屏的点击事件"), Category("SunnyUI")] [Description("开启后可响应某些触屏的点击事件"), Category("SunnyUI")]

View File

@ -138,6 +138,15 @@ namespace Sunny.UI
TextAlignmentChange += UITextBox_TextAlignmentChange; TextAlignmentChange += UITextBox_TextAlignmentChange;
} }
public override void SetDPIScale()
{
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
edit.SetDPIScale();
}
[Description("开启后可响应某些触屏的点击事件"), Category("SunnyUI")] [Description("开启后可响应某些触屏的点击事件"), Category("SunnyUI")]
[DefaultValue(false)] [DefaultValue(false)]
public bool TouchPressClick public bool TouchPressClick
@ -650,7 +659,10 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e) protected override void OnFontChanged(EventArgs e)
{ {
base.OnFontChanged(e); base.OnFontChanged(e);
edit.Font = Font;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
edit.Font = this.Font.Clone(DefaultFontSize);
SizeChange(); SizeChange();
Invalidate(); Invalidate();
} }

View File

@ -924,7 +924,7 @@ namespace Sunny.UI
if (DesignMode) return; if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return; if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size; 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")] [DefaultValue(typeof(Color), "155, 200, 255")]

View File

@ -82,7 +82,7 @@ namespace Sunny.UI
} }
private float DefaultFontSize = -1; protected float DefaultFontSize = -1;
public virtual void SetDPIScale() public virtual void SetDPIScale()
{ {

View File

@ -41,7 +41,7 @@ namespace Sunny.UI
{ {
public void SetDPIScale() public void SetDPIScale()
{ {
Font = UIDPIScale.SetDPIScaleFont(Font, Font.Size); Font = UIDPIScale.DPIScaleFont(Font, Font.Size);
noteTitle.Font = noteTitle.Font.DPIScaleFont(noteTitle.Font.Size); noteTitle.Font = noteTitle.Font.DPIScaleFont(noteTitle.Font.Size);
noteContent.Font = noteContent.Font.DPIScaleFont(noteContent.Font.Size); noteContent.Font = noteContent.Font.DPIScaleFont(noteContent.Font.Size);

View File

@ -29,7 +29,7 @@ namespace Sunny.UI
{ {
private static float dpiScale = -1; 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 private static float SystemDPIScale
{ {
@ -56,11 +56,11 @@ namespace Sunny.UI
if (UIStyles.GlobalFont) if (UIStyles.GlobalFont)
{ {
byte gdiCharSet = UIStyles.GetGdiCharSet(UIStyles.GlobalFontName); 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 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 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<T>(this T control, float fontSize) where T : Control, IStyleInterface internal static void SetDPIScaleFont<T>(this T control, float fontSize) where T : Control, IStyleInterface
{ {
if (!UIDPIScale.NeedSetDPIFont()) return; 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<IStyleInterface> GetAllDPIScaleControls(this Control control) internal static List<IStyleInterface> GetAllDPIScaleControls(this Control control)
{ {
var list = new List<IStyleInterface>(); var list = new List<IStyleInterface>();