* 重构DrawString函数
This commit is contained in:
parent
368f0c4498
commit
ac5d36c744
@ -120,62 +120,6 @@ namespace Sunny.UI
|
||||
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>
|
||||
|
@ -154,7 +154,7 @@ namespace Sunny.UI
|
||||
|
||||
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)
|
||||
@ -173,7 +173,7 @@ namespace Sunny.UI
|
||||
g.DrawRoundRectangle(rectColor, rect, Radius);
|
||||
if (ShowValue && canShow)
|
||||
{
|
||||
g.DrawString(processText, Font, fillColor, Size, Padding, TextAlign);
|
||||
DrawString(g, processText, Font, fillColor, Size, Padding, TextAlign);
|
||||
}
|
||||
|
||||
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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user