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

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>
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<UIBarSeries> 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];

View File

@ -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);

View File

@ -162,7 +162,7 @@ namespace Sunny.UI
private void DrawSeries(Graphics g, List<UIDoughnutSeries> 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];

View File

@ -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);
@ -838,6 +845,7 @@ namespace Sunny.UI
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> selectPointsTemp = new List<UILineSelectPoint>();
@ -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);
}

View File

@ -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];

View File

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

View File

@ -777,14 +777,12 @@ namespace Sunny.UI
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.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);
}
}
}
private int activeDay = -1;
private bool isToday;

View File

@ -1265,14 +1265,12 @@ namespace Sunny.UI
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.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);
}
}
}
private bool isToday;

View File

@ -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();
}
/// <summary>
/// 重载字体变更
/// </summary>
@ -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();
}

View File

@ -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<UISymbolButton>()) label.SetDPIScale();
}
}
}

View File

@ -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")]

View File

@ -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();
}

View File

@ -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")]

View File

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

View File

@ -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);

View File

@ -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<T>(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<IStyleInterface> GetAllDPIScaleControls(this Control control)
{
var list = new List<IStyleInterface>();