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

This commit is contained in:
Sunny 2023-08-30 11:18:57 +08:00
parent 83caee295e
commit 82f4074ba5
3 changed files with 27 additions and 26 deletions

View File

@ -642,7 +642,7 @@ namespace Sunny.UI
NavBarMenu.Style = UIStyles.Style; NavBarMenu.Style = UIStyles.Style;
NavBarMenu.Items.Clear(); NavBarMenu.Items.Clear();
NavBarMenu.ImageList = ImageList; NavBarMenu.ImageList = ImageList;
NavBarMenu.Font = DropMenuFont.SetDPIScaleFont(DropMenuFont.Size); NavBarMenu.Font = DropMenuFont;
foreach (TreeNode node in Nodes[SelectedIndex].Nodes) foreach (TreeNode node in Nodes[SelectedIndex].Nodes)
{ {
ToolStripMenuItem item = new ToolStripMenuItem(node.Text) { Tag = node }; ToolStripMenuItem item = new ToolStripMenuItem(node.Text) { Tag = node };

View File

@ -35,8 +35,6 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Windows.Forms; using System.Windows.Forms;
#pragma warning disable 1591
namespace Sunny.UI namespace Sunny.UI
{ {
public sealed partial class UINotifier : Form public sealed partial class UINotifier : Form
@ -108,7 +106,7 @@ namespace Sunny.UI
InApplication = insideMe; InApplication = insideMe;
InitializeComponent(); InitializeComponent();
if (Notes.Count == 0) if (Notes.IsEmpty)
ID = 1; ID = 1;
else else
ID = (short)(Notes.Keys.Max() + 1); // Set the Note ID ID = (short)(Notes.Keys.Max() + 1); // Set the Note ID

View File

@ -29,7 +29,11 @@ namespace Sunny.UI
{ {
private static float dpiScale = -1; private static float dpiScale = -1;
public static float DPIScale() public static float DPIScale() => UIStyles.GlobalFont ? SystemDPIScale * 100.0f / UIStyles.GlobalFontScale : SystemDPIScale;
private static float SystemDPIScale
{
get
{ {
if (dpiScale < 0) if (dpiScale < 0)
{ {
@ -38,13 +42,11 @@ namespace Sunny.UI
dpiScale = g.DpiX / 96.0f; dpiScale = g.DpiX / 96.0f;
} }
return UIStyles.GlobalFont ? dpiScale * 100.0f / UIStyles.GlobalFontScale : dpiScale; return dpiScale;
}
} }
public static bool NeedSetDPIFont() public static bool NeedSetDPIFont() => UIStyles.DPIScale && (SystemDPIScale > 1 || UIStyles.GlobalFont);
{
return UIStyles.DPIScale && (DPIScale() > 1 || UIStyles.GlobalFont);
}
internal static Font DPIScaleFont(this Font font, float fontSize) internal static Font DPIScaleFont(this Font font, float fontSize)
{ {
@ -78,21 +80,22 @@ namespace Sunny.UI
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>();
foreach (Control con in control.Controls) foreach (Control ctrl in control.Controls)
{ {
if (con is IStyleInterface ctrl) list.Add(ctrl); if (ctrl is IStyleInterface istyleCtrl) list.Add(istyleCtrl);
if (con is UITextBox) continue; if (ctrl is UITextBox) continue;
if (con is UIDropControl) continue; if (ctrl is UIDropControl) continue;
if (con is UIListBox) continue; if (ctrl is UIListBox) continue;
if (con is UIImageListBox) continue; if (ctrl is UIImageListBox) continue;
if (con is UIPagination) continue; if (ctrl is UIPagination) continue;
if (con is UIRichTextBox) continue; if (ctrl is UIRichTextBox) continue;
if (con is UITreeView) continue; if (ctrl is UITreeView) continue;
if (ctrl is UITransfer) continue;
if (con.Controls.Count > 0) if (ctrl.Controls.Count > 0)
{ {
list.AddRange(GetAllDPIScaleControls(con)); list.AddRange(GetAllDPIScaleControls(ctrl));
} }
} }