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

This commit is contained in:
Sunny 2023-08-29 10:18:11 +08:00
parent 5866a810f0
commit 1ec80d250c
15 changed files with 81 additions and 163 deletions

View File

@ -305,8 +305,6 @@ namespace Sunny.UI
return point; return point;
} }
static Font tmpFont;
/// <summary> /// <summary>
/// 创建消息窗图像,同时输出内容区,用于外部定位 /// 创建消息窗图像,同时输出内容区,用于外部定位
/// </summary> /// </summary>
@ -317,8 +315,7 @@ namespace Sunny.UI
var iconBounds = Rectangle.Empty; var iconBounds = Rectangle.Empty;
var textBounds = Rectangle.Empty; var textBounds = Rectangle.Empty;
Font font = style.TextFont ?? DefaultFont; Font font = style.TextFont ?? DefaultFont;
if (tmpFont == null || !tmpFont.Size.EqualsFloat(font.DPIScaleFontSize())) using Font tmpFont = font.DPIScaleFont(font.Size);
tmpFont = font.DPIScaleFont();
if (style.Icon != null) if (style.Icon != null)
{ {

View File

@ -61,16 +61,14 @@ namespace Sunny.UI
} }
[Browsable(false)] private float DefaultFontSize = -1;
public bool IsScaled { get; private set; }
public void SetDPIScale() public void SetDPIScale()
{ {
if (!IsScaled) if (DesignMode) return;
{ if (!UIDPIScale.NeedSetDPIFont()) return;
this.SetDPIScaleFont(); if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
IsScaled = true; this.SetDPIScaleFont(DefaultFontSize);
}
} }
public void SetLightness(double lightness) public void SetLightness(double lightness)

View File

@ -33,16 +33,14 @@ namespace Sunny.UI
} }
[Browsable(false)] private float DefaultFontSize = -1;
public bool IsScaled { get; private set; }
public void SetDPIScale() public void SetDPIScale()
{ {
if (!IsScaled) if (DesignMode) return;
{ if (!UIDPIScale.NeedSetDPIFont()) return;
this.SetDPIScaleFont(); if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
IsScaled = true; this.SetDPIScaleFont(DefaultFontSize);
}
} }
public new string Text public new string Text

View File

@ -144,7 +144,7 @@ namespace Sunny.UI
box.Parent = this; box.Parent = this;
box.TagString = i.ToString(); box.TagString = i.ToString();
box.Style = Style; box.Style = Style;
box.IsScaled = IsScaled; //box.IsScaled = IsScaled;
box.ValueChanged += Box_ValueChanged; box.ValueChanged += Box_ValueChanged;
box.Text = Items[i]?.ToString(); box.Text = Items[i]?.ToString();
box.StyleCustomMode = StyleCustomMode; box.StyleCustomMode = StyleCustomMode;

View File

@ -81,13 +81,13 @@ namespace Sunny.UI
[Browsable(false), DefaultValue(false)] [Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; } public bool IsScaled { get; set; }
private float DefaultFontSize = -1;
public virtual void SetDPIScale() public virtual void SetDPIScale()
{ {
if (!IsScaled) if (!UIDPIScale.NeedSetDPIFont()) return;
{ if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(); this.SetDPIScaleFont(DefaultFontSize);
IsScaled = true;
}
} }
protected void SetStyleFlags(bool supportTransparent = true, bool selectable = true, bool resizeRedraw = false) protected void SetStyleFlags(bool supportTransparent = true, bool selectable = true, bool resizeRedraw = false)

View File

@ -84,16 +84,14 @@ namespace Sunny.UI
} }
[Browsable(false)] private float DefaultFontSize = -1;
public bool IsScaled { get; private set; }
public void SetDPIScale() public void SetDPIScale()
{ {
if (!IsScaled) if (DesignMode) return;
{ if (!UIDPIScale.NeedSetDPIFont()) return;
this.SetDPIScaleFont(); if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
IsScaled = true; this.SetDPIScaleFont(DefaultFontSize);
}
} }
/// <summary> /// <summary>

View File

@ -32,16 +32,14 @@ namespace Sunny.UI
Version = UIGlobal.Version; Version = UIGlobal.Version;
} }
[Browsable(false)] private float DefaultFontSize = -1;
public bool IsScaled { get; private set; }
public void SetDPIScale() public void SetDPIScale()
{ {
if (!IsScaled) if (DesignMode) return;
{ if (!UIDPIScale.NeedSetDPIFont()) return;
this.SetDPIScaleFont(); if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
IsScaled = true; this.SetDPIScaleFont(DefaultFontSize);
}
} }
protected override void OnControlAdded(ControlEventArgs e) protected override void OnControlAdded(ControlEventArgs e)

View File

@ -918,16 +918,14 @@ namespace Sunny.UI
base.DoubleBuffered = true; base.DoubleBuffered = true;
} }
[Browsable(false), DefaultValue(false)] private float DefaultFontSize = -1;
public bool IsScaled { get; set; }
public void SetDPIScale() public void SetDPIScale()
{ {
if (!IsScaled) if (DesignMode) return;
{ if (!UIDPIScale.NeedSetDPIFont()) return;
this.SetDPIScaleFont(); if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
IsScaled = true; this.SetDPIScaleFont(DefaultFontSize);
}
} }
[DefaultValue(typeof(Color), "155, 200, 255")] [DefaultValue(typeof(Color), "155, 200, 255")]

View File

@ -82,17 +82,14 @@ namespace Sunny.UI
} }
[Browsable(false), DefaultValue(false)] private float DefaultFontSize = -1;
public bool IsScaled { get; set; }
public virtual void SetDPIScale() public void SetDPIScale()
{ {
if (DesignMode) return; if (DesignMode) return;
if (!IsScaled) if (!UIDPIScale.NeedSetDPIFont()) return;
{ if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(); this.SetDPIScaleFont(DefaultFontSize);
IsScaled = true;
}
} }
protected bool isReadOnly; protected bool isReadOnly;

View File

@ -107,7 +107,7 @@ namespace Sunny.UI
{ {
byte gdiCharSet = UIStyles.GetGdiCharSet(Font.Name); byte gdiCharSet = UIStyles.GetGdiCharSet(Font.Name);
using Font font = new Font(Font.Name, CodeFontSize, FontStyle.Bold, GraphicsUnit.Point, gdiCharSet); 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; Code = code;
Size sf = TextRenderer.MeasureText(code, fontex); Size sf = TextRenderer.MeasureText(code, fontex);
Bitmap image = new Bitmap((int)sf.Width + 16, Height - 2); Bitmap image = new Bitmap((int)sf.Width + 16, Height - 2);

View File

@ -119,34 +119,23 @@ namespace Sunny.UI
[DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")] [DefaultValue(false), Category("SunnyUI"), Description("禁止控件跟随窗体缩放")]
public bool ZoomScaleDisabled { get; set; } public bool ZoomScaleDisabled { get; set; }
[Browsable(false)] private float DefaultFontSize = -1;
public bool IsScaled { get; private set; } private float TitleFontSize = -1;
public void SetDPIScale() public void SetDPIScale()
{ {
if (DesignMode) return; if (DesignMode) return;
if (!IsScaled && UIStyles.DPIScale) if (!UIDPIScale.NeedSetDPIFont()) return;
{
this.SetDPIScaleFont();
if (UIDPIScale.NeedSetDPIFont()) if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
{ if (TitleFontSize < 0) TitleFontSize = this.TitleFont.Size;
TitleFont = TitleFont.DPIScaleFont();
}
foreach (Control control in this.GetAllDPIScaleControls()) this.SetDPIScaleFont(DefaultFontSize);
TitleFont = TitleFont.DPIScaleFont(TitleFontSize);
foreach (var control in this.GetAllDPIScaleControls())
{ {
if (control is UIDataGridView dgv) control.SetDPIScale();
{
dgv.SetDPIScale();
}
else
{
control.SetDPIScaleFont();
}
}
IsScaled = true;
} }
} }
@ -204,12 +193,6 @@ namespace Sunny.UI
public event OnZoomScaleChanged ZoomScaleChanged; public event OnZoomScaleChanged ZoomScaleChanged;
public void ResetDPIScale()
{
IsScaled = false;
SetDPIScale();
}
[DefaultValue(true)] [DefaultValue(true)]
[Description("是否点击标题栏可以移动窗体"), Category("SunnyUI")] [Description("是否点击标题栏可以移动窗体"), Category("SunnyUI")]
public bool Movable { get; set; } = true; public bool Movable { get; set; } = true;

View File

@ -267,40 +267,23 @@ namespace Sunny.UI
} }
[Browsable(false)] private float DefaultFontSize = -1;
public bool IsScaled { get; private set; } private float TitleFontSize = -1;
public void SetDPIScale() public void SetDPIScale()
{ {
if (DesignMode) return; if (DesignMode) return;
if (!IsScaled) if (!UIDPIScale.NeedSetDPIFont()) return;
{
this.SetDPIScaleFont();
if (UIDPIScale.NeedSetDPIFont())
{
this.TitleFont = TitleFont.DPIScaleFont();
}
foreach (Control control in this.GetAllDPIScaleControls()) if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
{ if (TitleFontSize < 0) TitleFontSize = this.TitleFont.Size;
if (control is UIDataGridView dgv)
{
dgv.SetDPIScale();
}
else
{
control.SetDPIScaleFont();
}
}
IsScaled = true; this.SetDPIScaleFont(DefaultFontSize);
} TitleFont = TitleFont.DPIScaleFont(TitleFontSize);
} foreach (var control in this.GetAllDPIScaleControls())
public void ResetDPIScale()
{ {
IsScaled = false; control.SetDPIScale();
SetDPIScale(); }
} }
public void Render() public void Render()

View File

@ -31,38 +31,24 @@ namespace Sunny.UI
public static float DPIScale() public static float DPIScale()
{ {
if (dpiScale > 0) if (dpiScale < 0)
{ {
return dpiScale;
}
using Bitmap bmp = new Bitmap(1, 1); using Bitmap bmp = new Bitmap(1, 1);
using Graphics g = bmp.Graphics(); using Graphics g = bmp.Graphics();
dpiScale = g.DpiX / 96.0f; 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() public static bool NeedSetDPIFont()
{ {
return DPIScale() > 1 || UIStyles.GlobalFont; return UIStyles.DPIScale && (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);
} }
internal static Font DPIScaleFont(this Font font, float fontSize) internal static Font DPIScaleFont(this Font font, float fontSize)
{ {
if (fontSize <= 0) return font;
if (UIStyles.DPIScale) if (UIStyles.DPIScale)
{ {
if (UIStyles.GlobalFont) 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 = SetDPIScaleFont(control.Font, fontSize);
control.Font = control.Font.DPIScaleFont();
}
} }
} }
internal static List<Control> GetAllDPIScaleControls(this Control control) 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<Control>(); var list = new List<IStyleInterface>();
foreach (Control con in control.Controls) 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 UITextBox) continue;
if (con is UIDropControl) continue; if (con is UIDropControl) continue;

View File

@ -32,32 +32,18 @@ namespace Sunny.UI
{ {
public interface IStyleInterface public interface IStyleInterface
{ {
UIStyle Style UIStyle Style { get; set; }
{
get; set;
}
bool StyleCustomMode bool StyleCustomMode { get; set; }
{
get; set;
}
string Version string Version { get; }
{
get;
}
string TagString string TagString { get; set; }
{
get; set;
}
void SetStyleColor(UIBaseStyle uiColor); void SetStyleColor(UIBaseStyle uiColor);
void SetStyle(UIStyle style); void SetStyle(UIStyle style);
bool IsScaled { get; }
void SetDPIScale(); void SetDPIScale();
} }

View File

@ -405,8 +405,6 @@ namespace Sunny.UI
public static void SetDPIScale() public static void SetDPIScale()
{ {
if (!DPIScale) return;
foreach (var form in Forms.Values) foreach (var form in Forms.Values)
{ {
if (UIDPIScale.NeedSetDPIFont()) if (UIDPIScale.NeedSetDPIFont())