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

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -31,38 +31,24 @@ namespace Sunny.UI
public static float DPIScale()
{
if (dpiScale > 0)
if (dpiScale < 0)
{
return dpiScale;
}
using Bitmap bmp = new Bitmap(1, 1);
using Graphics g = bmp.Graphics();
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()
{
return 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);
return UIStyles.DPIScale && (DPIScale() > 1 || UIStyles.GlobalFont);
}
internal static Font DPIScaleFont(this Font font, float fontSize)
{
if (fontSize <= 0) return font;
if (UIStyles.DPIScale)
{
if (UIStyles.GlobalFont)
@ -81,25 +67,23 @@ namespace Sunny.UI
}
}
internal static void SetDPIScaleFont(this Control control)
{
if (!UIStyles.DPIScale) return;
if (UIDPIScale.NeedSetDPIFont())
internal static void SetDPIScaleFont(this Control control, float fontSize)
{
if (!UIDPIScale.NeedSetDPIFont()) return;
if (control is IStyleInterface ctrl)
{
if (!ctrl.IsScaled)
control.Font = control.Font.DPIScaleFont();
}
control.Font = SetDPIScaleFont(control.Font, fontSize);
}
}
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)
{
list.Add(con);
if (con is IStyleInterface ctrl) list.Add(ctrl);
if (con is UITextBox) continue;
if (con is UIDropControl) continue;

View File

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

View File

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