重构字体图标显示,以期增加字体图标显示变化

This commit is contained in:
Sunny 2023-10-25 11:23:50 +08:00
parent 047a1f508d
commit dc4d7a4840
3 changed files with 37 additions and 23 deletions

View File

@ -742,7 +742,10 @@ namespace Sunny.UI
int size = 24; int size = 24;
int left = Width - size - 6; int left = Width - size - 6;
if (Bar.Visible) left -= Bar.Width; if (Bar.Visible) left -= Bar.Width;
e.Graphics.DrawFontImage(e.Node.IsExpanded ? 61702 : 61703, 24, ForeColor, left, e.Bounds.Y + (ItemHeight - 24) / 2);
SizeF sf = e.Graphics.GetFontImageSize(61702, 24);
Rectangle rect = new Rectangle((int)(left + sf.Width / 2) - 12, e.Bounds.Y, 24, e.Bounds.Height);
e.Graphics.DrawFontImage(e.Node.IsExpanded ? 61702 : 61703, 24, ForeColor, rect);
} }
//显示Tips圆圈 //显示Tips圆圈

View File

@ -289,7 +289,7 @@ namespace Sunny.UI
} }
if (Text.IsNullOrEmpty()) if (Text.IsNullOrEmpty())
e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, (Width - SymbolSize) / 2.0f, (Height - SymbolSize) / 2.0f, SymbolOffset.X, SymbolOffset.Y); e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, new RectangleF((Width - SymbolSize) / 2.0f, (Height - SymbolSize) / 2.0f, SymbolSize, SymbolSize), SymbolOffset.X, SymbolOffset.Y);
else else
e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, new Rectangle(rect.Left, rect.Top, SymbolSize, rect.Height), SymbolOffset.X, SymbolOffset.Y); e.Graphics.DrawFontImage(Symbol, SymbolSize, symbolColor, new Rectangle(rect.Left, rect.Top, SymbolSize, rect.Height), SymbolOffset.X, SymbolOffset.Y);

View File

@ -74,7 +74,7 @@ namespace Sunny.UI
/// <param name="symbol">字符</param> /// <param name="symbol">字符</param>
/// <param name="symbolSize">大小</param> /// <param name="symbolSize">大小</param>
/// <returns>字体大小</returns> /// <returns>字体大小</returns>
private static SizeF GetFontImageSize(this Graphics graphics, int symbol, int symbolSize) internal static SizeF GetFontImageSize(this Graphics graphics, int symbol, int symbolSize)
{ {
Font font = GetFont(symbol, symbolSize); Font font = GetFont(symbol, symbolSize);
if (font == null) if (font == null)
@ -109,32 +109,43 @@ namespace Sunny.UI
RectangleF rect, int xOffset = 0, int yOffSet = 0, int angle = 0) RectangleF rect, int xOffset = 0, int yOffSet = 0, int angle = 0)
{ {
SizeF sf = graphics.GetFontImageSize(symbol, symbolSize); SizeF sf = graphics.GetFontImageSize(symbol, symbolSize);
graphics.DrawFontImage(symbol, symbolSize, color, rect.Left + ((rect.Width - sf.Width) / 2.0f).RoundEx(),
if (angle == 0) rect.Top + ((rect.Height - sf.Height) / 2.0f).RoundEx() + 1, xOffset, yOffSet);
{
graphics.DrawFontImage(symbol, symbolSize, color, rect.Left + ((rect.Width - sf.Width) / 2.0f).RoundEx(),
rect.Top + ((rect.Height - sf.Height) / 2.0f).RoundEx(), xOffset, yOffSet);
}
else
{
graphics.DrawFontImage(symbol, symbolSize, color, angle, rect, xOffset, yOffSet);
}
} }
public static void DrawFontImage(this Graphics graphics, int symbol, int symbolSize, Color color, int angle, public static void DrawFontImage(this Graphics graphics, int symbol, int symbolSize, Color color, int angle,
RectangleF rect, int xOffset = 0, int yOffSet = 0) RectangleF rect, int xOffset = 0, int yOffSet = 0)
{ {
SizeF sf = graphics.GetFontImageSize(symbol, symbolSize); SizeF sf = graphics.GetFontImageSize(symbol, symbolSize);
PointF center = rect.Center(); using Bitmap bmp = new Bitmap(symbolSize, symbolSize);
Font font = GetFont(symbol, symbolSize); using Graphics g = Graphics.FromImage(bmp);
g.DrawFontImage(symbol, symbolSize, color, (symbolSize - sf.Width) / 2.0f, (symbolSize - sf.Height) / 2.0f);
var symbolValue = GetSymbolValue(symbol); using Bitmap bmp1 = RotateTransform(bmp, angle);
string text = char.ConvertFromUtf32(symbolValue); graphics.DrawImage(angle == 0 ? bmp : bmp1,
graphics.TextRenderingHint = TextRenderingHint.AntiAlias; new RectangleF(rect.Left + (rect.Width - bmp1.Width) / 2.0f, rect.Top + (rect.Height - bmp1.Height) / 2.0f, bmp1.Width, bmp1.Height),
graphics.InterpolationMode = InterpolationMode.HighQualityBilinear; new RectangleF(0, 0, bmp1.Width, bmp1.Height),
graphics.DrawRotateString(text, font, color, center, angle, xOffset, yOffSet); GraphicsUnit.Pixel);
graphics.TextRenderingHint = TextRenderingHint.SystemDefault; }
graphics.InterpolationMode = InterpolationMode.Default;
private static Bitmap RotateTransform(Bitmap originalImage, int angle)
{
// 创建新的位图,大小为旋转后的图片大小
Bitmap rotatedImage = new Bitmap(originalImage.Height, originalImage.Width);
// 创建绘图对象
using Graphics g = Graphics.FromImage(rotatedImage);
// 设置绘图参数将原始图片旋转90°
g.TranslateTransform(originalImage.Width / 2, originalImage.Height / 2);
g.RotateTransform(angle);
g.DrawImage(originalImage,
new Rectangle(-originalImage.Width / 2, -originalImage.Height / 2, originalImage.Width, originalImage.Height),
new Rectangle(0, 0, rotatedImage.Width, originalImage.Height),
GraphicsUnit.Pixel);
g.TranslateTransform(-originalImage.Width / 2, -originalImage.Height / 2);
return rotatedImage;
} }
/// <summary> /// <summary>
@ -148,7 +159,7 @@ namespace Sunny.UI
/// <param name="top">上</param> /// <param name="top">上</param>
/// <param name="xOffset">左右偏移</param> /// <param name="xOffset">左右偏移</param>
/// <param name="yOffSet">上下偏移</param> /// <param name="yOffSet">上下偏移</param>
public static void DrawFontImage(this Graphics graphics, int symbol, int symbolSize, Color color, private static void DrawFontImage(this Graphics graphics, int symbol, int symbolSize, Color color,
float left, float top, int xOffset = 0, int yOffSet = 0) float left, float top, int xOffset = 0, int yOffSet = 0)
{ {
Font font = GetFont(symbol, symbolSize); Font font = GetFont(symbol, symbolSize);