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

This commit is contained in:
Sunny 2023-08-29 16:03:00 +08:00
parent 1ec80d250c
commit 83caee295e
37 changed files with 157 additions and 403 deletions

View File

@ -65,7 +65,6 @@ namespace Sunny.UI
private void Tip_VisibleChanged(object sender, EventArgs e)
{
tip.IsScaled = true;
tip.Font = this.Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
}

View File

@ -13,26 +13,16 @@ namespace Sunny.UI
public override void SetDPIScale()
{
if (!IsScaled)
{
m_opacitySlider.SetDPIScaleFont();
m_colorBar.SetDPIScaleFont();
foreach (var label in this.GetControls<UILabel>())
{
label.SetDPIScaleFont();
}
foreach (var label in this.GetControls<UITextBox>())
{
label.SetDPIScaleFont();
}
btnOK.SetDPIScaleFont();
btnCancel.SetDPIScaleFont();
}
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
m_opacitySlider.SetDPIScale();
m_colorBar.SetDPIScale();
btnOK.SetDPIScale();
btnCancel.SetDPIScale();
foreach (var label in this.GetControls<UILabel>()) label.SetDPIScale();
foreach (var label in this.GetControls<UITextBox>()) label.SetDPIScale();
}
public void Translate()

View File

@ -67,16 +67,15 @@ namespace Sunny.UI
public override void SetDPIScale()
{
if (!IsScaled)
{
edtFilter.SetDPIScaleFont();
btnSearch.SetDPIScaleFont();
btnClear.SetDPIScaleFont();
btnOK.SetDPIScaleFont();
btnCancel.SetDPIScaleFont();
}
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
edtFilter.SetDPIScale();
btnSearch.SetDPIScale();
btnClear.SetDPIScale();
btnOK.SetDPIScale();
btnCancel.SetDPIScale();
}
public void Translate()

View File

@ -14,13 +14,12 @@ namespace Sunny.UI
public override void SetDPIScale()
{
if (!IsScaled)
{
btnOK.SetDPIScaleFont();
btnCancel.SetDPIScaleFont();
}
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
btnOK.SetDPIScale();
btnCancel.SetDPIScale();
}
public bool ShowSelectedAllCheckBox

View File

@ -330,12 +330,11 @@ namespace Sunny.UI
public override void SetDPIScale()
{
if (!IsScaled)
{
TopPanel.SetDPIScaleFont();
}
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
TopPanel.SetDPIScale();
}
public DateTime max = DateTime.MaxValue;

View File

@ -738,20 +738,14 @@ namespace Sunny.UI
public override void SetDPIScale()
{
if (!IsScaled)
{
TopPanel.SetDPIScaleFont();
foreach (var label in this.GetControls<UILabel>())
{
label.SetDPIScaleFont();
}
btnOK.SetDPIScaleFont();
btnCancel.SetDPIScaleFont();
}
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
TopPanel.SetDPIScale();
btnOK.SetDPIScale();
btnCancel.SetDPIScale();
foreach (var label in this.GetControls<UILabel>()) label.SetDPIScale();
}
public void Translate()

View File

@ -77,8 +77,8 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
edit.IsScaled = true;
edit.Font = Font;
edit.SetDPIScale();
SizeChange();
Invalidate();
}

View File

@ -440,18 +440,13 @@ namespace Sunny.UI
public override void SetDPIScale()
{
if (!IsScaled)
{
foreach (var label in this.GetControls<UILabel>())
{
label.SetDPIScaleFont();
}
btnOK.SetDPIScaleFont();
btnCancel.SetDPIScaleFont();
}
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
btnOK.SetDPIScale();
btnCancel.SetDPIScale();
foreach (var label in this.GetControls<UILabel>()) label.SetDPIScale();
}
private void UITimeItem_MouseWheel(object sender, System.Windows.Forms.MouseEventArgs e)

View File

@ -295,32 +295,6 @@ namespace Sunny.UI
}
}
Font tmpFont;
private Font TempFont
{
get
{
if (tmpFont == null || !tmpFont.Size.EqualsFloat(TipsFont.DPIScaleFontSize()))
{
tmpFont?.Dispose();
tmpFont = TipsFont.DPIScaleFont();
}
return tmpFont;
}
}
/// <summary>
/// 析构函数
/// </summary>
/// <param name="disposing">释放参数</param>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
tmpFont?.Dispose();
}
/// <summary>
/// 重载绘图
/// </summary>
@ -339,6 +313,7 @@ namespace Sunny.UI
if (Enabled && ShowTips && !string.IsNullOrEmpty(TipsText))
{
e.Graphics.SetHighQuality();
using var TempFont = TipsFont.DPIScaleFont(TipsFont.Size);
Size sf = TextRenderer.MeasureText(TipsText, TempFont);
int sfMax = Math.Max(sf.Width, sf.Height);
int x = Width - 1 - 2 - sfMax;

View File

@ -344,12 +344,11 @@ namespace Sunny.UI
public override void SetDPIScale()
{
if (!IsScaled)
{
TopPanel.SetDPIScaleFont();
}
base.SetDPIScale();
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
TopPanel.SetDPIScale();
}
public void Translate()

View File

@ -63,25 +63,19 @@ namespace Sunny.UI
}
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
protected override void OnOpening(CancelEventArgs e)
{
base.OnOpening(e);
if (!IsScaled && UIStyles.DPIScale)
{
SetDPIScale();
}
SetDPIScale();
}
/// <summary>

View File

@ -78,9 +78,6 @@ namespace Sunny.UI
protected bool selected;
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
private float DefaultFontSize = -1;
public virtual void SetDPIScale()

View File

@ -191,22 +191,35 @@ namespace Sunny.UI
}
[Browsable(false)]
public bool IsScaled { get; private set; }
float ColumnHeadersDefaultCellStyleFontSize = -1;
float RowHeadersDefaultCellStyleFontSize = -1;
float DefaultCellStyleFontSize = -1;
float RowsDefaultCellStyleFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
if (ColumnHeadersDefaultCellStyle.Font != null)
{
if (ColumnHeadersDefaultCellStyle.Font != null)
ColumnHeadersDefaultCellStyle.Font = ColumnHeadersDefaultCellStyle.Font.DPIScaleFont();
if (RowHeadersDefaultCellStyle.Font != null)
RowHeadersDefaultCellStyle.Font = RowHeadersDefaultCellStyle.Font.DPIScaleFont();
if (DefaultCellStyle.Font != null)
DefaultCellStyle.Font = DefaultCellStyle.Font.DPIScaleFont();
if (RowsDefaultCellStyle.Font != null)
RowsDefaultCellStyle.Font = RowsDefaultCellStyle.Font.DPIScaleFont();
IsScaled = true;
if (ColumnHeadersDefaultCellStyleFontSize < 0) ColumnHeadersDefaultCellStyleFontSize = ColumnHeadersDefaultCellStyle.Font.Size;
ColumnHeadersDefaultCellStyle.Font = ColumnHeadersDefaultCellStyle.Font.DPIScaleFont(ColumnHeadersDefaultCellStyleFontSize);
}
if (RowHeadersDefaultCellStyle.Font != null)
{
if (RowHeadersDefaultCellStyleFontSize < 0) RowHeadersDefaultCellStyleFontSize = RowHeadersDefaultCellStyle.Font.Size;
RowHeadersDefaultCellStyle.Font = RowHeadersDefaultCellStyle.Font.DPIScaleFont(RowHeadersDefaultCellStyleFontSize);
}
if (DefaultCellStyle.Font != null)
{
if (DefaultCellStyleFontSize < 0) DefaultCellStyleFontSize = DefaultCellStyle.Font.Size;
DefaultCellStyle.Font = DefaultCellStyle.Font.DPIScaleFont(DefaultCellStyleFontSize);
}
if (RowsDefaultCellStyle.Font != null)
{
if (RowsDefaultCellStyleFontSize < 0) RowsDefaultCellStyleFontSize = RowsDefaultCellStyle.Font.Size;
RowsDefaultCellStyle.Font = RowsDefaultCellStyle.Font.DPIScaleFont(RowsDefaultCellStyleFontSize);
}
}

View File

@ -99,18 +99,8 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
if (pnlValue != null)
{
pnlValue.IsScaled = true;
pnlValue.Font = Font;
}
if (edit != null)
{
edit.IsScaled = true;
edit.Font = Font;
}
pnlValue?.SetDPIScale();
edit?.SetDPIScale();
}
public event OnValueChanged ValueChanged;

View File

@ -173,16 +173,13 @@ namespace Sunny.UI
}
}
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
Font = UIDPIScale.SetDPIScaleFont(Font, DefaultFontSize);
}
[Description("开启后可响应某些触屏的点击事件"), Category("SunnyUI")]

View File

@ -727,6 +727,7 @@ namespace Sunny.UI
if (Enabled && ShowTips && !string.IsNullOrEmpty(TipsText))
{
e.Graphics.SetHighQuality();
using var TempFont = TipsFont.DPIScaleFont(TipsFont.Size);
Size sf = TextRenderer.MeasureText(TipsText, TempFont);
int sfMax = Math.Max(sf.Width, sf.Height);
int x = Width - 1 - 2 - sfMax;
@ -736,22 +737,6 @@ namespace Sunny.UI
}
}
Font tmpFont;
private Font TempFont
{
get
{
if (tmpFont == null || !tmpFont.Size.EqualsFloat(TipsFont.DPIScaleFontSize()))
{
tmpFont?.Dispose();
tmpFont = TipsFont.DPIScaleFont();
}
return tmpFont;
}
}
[DefaultValue(null)]
[Description("关联的TabControl"), Category("SunnyUI")]
public UITabControl TabControl { get; set; }

View File

@ -117,15 +117,13 @@ namespace Sunny.UI
}
private UIStyle _style = UIStyle.Blue;
public bool IsScaled { get; private set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
[Category("SunnyUI")]

View File

@ -223,7 +223,6 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
listbox.IsScaled = true;
listbox.Font = Font;
}
@ -461,7 +460,7 @@ namespace Sunny.UI
}
[ToolboxItem(false)]
private sealed class ImageListBox : ListBox
private sealed class ImageListBox : ListBox, IStyleInterface
{
private UIScrollBar bar;
@ -482,16 +481,13 @@ namespace Sunny.UI
}
}
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
//protected override void WndProc(ref Message m)

View File

@ -117,18 +117,8 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
if (pnlValue != null)
{
pnlValue.IsScaled = true;
pnlValue.Font = Font;
}
if (edit != null)
{
edit.IsScaled = true;
edit.Font = Font;
}
pnlValue?.SetDPIScale();
edit?.SetDPIScale();
}
private int step = 1;

View File

@ -1323,17 +1323,13 @@ namespace Sunny.UI
#endregion
[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);
}
public string Version

View File

@ -76,16 +76,13 @@ namespace Sunny.UI
}
}
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
/// <summary>
@ -213,16 +210,13 @@ 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 (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
/// <summary>

View File

@ -413,7 +413,6 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
listbox.IsScaled = true;
listbox.Font = Font;
}

View File

@ -31,7 +31,7 @@ namespace Sunny.UI
/// ListBox
/// </summary>
[ToolboxItem(false)]
internal sealed class ListBoxEx : ListBox
internal sealed class ListBoxEx : ListBox, IStyleInterface
{
private UIScrollBar bar;
@ -42,16 +42,13 @@ namespace Sunny.UI
[Description("获取或设置包含有关控件的数据的对象字符串"), Category("SunnyUI")]
public string TagString { get; set; }
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
public UIScrollBar Bar

View File

@ -145,29 +145,10 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
foreach (var item in this.GetControls<UISymbolButton>(true))
{
item.IsScaled = true;
item.Font = Font;
}
foreach (var item in this.GetControls<UITextBox>(true))
{
item.IsScaled = true;
item.Font = Font;
}
foreach (var item in this.GetControls<UIComboBox>(true))
{
item.IsScaled = true;
item.Font = Font;
}
foreach (var item in this.GetControls<UILabel>(true))
{
item.IsScaled = true;
item.Font = Font;
}
foreach (var item in this.GetControls<UISymbolButton>(true)) item.Font = Font;
foreach (var item in this.GetControls<UITextBox>(true)) item.Font = Font;
foreach (var item in this.GetControls<UIComboBox>(true)) item.Font = Font;
foreach (var item in this.GetControls<UILabel>(true)) item.Font = Font;
}
private int buttonInterval = 8;

View File

@ -91,16 +91,13 @@ namespace Sunny.UI
Invalidate();
}
[Browsable(false)]
public bool IsScaled { get; private set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
private int radius;
@ -645,8 +642,7 @@ namespace Sunny.UI
NavBarMenu.Style = UIStyles.Style;
NavBarMenu.Items.Clear();
NavBarMenu.ImageList = ImageList;
NavBarMenu.IsScaled = false;
NavBarMenu.Font = DropMenuFont;
NavBarMenu.Font = DropMenuFont.SetDPIScaleFont(DropMenuFont.Size);
foreach (TreeNode node in Nodes[SelectedIndex].Nodes)
{
ToolStripMenuItem item = new ToolStripMenuItem(node.Text) { Tag = node };

View File

@ -197,22 +197,13 @@ namespace Sunny.UI
}
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
tmpFont?.Dispose();
}
[Browsable(false)]
public bool IsScaled { get; private set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
[DefaultValue(false)]
@ -757,6 +748,7 @@ namespace Sunny.UI
//显示Tips圆圈
if (ShowTips && MenuHelper.GetTipsText(e.Node).IsValid())
{
using var TempFont = TipsFont.DPIScaleFont(TipsFont.Size);
Size tipsSize = TextRenderer.MeasureText(MenuHelper.GetTipsText(e.Node), TempFont);
int sfMax = Math.Max(tipsSize.Width, tipsSize.Height);
int tipsLeft = Width - sfMax - 16;
@ -784,22 +776,6 @@ namespace Sunny.UI
base.OnDrawNode(e);
}
Font tmpFont;
private Font TempFont
{
get
{
if (tmpFont == null || !tmpFont.Size.EqualsFloat(TipsFont.DPIScaleFontSize()))
{
tmpFont?.Dispose();
tmpFont = TipsFont.DPIScaleFont();
}
return tmpFont;
}
}
private Color tipsColor = Color.Red;
[DefaultValue(typeof(Color), "Red"), Category("SunnyUI"), Description("节点提示圆点背景颜色")]

View File

@ -140,30 +140,10 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
foreach (var item in this.GetControls<UISymbolButton>(true))
{
item.IsScaled = true;
item.Font = Font;
}
foreach (var item in this.GetControls<UITextBox>(true))
{
item.IsScaled = true;
item.Font = Font;
}
foreach (var item in this.GetControls<UIComboBox>(true))
{
item.IsScaled = true;
item.Font = Font;
}
foreach (var item in this.GetControls<UILabel>(true))
{
item.IsScaled = true;
item.Font = Font;
}
foreach (var item in this.GetControls<UISymbolButton>(true)) item.SetDPIScale();
foreach (var item in this.GetControls<UITextBox>(true)) item.SetDPIScale();
foreach (var item in this.GetControls<UIComboBox>(true)) item.SetDPIScale();
foreach (var item in this.GetControls<UILabel>(true)) item.SetDPIScale();
Translate();
}

View File

@ -110,7 +110,6 @@ namespace Sunny.UI
Parent = this,
TagString = i.ToString(),
Style = Style,
IsScaled = IsScaled,
Text = Items[i]?.ToString(),
StyleCustomMode = StyleCustomMode,
ForeColor = ForeColor

View File

@ -87,16 +87,13 @@ namespace Sunny.UI
}
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
private float DefaultFontSize = -1;
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
private Color foreColor;

View File

@ -288,9 +288,6 @@ namespace Sunny.UI
get; set;
}
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
public void Collapse()
{
if (_collapsePanel != UICollapsePanel.None && SplitPanelState == UISplitPanelState.Expanded)

View File

@ -166,16 +166,13 @@ 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 (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
}
protected override void Dispose(bool disposing)
@ -696,6 +693,7 @@ namespace Sunny.UI
string TipsText = GetTipsText(TabPages[index]);
if (Enabled && TipsText.IsValid())
{
using var TempFont = TipsFont.DPIScaleFont(TipsFont.Size);
sf = TextRenderer.MeasureText(TipsText, TempFont);
int sfMax = Math.Max(sf.Width, sf.Height);
int x = TabRect.Width - 1 - 2 - sfMax;
@ -734,22 +732,6 @@ namespace Sunny.UI
}
}
Font tmpFont;
private Font TempFont
{
get
{
if (tmpFont == null || !tmpFont.Size.EqualsFloat(TipsFont.DPIScaleFontSize()))
{
tmpFont?.Dispose();
tmpFont = TipsFont.DPIScaleFont();
}
return tmpFont;
}
}
/// <summary>
/// 重载鼠标按下事件
/// </summary>

View File

@ -650,7 +650,6 @@ namespace Sunny.UI
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
edit.IsScaled = true;
edit.Font = Font;
SizeChange();
Invalidate();

View File

@ -44,19 +44,11 @@ namespace Sunny.UI
InitOwnerDraw();
}
public UIToolTip(IContainer cont)
: base(cont)
public UIToolTip(IContainer cont) : base(cont)
{
InitOwnerDraw();
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
tmpTitleFont?.Dispose();
tmpFont?.Dispose();
}
[DefaultValue(typeof(Font), "微软雅黑, 9pt"), Description("字体"), Category("SunnyUI")]
public Font Font { get; set; } = new Font("宋体", 9);
@ -172,6 +164,9 @@ namespace Sunny.UI
private void UIToolTip_Popup(object sender, PopupEventArgs e)
{
using var TempFont = Font.DPIScaleFont(Font.Size);
using var TempTitleFont = TitleFont.DPIScaleFont(TitleFont.Size);
if (ToolTipControls.ContainsKey(e.AssociatedControl))
{
var tooltip = ToolTipControls[e.AssociatedControl];
@ -206,43 +201,13 @@ namespace Sunny.UI
}
}
Font tmpFont;
private Font TempFont
{
get
{
if (tmpFont == null || !tmpFont.Size.EqualsFloat(Font.DPIScaleFontSize()))
{
tmpFont?.Dispose();
tmpFont = Font.DPIScaleFont();
}
return tmpFont;
}
}
Font tmpTitleFont;
private Font TempTitleFont
{
get
{
if (tmpTitleFont == null || !tmpTitleFont.Size.EqualsFloat(TitleFont.DPIScaleFontSize()))
{
tmpTitleFont?.Dispose();
tmpTitleFont = TitleFont.DPIScaleFont();
}
return tmpTitleFont;
}
}
private void ToolTipExDraw(object sender, DrawToolTipEventArgs e)
{
var bounds = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width - 1, e.Bounds.Height - 1);
e.Graphics.FillRectangle(BackColor, bounds);
e.Graphics.DrawRectangle(RectColor, bounds);
using var TempFont = Font.DPIScaleFont(Font.Size);
using var TempTitleFont = TitleFont.DPIScaleFont(TitleFont.Size);
if (ToolTipControls.ContainsKey(e.AssociatedControl))
{

View File

@ -746,7 +746,6 @@ namespace Sunny.UI
base.OnFontChanged(e);
if (view != null)
{
view.IsScaled = true;
view.Font = Font;
}
}
@ -925,7 +924,7 @@ namespace Sunny.UI
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;
if (DefaultFontSize < 0) DefaultFontSize = this.Font.Size;
this.SetDPIScaleFont(DefaultFontSize);
this.Font = UIDPIScale.SetDPIScaleFont(this.Font, DefaultFontSize);
}
[DefaultValue(typeof(Color), "155, 200, 255")]

View File

@ -84,7 +84,7 @@ namespace Sunny.UI
private float DefaultFontSize = -1;
public void SetDPIScale()
public virtual void SetDPIScale()
{
if (DesignMode) return;
if (!UIDPIScale.NeedSetDPIFont()) return;

View File

@ -41,26 +41,17 @@ namespace Sunny.UI
{
public sealed partial class UINotifier : Form
{
[Browsable(false)]
public bool IsScaled { get; private set; }
public void SetDPIScale()
{
if (!IsScaled && UIStyles.DPIScale)
Font = UIDPIScale.SetDPIScaleFont(Font, Font.Size);
noteTitle.Font = noteTitle.Font.DPIScaleFont(noteTitle.Font.Size);
noteContent.Font = noteContent.Font.DPIScaleFont(noteContent.Font.Size);
noteDate.Font = noteDate.Font.DPIScaleFont(noteDate.Font.Size);
foreach (var control in this.GetAllDPIScaleControls())
{
this.SetDPIScaleFont();
noteTitle.Font = noteTitle.Font.DPIScaleFont();
noteContent.Font = noteContent.Font.DPIScaleFont();
noteDate.Font = noteDate.Font.DPIScaleFont();
foreach (Control control in this.GetAllDPIScaleControls())
{
control.SetDPIScaleFont();
}
IsScaled = true;
control.SetDPIScale();
}
}
@ -385,7 +376,7 @@ namespace Sunny.UI
//-------------------------------------------------------------------------------------------------------------------------------
private void onMenuClick(object sender, EventArgs e)
{
closeAllToolStripMenuItem.Font = menu.Font.DPIScaleFont();
closeAllToolStripMenuItem.Font = menu.Font.DPIScaleFont(menu.Font.Size);
menu.Show(buttonMenu, new Point(0, buttonMenu.Height));
}

View File

@ -67,13 +67,10 @@ namespace Sunny.UI
}
}
internal static void SetDPIScaleFont(this Control control, float fontSize)
internal static void SetDPIScaleFont<T>(this T control, float fontSize) where T : Control, IStyleInterface
{
if (!UIDPIScale.NeedSetDPIFont()) return;
if (control is IStyleInterface ctrl)
{
control.Font = SetDPIScaleFont(control.Font, fontSize);
}
control.Font = SetDPIScaleFont(control.Font, fontSize);
}
internal static Font SetDPIScaleFont(this Font font, float fontSize) => UIDPIScale.NeedSetDPIFont() ? font.DPIScaleFont(fontSize) : font;