* 重构DrawString函数
This commit is contained in:
parent
e566bfe82b
commit
d5ec9b16d2
@ -82,6 +82,56 @@ namespace Sunny.UI
|
||||
TextRenderer.DrawText(g, text, font, new Point(left, top), color);
|
||||
}
|
||||
|
||||
public static void DrawString(this Graphics g, string text, Font font, Color color, Color backColor, Rectangle rect, ContentAlignment alignment, int offsetX = 0, int offsetY = 0)
|
||||
{
|
||||
if (text.IsNullOrEmpty()) return;
|
||||
rect.Offset(offsetX, offsetY);
|
||||
Size size = TextRenderer.MeasureText(text, font);
|
||||
int left = 0, top = 0;
|
||||
|
||||
switch (alignment)
|
||||
{
|
||||
case ContentAlignment.TopLeft:
|
||||
case ContentAlignment.MiddleLeft:
|
||||
case ContentAlignment.BottomLeft:
|
||||
left = rect.Left + 1;
|
||||
break;
|
||||
case ContentAlignment.TopCenter:
|
||||
case ContentAlignment.MiddleCenter:
|
||||
case ContentAlignment.BottomCenter:
|
||||
left = rect.Left + (rect.Width - size.Width) / 2;
|
||||
break;
|
||||
case ContentAlignment.TopRight:
|
||||
case ContentAlignment.MiddleRight:
|
||||
case ContentAlignment.BottomRight:
|
||||
left = rect.Left + rect.Width - size.Width - 1;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (alignment)
|
||||
{
|
||||
case ContentAlignment.TopLeft:
|
||||
case ContentAlignment.TopCenter:
|
||||
case ContentAlignment.TopRight:
|
||||
top = rect.Top + 1;
|
||||
break;
|
||||
case ContentAlignment.MiddleLeft:
|
||||
case ContentAlignment.MiddleCenter:
|
||||
case ContentAlignment.MiddleRight:
|
||||
|
||||
top = rect.Top + (rect.Height - size.Height) / 2;
|
||||
break;
|
||||
case ContentAlignment.BottomCenter:
|
||||
case ContentAlignment.BottomLeft:
|
||||
case ContentAlignment.BottomRight:
|
||||
top = rect.Top + rect.Height - size.Height - 1;
|
||||
break;
|
||||
}
|
||||
|
||||
TextRenderer.DrawText(g, text, font, new Point(left, top), color, backColor);
|
||||
}
|
||||
|
||||
|
||||
public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment, int offsetX = 0, int offsetY = 0)
|
||||
{
|
||||
if (text.IsNullOrEmpty()) return;
|
||||
@ -118,6 +168,42 @@ namespace Sunny.UI
|
||||
TextRenderer.DrawText(g, text, font, new Point(left, top), color);
|
||||
}
|
||||
|
||||
public static void DrawString(this Graphics g, string text, Font font, Color color, Color backColor, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment, int offsetX = 0, int offsetY = 0)
|
||||
{
|
||||
if (text.IsNullOrEmpty()) return;
|
||||
rect.Offset(offsetX, offsetY);
|
||||
Size size = TextRenderer.MeasureText(text, font);
|
||||
int left = 0, top = 0;
|
||||
|
||||
switch (alignment)
|
||||
{
|
||||
case StringAlignment.Near:
|
||||
left = rect.Left + 1;
|
||||
break;
|
||||
case StringAlignment.Center:
|
||||
left = rect.Left + (rect.Width - size.Width) / 2;
|
||||
break;
|
||||
case StringAlignment.Far:
|
||||
left = rect.Left + rect.Width - size.Width - 1;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (lineAlignment)
|
||||
{
|
||||
case StringAlignment.Near:
|
||||
top = rect.Top + 1;
|
||||
break;
|
||||
case StringAlignment.Center:
|
||||
top = rect.Top + (rect.Height - size.Height) / 2;
|
||||
break;
|
||||
case StringAlignment.Far:
|
||||
top = rect.Top + rect.Height - size.Height - 1;
|
||||
break;
|
||||
}
|
||||
|
||||
TextRenderer.DrawText(g, text, font, new Point(left, top), color, backColor);
|
||||
}
|
||||
|
||||
public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, HorizontalAlignment horizontalAlignment, int offsetX = 0, int offsetY = 0)
|
||||
{
|
||||
StringAlignment alignment = StringAlignment.Center;
|
||||
@ -126,6 +212,14 @@ namespace Sunny.UI
|
||||
g.DrawString(text, font, color, rect, alignment, StringAlignment.Center, offsetX, offsetY);
|
||||
}
|
||||
|
||||
public static void DrawString(this Graphics g, string text, Font font, Color color, Color backColor, Rectangle rect, HorizontalAlignment horizontalAlignment, int offsetX = 0, int offsetY = 0)
|
||||
{
|
||||
StringAlignment alignment = StringAlignment.Center;
|
||||
if (horizontalAlignment == HorizontalAlignment.Left) alignment = StringAlignment.Near;
|
||||
if (horizontalAlignment == HorizontalAlignment.Right) alignment = StringAlignment.Far;
|
||||
g.DrawString(text, font, color, backColor, rect, alignment, StringAlignment.Center, offsetX, offsetY);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绘制字符串
|
||||
/// </summary>
|
||||
|
@ -19,6 +19,7 @@
|
||||
* 2020-01-01: V2.2.0 增加文件说明
|
||||
* 2020-04-25: V2.2.4 更新主题配置类
|
||||
* 2022-05-30: V3.1.9 修复Padding设置
|
||||
* 2022-05-13: V3.3.6 重构DrawString函数
|
||||
******************************************************************************/
|
||||
|
||||
using System;
|
||||
@ -66,17 +67,7 @@ namespace Sunny.UI
|
||||
/// <param name="path">绘图路径</param>
|
||||
protected override void OnPaintFore(Graphics g, GraphicsPath path)
|
||||
{
|
||||
SizeF sf = g.MeasureString(Text, Font);
|
||||
|
||||
float left = TitleInterval;
|
||||
if (TitleAlignment == HorizontalAlignment.Right)
|
||||
left = Width - TitleInterval - sf.Width;
|
||||
if (TitleAlignment == HorizontalAlignment.Center)
|
||||
left = (Width - sf.Width) / 2.0f;
|
||||
|
||||
float top = TitleTop - sf.Height / 2.0f;
|
||||
g.FillRectangle(FillColor, left - 2, top, sf.Width + 2, sf.Height);
|
||||
g.DrawString(Text, Font, ForeColor, left, top);
|
||||
g.DrawString(Text, Font, ForeColor, FillColor, new Rectangle(TitleInterval, 0, Width - TitleInterval * 2, TitleTop * 2), TitleAlignment);
|
||||
}
|
||||
|
||||
private int _titleTop = 16;
|
||||
|
@ -24,6 +24,7 @@
|
||||
* 2021-12-07: V3.0.9 更改图片自动刷新
|
||||
* 2022-01-02: V3.0.9 增加角标
|
||||
* 2022-03-19: V3.1.1 重构主题配色
|
||||
* 2022-05-13: V3.3.6 重构DrawString函数
|
||||
******************************************************************************/
|
||||
|
||||
using System;
|
||||
@ -625,7 +626,8 @@ namespace Sunny.UI
|
||||
case TextImageRelation.TextAboveImage:
|
||||
{
|
||||
#region 文本在上
|
||||
e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Padding.Top);
|
||||
//e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Padding.Top);
|
||||
e.Graphics.DrawString(Text, Font, color, new Rectangle(0, Padding.Top, Width, Height), ContentAlignment.TopCenter);
|
||||
|
||||
//字体图标
|
||||
if (Symbol > 0 && Image == null)
|
||||
@ -678,14 +680,16 @@ namespace Sunny.UI
|
||||
e.Graphics.DrawImage(Image, ImageTop, (Height - ImageSize.Height) / 2.0f, ImageSize.Width, ImageSize.Height);
|
||||
}
|
||||
|
||||
e.Graphics.DrawString(Text, Font, color, Width - Padding.Right - sf.Width, (Height - sf.Height) / 2);
|
||||
//e.Graphics.DrawString(Text, Font, color, Width - Padding.Right - sf.Width, (Height - sf.Height) / 2);
|
||||
e.Graphics.DrawString(Text, Font, color, new Rectangle(0, 0, Width - Padding.Right, Height), ContentAlignment.MiddleRight);
|
||||
#endregion
|
||||
}
|
||||
break;
|
||||
case TextImageRelation.TextBeforeImage:
|
||||
{
|
||||
#region 文本在前
|
||||
e.Graphics.DrawString(Text, Font, color, Padding.Left, (Height - sf.Height) / 2);
|
||||
//e.Graphics.DrawString(Text, Font, color, Padding.Left, (Height - sf.Height) / 2);
|
||||
e.Graphics.DrawString(Text, Font, color, new Rectangle(Padding.Left, 0, Width, Height), ContentAlignment.MiddleLeft);
|
||||
|
||||
//字体图标
|
||||
if (Symbol > 0 && Image == null)
|
||||
@ -738,7 +742,8 @@ namespace Sunny.UI
|
||||
e.Graphics.DrawImage(Image, (Width - ImageSize.Width) / 2.0f, ImageTop, ImageSize.Width, ImageSize.Height);
|
||||
}
|
||||
|
||||
e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Height - Padding.Bottom - sf.Height);
|
||||
//e.Graphics.DrawString(Text, Font, color, (Width - sf.Width) / 2, Height - Padding.Bottom - sf.Height);
|
||||
e.Graphics.DrawString(Text, Font, color, new Rectangle(0, 0, Width, Height - Padding.Bottom), ContentAlignment.BottomCenter);
|
||||
#endregion
|
||||
}
|
||||
break;
|
||||
@ -748,11 +753,11 @@ namespace Sunny.UI
|
||||
{
|
||||
e.Graphics.SetHighQuality();
|
||||
sf = e.Graphics.MeasureString(TipsText, TempFont);
|
||||
float sfMax = Math.Max(sf.Width, sf.Height);
|
||||
float x = Width - 1 - 2 - sfMax;
|
||||
float y = 1 + 1;
|
||||
e.Graphics.FillEllipse(TipsColor, x, y, sfMax, sfMax);
|
||||
e.Graphics.DrawString(TipsText, TempFont, TipsForeColor, x + sfMax / 2.0f - sf.Width / 2.0f, y + sfMax / 2.0f - sf.Height / 2.0f);
|
||||
int sfMax = (int)Math.Max(sf.Width, sf.Height) + 1;
|
||||
int x = Width - 1 - 2 - sfMax;
|
||||
int y = 1 + 1;
|
||||
e.Graphics.FillEllipse(TipsColor, x - 1, y, sfMax, sfMax);
|
||||
e.Graphics.DrawString(TipsText, TempFont, TipsForeColor, new Rectangle(x, y, sfMax, sfMax), ContentAlignment.MiddleCenter);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user