* 重构字体

This commit is contained in:
Sunny 2023-05-25 10:51:41 +08:00
parent 0b8c69b095
commit 7ae4bb613d
25 changed files with 114 additions and 127 deletions

View File

@ -40,8 +40,8 @@ namespace Sunny.UI
Width = 400; Width = 400;
Height = 300; Height = 300;
SubFont = UIFontColor.SubFont(); SubFont = UIStyles.SubFont();
LegendFont = UIFontColor.SubFont(); LegendFont = UIStyles.SubFont();
tip.Parent = this; tip.Parent = this;
tip.Height = 32; tip.Height = 32;
@ -50,7 +50,7 @@ namespace Sunny.UI
tip.Top = 1; tip.Top = 1;
tip.StyleCustomMode = true; tip.StyleCustomMode = true;
tip.Style = UIStyle.Custom; tip.Style = UIStyle.Custom;
tip.Font = UIFontColor.SubFont(); tip.Font = UIStyles.SubFont();
tip.RadiusSides = UICornerRadiusSides.None; tip.RadiusSides = UICornerRadiusSides.None;
tip.Visible = false; tip.Visible = false;
@ -65,8 +65,7 @@ namespace Sunny.UI
private void Tip_VisibleChanged(object sender, EventArgs e) private void Tip_VisibleChanged(object sender, EventArgs e)
{ {
tip.IsScaled = true; tip.IsScaled = true;
float size = SubFont != null ? SubFont.Size : UIFontColor.SubFontSize; tip.Font = this.Font.DPIScaleFont(UIStyles.DefaultSubFontSize);
tip.Font = this.Font.DPIScaleFont(size);
} }
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
@ -206,7 +205,7 @@ namespace Sunny.UI
{ {
get get
{ {
float size = SubFont != null ? SubFont.Size : UIFontColor.SubFontSize; float size = UIStyles.DefaultSubFontSize;
if (tmpFont == null || !tmpFont.Size.EqualsFloat(size / UIDPIScale.DPIScale())) if (tmpFont == null || !tmpFont.Size.EqualsFloat(size / UIDPIScale.DPIScale()))
{ {
@ -224,7 +223,7 @@ namespace Sunny.UI
{ {
get get
{ {
float size = LegendFont != null ? LegendFont.Size : UIFontColor.SubFontSize; float size = UIStyles.DefaultSubFontSize;
if (tmpLegendFont == null || !tmpLegendFont.Size.EqualsFloat(size / UIDPIScale.DPIScale())) if (tmpLegendFont == null || !tmpLegendFont.Size.EqualsFloat(size / UIDPIScale.DPIScale()))
{ {

View File

@ -47,7 +47,7 @@ namespace Sunny.UI
public static class UIMessageTip public static class UIMessageTip
{ {
//默认字体。当样式中的Font==null时用该字体替换 //默认字体。当样式中的Font==null时用该字体替换
static readonly Font DefaultFont = UIFontColor.Font(); static readonly Font DefaultFont = UIStyles.Font();
//文本格式。用于测量和绘制 //文本格式。用于测量和绘制
static readonly StringFormat DefStringFormat = StringFormat.GenericTypographic; static readonly StringFormat DefStringFormat = StringFormat.GenericTypographic;
@ -634,7 +634,7 @@ namespace Sunny.UI
Width = 2 Width = 2
}; };
IconSpacing = 5; IconSpacing = 5;
TextFont = UIFontColor.Font(); TextFont = UIStyles.Font();
var fontName = TextFont.Name; var fontName = TextFont.Name;
if (fontName == "宋体") { TextOffset = new Point(1, 1); } if (fontName == "宋体") { TextOffset = new Point(1, 1); }
TextColor = Color.Black; TextColor = Color.Black;

View File

@ -45,7 +45,7 @@ namespace Sunny.UI
Padding = new Padding(0, 0, 30, 2); Padding = new Padding(0, 0, 30, 2);
edit.AutoSize = false; edit.AutoSize = false;
edit.Font = UIFontColor.Font(); edit.Font = UIStyles.Font();
edit.Left = 4; edit.Left = 4;
edit.Top = 3; edit.Top = 3;
edit.Text = String.Empty; edit.Text = String.Empty;

View File

@ -195,7 +195,7 @@ namespace Sunny.UI
} }
} }
private Font tipsFont = UIFontColor.SubFont(); private Font tipsFont = UIStyles.SubFont();
/// <summary> /// <summary>
/// 角标文字字体 /// 角标文字字体

View File

@ -33,7 +33,7 @@ namespace Sunny.UI
public UIContextMenuStrip() public UIContextMenuStrip()
{ {
Font = UIFontColor.Font(); Font = UIStyles.Font();
RenderMode = ToolStripRenderMode.Professional; RenderMode = ToolStripRenderMode.Professional;
Renderer = new ToolStripProfessionalRenderer(ColorTable); Renderer = new ToolStripProfessionalRenderer(ColorTable);
Version = UIGlobal.Version; Version = UIGlobal.Version;

View File

@ -47,7 +47,7 @@ namespace Sunny.UI
public UIControl() public UIControl()
{ {
Version = UIGlobal.Version; Version = UIGlobal.Version;
base.Font = UIFontColor.Font(); base.Font = UIStyles.Font();
Size = new Size(100, 35); Size = new Size(100, 35);
base.MinimumSize = new Size(1, 1); base.MinimumSize = new Size(1, 1);
} }

View File

@ -63,7 +63,7 @@ namespace Sunny.UI
{ {
BackgroundColor = UIColor.White; BackgroundColor = UIColor.White;
GridColor = UIColor.Blue; GridColor = UIColor.Blue;
base.Font = UIFontColor.Font(); base.Font = UIStyles.Font();
base.DoubleBuffered = true; base.DoubleBuffered = true;
VBar.Parent = this; VBar.Parent = this;
@ -91,17 +91,17 @@ namespace Sunny.UI
ColumnHeadersDefaultCellStyle.BackColor = UIColor.Blue; ColumnHeadersDefaultCellStyle.BackColor = UIColor.Blue;
ColumnHeadersDefaultCellStyle.ForeColor = UIColor.White; ColumnHeadersDefaultCellStyle.ForeColor = UIColor.White;
ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True; ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True;
ColumnHeadersDefaultCellStyle.Font = UIFontColor.Font(); ColumnHeadersDefaultCellStyle.Font = UIStyles.Font();
//行头部颜色 //行头部颜色
RowHeadersDefaultCellStyle.BackColor = UIColor.LightBlue; RowHeadersDefaultCellStyle.BackColor = UIColor.LightBlue;
RowHeadersDefaultCellStyle.ForeColor = UIFontColor.Primary; RowHeadersDefaultCellStyle.ForeColor = UIFontColor.Primary;
RowHeadersDefaultCellStyle.SelectionBackColor = UIColor.Blue; RowHeadersDefaultCellStyle.SelectionBackColor = UIColor.Blue;
RowHeadersDefaultCellStyle.SelectionForeColor = Color.White; RowHeadersDefaultCellStyle.SelectionForeColor = Color.White;
RowHeadersDefaultCellStyle.Font = UIFontColor.Font(); RowHeadersDefaultCellStyle.Font = UIStyles.Font();
RowsDefaultCellStyle.Font = UIFontColor.Font(); RowsDefaultCellStyle.Font = UIStyles.Font();
DefaultCellStyle.Font = UIFontColor.Font(); DefaultCellStyle.Font = UIStyles.Font();
//标题行行高与OnColumnAdded事件配合 //标题行行高与OnColumnAdded事件配合
ColumnHeadersHeight = 32; ColumnHeadersHeight = 32;

View File

@ -40,7 +40,7 @@ namespace Sunny.UI
Height = 29; Height = 29;
RadiusSides = UICornerRadiusSides.None; RadiusSides = UICornerRadiusSides.None;
Font = UIFontColor.Font(); Font = UIStyles.Font();
foreColor = UIStyles.Blue.DataGridViewFooterForeColor; foreColor = UIStyles.Blue.DataGridViewFooterForeColor;
fillColor = UIStyles.Blue.PlainColor; fillColor = UIStyles.Blue.PlainColor;
rectColor = UIStyles.Blue.RectColor; rectColor = UIStyles.Blue.RectColor;

View File

@ -42,7 +42,7 @@ namespace Sunny.UI
{ {
//设置为单选边框 //设置为单选边框
BorderStyle = BorderStyle.FixedSingle; BorderStyle = BorderStyle.FixedSingle;
base.Font = UIFontColor.Font(); base.Font = UIStyles.Font();
base.ForeColor = UIFontColor.Primary; base.ForeColor = UIFontColor.Primary;
Width = 150; Width = 150;
base.MaxLength = 32767; base.MaxLength = 32767;

View File

@ -146,7 +146,7 @@ namespace Sunny.UI
} }
} }
private Font tipsFont = UIFontColor.SubFont(); private Font tipsFont = UIStyles.SubFont();
[Description("角标文字字体"), Category("SunnyUI")] [Description("角标文字字体"), Category("SunnyUI")]
[DefaultValue(typeof(Font), "微软雅黑, 9pt")] [DefaultValue(typeof(Font), "微软雅黑, 9pt")]

View File

@ -209,7 +209,7 @@ namespace Sunny.UI
Height = 35; Height = 35;
Version = UIGlobal.Version; Version = UIGlobal.Version;
Cursor = Cursors.Hand; Cursor = Cursors.Hand;
base.Font = UIFontColor.Font(); base.Font = UIStyles.Font();
} }
/// <summary> /// <summary>

View File

@ -36,7 +36,7 @@ namespace Sunny.UI
{ {
public UILabel() public UILabel()
{ {
base.Font = UIFontColor.Font(); base.Font = UIStyles.Font();
Version = UIGlobal.Version; Version = UIGlobal.Version;
base.TextAlign = ContentAlignment.MiddleLeft; base.TextAlign = ContentAlignment.MiddleLeft;
foreColor = UIStyles.Blue.LabelForeColor; foreColor = UIStyles.Blue.LabelForeColor;
@ -181,7 +181,7 @@ namespace Sunny.UI
{ {
public UILinkLabel() public UILinkLabel()
{ {
Font = UIFontColor.Font(); Font = UIStyles.Font();
LinkBehavior = LinkBehavior.AlwaysUnderline; LinkBehavior = LinkBehavior.AlwaysUnderline;
Version = UIGlobal.Version; Version = UIGlobal.Version;

View File

@ -191,7 +191,7 @@ namespace Sunny.UI
// //
BorderStyle = BorderStyle.FixedSingle; BorderStyle = BorderStyle.FixedSingle;
DrawMode = DrawMode.OwnerDrawFixed; DrawMode = DrawMode.OwnerDrawFixed;
Font = UIFontColor.Font(); Font = UIStyles.Font();
IntegralHeight = false; IntegralHeight = false;
ItemHeight = 25; ItemHeight = 25;
Size = new Size(150, 200); Size = new Size(150, 200);

View File

@ -56,7 +56,7 @@ namespace Sunny.UI
DoubleBuffered = true; DoubleBuffered = true;
UpdateStyles(); UpdateStyles();
Font = UIFontColor.Font(); Font = UIStyles.Font();
NavBarMenu.VisibleChanged += NavBarMenu_VisibleChanged; NavBarMenu.VisibleChanged += NavBarMenu_VisibleChanged;
Dock = DockStyle.Top; Dock = DockStyle.Top;
@ -351,7 +351,7 @@ namespace Sunny.UI
set => NavBarMenu.ImageList = value; set => NavBarMenu.ImageList = value;
} }
private Font dropMenuFont = UIFontColor.Font(); private Font dropMenuFont = UIStyles.Font();
/// <summary> /// <summary>
/// 标题字体 /// 标题字体

View File

@ -72,7 +72,7 @@ namespace Sunny.UI
//ShowRootLines = false; //ShowRootLines = false;
DoubleBuffered = true; DoubleBuffered = true;
Font = UIFontColor.Font(); Font = UIStyles.Font();
//CheckBoxes = false; //CheckBoxes = false;
ItemHeight = 50; ItemHeight = 50;
BackColor = Color.FromArgb(56, 56, 56); BackColor = Color.FromArgb(56, 56, 56);
@ -340,7 +340,7 @@ namespace Sunny.UI
} }
} }
private Font tipsFont = UIFontColor.SubFont(); private Font tipsFont = UIStyles.SubFont();
[Description("角标文字字体"), Category("SunnyUI")] [Description("角标文字字体"), Category("SunnyUI")]
[DefaultValue(typeof(Font), "微软雅黑, 9pt")] [DefaultValue(typeof(Font), "微软雅黑, 9pt")]

View File

@ -39,7 +39,7 @@ namespace Sunny.UI
public UIPanel() public UIPanel()
{ {
InitializeComponent(); InitializeComponent();
base.Font = UIFontColor.Font(); base.Font = UIStyles.Font();
base.MinimumSize = new System.Drawing.Size(1, 1); base.MinimumSize = new System.Drawing.Size(1, 1);
showText = true; showText = true;
SetStyleFlags(true, false, true); SetStyleFlags(true, false, true);

View File

@ -36,7 +36,7 @@ namespace Sunny.UI
{ {
public UISmoothLabel() public UISmoothLabel()
{ {
base.Font = UIFontColor.Font(36); base.Font = UIStyles.Font();
Version = UIGlobal.Version; Version = UIGlobal.Version;
foreColor = UIStyles.Blue.SmoothLabelForeColor; foreColor = UIStyles.Blue.SmoothLabelForeColor;

View File

@ -61,7 +61,7 @@ namespace Sunny.UI
ItemSize = new Size(150, 40); ItemSize = new Size(150, 40);
DrawMode = TabDrawMode.OwnerDrawFixed; DrawMode = TabDrawMode.OwnerDrawFixed;
Font = UIFontColor.Font(); Font = UIStyles.Font();
AfterSetFillColor(FillColor); AfterSetFillColor(FillColor);
Version = UIGlobal.Version; Version = UIGlobal.Version;
@ -149,7 +149,7 @@ namespace Sunny.UI
} }
} }
private Font tipsFont = UIFontColor.SubFont(); private Font tipsFont = UIStyles.SubFont();
[Description("角标文字字体"), Category("SunnyUI")] [Description("角标文字字体"), Category("SunnyUI")]
[DefaultValue(typeof(Font), "微软雅黑, 9pt")] [DefaultValue(typeof(Font), "微软雅黑, 9pt")]

View File

@ -42,7 +42,7 @@ namespace Sunny.UI
base.ItemSize = new Size(40, 200); base.ItemSize = new Size(40, 200);
DrawMode = TabDrawMode.OwnerDrawFixed; DrawMode = TabDrawMode.OwnerDrawFixed;
Font = UIFontColor.Font(); Font = UIStyles.Font();
AfterSetFillColor(FillColor); AfterSetFillColor(FillColor);
Size = new Size(450, 270); Size = new Size(450, 270);
Version = UIGlobal.Version; Version = UIGlobal.Version;

View File

@ -72,7 +72,7 @@ namespace Sunny.UI
SetStyleFlags(true, true, true); SetStyleFlags(true, true, true);
ShowText = false; ShowText = false;
Font = UIFontColor.Font(); Font = UIStyles.Font();
MinimumSize = new Size(1, 16); MinimumSize = new Size(1, 16);
Width = 150; Width = 150;

View File

@ -46,7 +46,7 @@ namespace Sunny.UI
InitializeComponent(); InitializeComponent();
Version = UIGlobal.Version; Version = UIGlobal.Version;
AutoScaleMode = AutoScaleMode.None; AutoScaleMode = AutoScaleMode.None;
base.Font = UIFontColor.Font(); base.Font = UIStyles.Font();
base.MinimumSize = new System.Drawing.Size(1, 1); base.MinimumSize = new System.Drawing.Size(1, 1);
SetStyleFlags(true, false); SetStyleFlags(true, false);
} }

View File

@ -558,7 +558,7 @@ namespace Sunny.UI
/// <summary> /// <summary>
/// 标题字体 /// 标题字体
/// </summary> /// </summary>
private Font titleFont = UIFontColor.Font(); private Font titleFont = UIStyles.Font();
/// <summary> /// <summary>
/// 标题字体 /// 标题字体

View File

@ -841,7 +841,7 @@ namespace Sunny.UI
/// <summary> /// <summary>
/// 标题字体 /// 标题字体
/// </summary> /// </summary>
private Font titleFont = UIFontColor.Font(); private Font titleFont = UIStyles.Font();
/// <summary> /// <summary>
/// 标题字体 /// 标题字体

View File

@ -23,11 +23,9 @@
* 2021-10-16: V3.0.8 DPI缩放自适应 * 2021-10-16: V3.0.8 DPI缩放自适应
******************************************************************************/ ******************************************************************************/
using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
@ -309,93 +307,6 @@ namespace Sunny.UI
/// </summary> /// </summary>
public static class UIFontColor public static class UIFontColor
{ {
private static readonly ConcurrentDictionary<string, byte> FontCharSets = new ConcurrentDictionary<string, byte>();
//GdiCharSet
//一个字节值,该值指定使用此 Font 字符集的 GDI 字符集。 默认值为 1。
//字符集 值
//ANSI 0
//DEFAULT 1
//象征 2
//SHIFTJIS 128
//HANGEUL 129
//HANGUL 129
//GB2312 134
//中国BIG5 136
//OEM 255
//JOHAB 130
//希伯来语 177
//阿拉伯语 178
//希腊语 161
//土耳其语 162
//越南语 163
//泰语 222
//EASTEUROPE 238
//俄语 204
//MAC 77
//波罗的海 186
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class LOGFONT
{
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 32)]
public string lfFaceName;
}
internal static byte GetGdiCharSet(string fontName, float fontSize)
{
if (FontCharSets.ContainsKey(fontName)) return FontCharSets[fontName];
using Font font = new Font(fontName, fontSize);
LOGFONT obj = new LOGFONT();
font.ToLogFont(obj);
FontCharSets.TryAdd(fontName, obj.lfCharSet);
return obj.lfCharSet;
}
/// <summary>
/// 默认字体
/// </summary>
public static Font Font()
{
byte gdiCharSet = GetGdiCharSet("微软雅黑", FontSize);
return new Font("微软雅黑", FontSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet);
}
/// <summary>
/// 默认字体
/// </summary>
public static Font Font(float fontSize)
{
byte gdiCharSet = GetGdiCharSet("微软雅黑", fontSize);
return new Font("微软雅黑", fontSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet);
}
public static float FontSize = 12;
/// <summary>
/// 默认二级字体
/// </summary>
public static Font SubFont()
{
byte gdiCharSet = GetGdiCharSet("微软雅黑", SubFontSize);
return new Font("微软雅黑", SubFontSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet);
}
public static float SubFontSize = 9;
/// <summary> /// <summary>
/// 主要颜色 /// 主要颜色
/// </summary> /// </summary>

View File

@ -27,6 +27,7 @@ using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms; using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
@ -42,6 +43,82 @@ namespace Sunny.UI
public static float FontSize { get; set; } = 12; public static float FontSize { get; set; } = 12;
private static readonly ConcurrentDictionary<string, byte> FontCharSets = new ConcurrentDictionary<string, byte>();
//GdiCharSet
//一个字节值,该值指定使用此 Font 字符集的 GDI 字符集。 默认值为 1。
//字符集 值
//ANSI 0
//DEFAULT 1
//象征 2
//SHIFTJIS 128
//HANGEUL 129
//HANGUL 129
//GB2312 134
//中国BIG5 136
//OEM 255
//JOHAB 130
//希伯来语 177
//阿拉伯语 178
//希腊语 161
//土耳其语 162
//越南语 163
//泰语 222
//EASTEUROPE 238
//俄语 204
//MAC 77
//波罗的海 186
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
internal class LOGFONT
{
public int lfHeight;
public int lfWidth;
public int lfEscapement;
public int lfOrientation;
public int lfWeight;
public byte lfItalic;
public byte lfUnderline;
public byte lfStrikeOut;
public byte lfCharSet;
public byte lfOutPrecision;
public byte lfClipPrecision;
public byte lfQuality;
public byte lfPitchAndFamily;
[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst = 32)]
public string lfFaceName;
}
internal static byte GetGdiCharSet(string fontName)
{
if (FontCharSets.ContainsKey(fontName)) return FontCharSets[fontName];
using Font font = new Font(fontName, 16);
LOGFONT obj = new LOGFONT();
font.ToLogFont(obj);
FontCharSets.TryAdd(fontName, obj.lfCharSet);
return obj.lfCharSet;
}
internal static float DefaultFontSize = 12;
internal static float DefaultSubFontSize = 9;
/// <summary>
/// 默认字体
/// </summary>
internal static Font Font()
{
byte gdiCharSet = GetGdiCharSet("微软雅黑");
return new Font("微软雅黑", DefaultFontSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet);
}
/// <summary>
/// 默认二级字体
/// </summary>
internal static Font SubFont()
{
byte gdiCharSet = GetGdiCharSet("微软雅黑");
return new Font("微软雅黑", DefaultSubFontSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet);
}
public static List<UIStyle> PopularStyles() public static List<UIStyle> PopularStyles()
{ {
List<UIStyle> styles = new List<UIStyle>(); List<UIStyle> styles = new List<UIStyle>();