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

This commit is contained in:
Sunny 2023-10-25 14:55:12 +08:00
parent b0ab6079f6
commit 8e3f95d961
2 changed files with 36 additions and 37 deletions

View File

@ -71,6 +71,26 @@ namespace Sunny.UI
} }
} }
private int _symbolRotate = 0;
/// <summary>
/// 字体图标旋转角度
/// </summary>
[DefaultValue(0)]
[Description("字体图标旋转角度"), Category("SunnyUI")]
public int SymbolRotate
{
get => _symbolRotate;
set
{
if (_symbolRotate != value)
{
_symbolRotate = value;
Invalidate();
}
}
}
private Color symbolColor = Color.White; private Color symbolColor = Color.White;
/// <summary> /// <summary>
@ -379,7 +399,7 @@ namespace Sunny.UI
{ {
if (Symbol > 0 && Image == null) if (Symbol > 0 && Image == null)
{ {
e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(), ClientRectangle, SymbolOffset.X, SymbolOffset.Y); e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(), ClientRectangle, SymbolOffset.X, SymbolOffset.Y, SymbolRotate);
} }
if (Image != null) if (Image != null)
@ -396,7 +416,7 @@ namespace Sunny.UI
if (Symbol > 0 && Image == null) if (Symbol > 0 && Image == null)
{ {
e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(), e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(),
new RectangleF((Width - allWidth) / 2.0f, 0, ImageSize.Width, Height), SymbolOffset.X, SymbolOffset.Y); new RectangleF((Width - allWidth) / 2.0f, 0, ImageSize.Width, Height), SymbolOffset.X, SymbolOffset.Y, SymbolRotate);
} }
if (Image != null) if (Image != null)
@ -466,7 +486,7 @@ namespace Sunny.UI
if (Symbol > 0 && Image == null) if (Symbol > 0 && Image == null)
{ {
e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(), e.Graphics.DrawFontImage(Symbol, SymbolSize, GetSymbolForeColor(),
new Rectangle((int)left, (int)top, (int)ImageSize.Width, (int)ImageSize.Height), SymbolOffset.X, SymbolOffset.Y); new Rectangle((int)left, (int)top, (int)ImageSize.Width, (int)ImageSize.Height), SymbolOffset.X, SymbolOffset.Y, SymbolRotate);
} }
if (Image != null) if (Image != null)

View File

@ -109,43 +109,22 @@ 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(), float left = rect.Left + ((rect.Width - sf.Width) / 2.0f).RoundEx();
rect.Top + ((rect.Height - sf.Height) / 2.0f).RoundEx() + 1, xOffset, yOffSet); float top = rect.Top + ((rect.Height - sf.Height) / 2.0f).RoundEx();
} //graphics.DrawFontImage(symbol, symbolSize, color, left, top + 1, xOffset, yOffSet);
public static void DrawFontImage(this Graphics graphics, int symbol, int symbolSize, Color color, int angle, // 把画板的原点(默认是左上角)定位移到文字中心
RectangleF rect, int xOffset = 0, int yOffSet = 0) graphics.TranslateTransform(left + sf.Width / 2, top + sf.Height / 2);
{ // 旋转画板
SizeF sf = graphics.GetFontImageSize(symbol, symbolSize); graphics.RotateTransform(angle);
using Bitmap bmp = new Bitmap(symbolSize, symbolSize); // 回退画板x,y轴移动过的距离
using Graphics g = Graphics.FromImage(bmp); graphics.TranslateTransform(-(left + sf.Width / 2), -(top + sf.Height / 2));
g.DrawFontImage(symbol, symbolSize, color, (symbolSize - sf.Width) / 2.0f, (symbolSize - sf.Height) / 2.0f);
using Bitmap bmp1 = RotateTransform(bmp, angle); graphics.DrawFontImage(symbol, symbolSize, color, left, top, xOffset, yOffSet);
graphics.DrawImage(angle == 0 ? bmp : bmp1,
new RectangleF(rect.Left + (rect.Width - bmp1.Width) / 2.0f, rect.Top + (rect.Height - bmp1.Height) / 2.0f, bmp1.Width, bmp1.Height),
new RectangleF(0, 0, bmp1.Width, bmp1.Height),
GraphicsUnit.Pixel);
}
private static Bitmap RotateTransform(Bitmap originalImage, int angle) graphics.TranslateTransform(left + sf.Width / 2, top + sf.Height / 2);
{ graphics.RotateTransform(-angle);
// 创建新的位图,大小为旋转后的图片大小 graphics.TranslateTransform(-(left + sf.Width / 2), -(top + sf.Height / 2));
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>