* 重构DrawString函数

This commit is contained in:
Sunny 2023-05-08 22:49:39 +08:00
parent 368f0c4498
commit ac5d36c744
2 changed files with 48 additions and 58 deletions

View File

@ -120,62 +120,6 @@ namespace Sunny.UI
g.DrawString(text, font, br, x, y); g.DrawString(text, font, br, x, y);
} }
/// <summary>
/// 绘制字符串
/// </summary>
/// <param name="g">绘图图元</param>
/// <param name="str">字符串</param>
/// <param name="font">字体</param>
/// <param name="color">颜色</param>
/// <param name="size">大小</param>
/// <param name="padding">边距</param>
/// <param name="align">位置位置</param>
public static void DrawString(this Graphics g, string str, Font font, Color color, Size size, Padding padding, ContentAlignment align, int offsetX = 0, int offsetY = 0)
{
if (str.IsNullOrEmpty()) return;
SizeF sf = g.MeasureString(str, font);
using Brush br = color.Brush();
switch (align)
{
case ContentAlignment.MiddleCenter:
g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f + offsetX,
padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f + offsetY);
break;
case ContentAlignment.TopLeft:
g.DrawString(str, font, br, padding.Left + offsetX, padding.Top + offsetY);
break;
case ContentAlignment.TopCenter:
g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f + offsetX, padding.Top + offsetY);
break;
case ContentAlignment.TopRight:
g.DrawString(str, font, br, size.Width - sf.Width - padding.Right + offsetX, padding.Top + offsetY);
break;
case ContentAlignment.MiddleLeft:
g.DrawString(str, font, br, padding.Left + offsetX, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f + offsetY);
break;
case ContentAlignment.MiddleRight:
g.DrawString(str, font, br, size.Width - sf.Width - padding.Right + offsetX, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f + offsetY);
break;
case ContentAlignment.BottomLeft:
g.DrawString(str, font, br, padding.Left + offsetX, size.Height - sf.Height - padding.Bottom + offsetY);
break;
case ContentAlignment.BottomCenter:
g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f + offsetX, size.Height - sf.Height - padding.Bottom + offsetY);
break;
case ContentAlignment.BottomRight:
g.DrawString(str, font, br, size.Width - sf.Width - padding.Right + offsetX, size.Height - sf.Height - padding.Bottom + offsetY);
break;
}
}
/// <summary> /// <summary>
/// 以文字中心点为原点,旋转文字 /// 以文字中心点为原点,旋转文字
/// </summary> /// </summary>

View File

@ -154,7 +154,7 @@ namespace Sunny.UI
if (ShowValue && canShow) if (ShowValue && canShow)
{ {
e.Graphics.DrawString(processText, Font, foreColor, Size, Padding, TextAlign); DrawString(e.Graphics, processText, Font, foreColor, Size, Padding, TextAlign);
} }
if (image == null || image.Width != Width || image.Height != Height || imageRadius != Radius) if (image == null || image.Width != Width || image.Height != Height || imageRadius != Radius)
@ -173,7 +173,7 @@ namespace Sunny.UI
g.DrawRoundRectangle(rectColor, rect, Radius); g.DrawRoundRectangle(rectColor, rect, Radius);
if (ShowValue && canShow) if (ShowValue && canShow)
{ {
g.DrawString(processText, Font, fillColor, Size, Padding, TextAlign); DrawString(g, processText, Font, fillColor, Size, Padding, TextAlign);
} }
g.Dispose(); g.Dispose();
@ -194,6 +194,52 @@ namespace Sunny.UI
} }
} }
private void DrawString(Graphics g, string str, Font font, Color color, Size size, Padding padding, ContentAlignment align, int offsetX = 0, int offsetY = 0)
{
if (str.IsNullOrEmpty()) return;
SizeF sf = g.MeasureString(str, font);
using Brush br = color.Brush();
switch (align)
{
case ContentAlignment.MiddleCenter:
g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f + offsetX,
padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f + offsetY);
break;
case ContentAlignment.TopLeft:
g.DrawString(str, font, br, padding.Left + offsetX, padding.Top + offsetY);
break;
case ContentAlignment.TopCenter:
g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f + offsetX, padding.Top + offsetY);
break;
case ContentAlignment.TopRight:
g.DrawString(str, font, br, size.Width - sf.Width - padding.Right + offsetX, padding.Top + offsetY);
break;
case ContentAlignment.MiddleLeft:
g.DrawString(str, font, br, padding.Left + offsetX, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f + offsetY);
break;
case ContentAlignment.MiddleRight:
g.DrawString(str, font, br, size.Width - sf.Width - padding.Right + offsetX, padding.Top + (size.Height - sf.Height - padding.Top - padding.Bottom) / 2.0f + offsetY);
break;
case ContentAlignment.BottomLeft:
g.DrawString(str, font, br, padding.Left + offsetX, size.Height - sf.Height - padding.Bottom + offsetY);
break;
case ContentAlignment.BottomCenter:
g.DrawString(str, font, br, padding.Left + (size.Width - sf.Width - padding.Left - padding.Right) / 2.0f + offsetX, size.Height - sf.Height - padding.Bottom + offsetY);
break;
case ContentAlignment.BottomRight:
g.DrawString(str, font, br, size.Width - sf.Width - padding.Right + offsetX, size.Height - sf.Height - padding.Bottom + offsetY);
break;
}
}
/// <summary> /// <summary>
/// 重载控件尺寸变更 /// 重载控件尺寸变更
/// </summary> /// </summary>