* 重构DrawString函数

This commit is contained in:
Sunny 2023-05-07 18:57:36 +08:00
parent 39b482356d
commit ca67c3bd28
4 changed files with 75 additions and 40 deletions

View File

@ -33,36 +33,74 @@ namespace Sunny.UI
/// </summary> /// </summary>
public static class GraphicsEx public static class GraphicsEx
{ {
/// <summary> public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, ContentAlignment alignment = ContentAlignment.MiddleCenter)
/// 绘制字符串
/// </summary>
/// <param name="g">绘图图元</param>
/// <param name="text">文字</param>
/// <param name="font">字体</param>
/// <param name="color">颜色</param>
/// <param name="rect">区域</param>
/// <param name="format">格式</param>
public static void DrawString(this Graphics g, string text, Font font, Color color, RectangleF rect, StringFormat format)
{ {
if (text.IsNullOrEmpty()) return; if (text.IsNullOrEmpty()) return;
using Brush br = color.Brush(); TextFormatFlags flags = new TextFormatFlags();
g.DrawString(text, font, br, rect, format); switch (alignment)
{
case ContentAlignment.TopLeft:
flags = TextFormatFlags.Top | TextFormatFlags.Left;
break;
case ContentAlignment.TopCenter:
flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.TopRight:
flags = TextFormatFlags.Top | TextFormatFlags.Right;
break;
case ContentAlignment.MiddleLeft:
flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
break;
case ContentAlignment.MiddleCenter:
flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.MiddleRight:
flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
break;
case ContentAlignment.BottomLeft:
flags = TextFormatFlags.Bottom | TextFormatFlags.Left;
break;
case ContentAlignment.BottomCenter:
flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
break;
case ContentAlignment.BottomRight:
flags = TextFormatFlags.Bottom | TextFormatFlags.Right;
break;
default:
flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
break;
}
TextRenderer.DrawText(g, text, font, rect, color, flags);
} }
/// <summary> public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, StringAlignment alignment, StringAlignment lineAlignment)
/// 绘制字符串
/// </summary>
/// <param name="g">绘图图元</param>
/// <param name="text">文字</param>
/// <param name="font">字体</param>
/// <param name="color">颜色</param>
/// <param name="rect">区域</param>
/// <param name="format">格式</param>
public static void DrawString(this Graphics g, string text, Font font, Color color, Rectangle rect, StringFormat format)
{ {
if (text.IsNullOrEmpty()) return; if (text.IsNullOrEmpty()) return;
using Brush br = color.Brush(); TextFormatFlags flags = new TextFormatFlags();
g.DrawString(text, font, br, rect, format);
if (alignment == StringAlignment.Near && lineAlignment == StringAlignment.Near)
flags = TextFormatFlags.Top | TextFormatFlags.Left;
if (alignment == StringAlignment.Center && lineAlignment == StringAlignment.Near)
flags = TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
if (alignment == StringAlignment.Far && lineAlignment == StringAlignment.Near)
flags = TextFormatFlags.Top | TextFormatFlags.Right;
if (alignment == StringAlignment.Near && lineAlignment == StringAlignment.Center)
flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
if (alignment == StringAlignment.Center && lineAlignment == StringAlignment.Center)
flags = TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
if (alignment == StringAlignment.Far && lineAlignment == StringAlignment.Center)
flags = TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
if (alignment == StringAlignment.Near && lineAlignment == StringAlignment.Far)
flags = TextFormatFlags.Bottom | TextFormatFlags.Left;
if (alignment == StringAlignment.Center && lineAlignment == StringAlignment.Far)
flags = TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
if (alignment == StringAlignment.Far && lineAlignment == StringAlignment.Far)
flags = TextFormatFlags.Bottom | TextFormatFlags.Right;
TextRenderer.DrawText(g, text, font, rect, color, flags);
} }
/// <summary> /// <summary>

View File

@ -149,7 +149,7 @@ namespace Sunny.UI
{ {
if (TextAngle.EqualsFloat(0)) if (TextAngle.EqualsFloat(0))
{ {
e.Graphics.DrawString(Text, Font, ForeColor, r, format); e.Graphics.DrawString(Text, Font, ForeColor, r, TextAlign);
} }
else else
{ {

View File

@ -300,10 +300,6 @@ namespace Sunny.UI
return; return;
} }
StringFormat sStringFormat = new StringFormat();
sStringFormat.LineAlignment = StringAlignment.Center;
sStringFormat.Alignment = textAlignment;
bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected; bool isSelected = (e.State & DrawItemState.Selected) == DrawItemState.Selected;
Color backColor = isSelected ? ItemSelectBackColor : BackColor; Color backColor = isSelected ? ItemSelectBackColor : BackColor;
Color foreColor = isSelected ? ItemSelectForeColor : ForeColor; Color foreColor = isSelected ? ItemSelectForeColor : ForeColor;
@ -315,7 +311,8 @@ namespace Sunny.UI
{ {
e.Graphics.FillRectangle(BackColor, e.Bounds); e.Graphics.FillRectangle(BackColor, e.Bounds);
e.Graphics.FillRectangle(backColor, rect); e.Graphics.FillRectangle(backColor, rect);
e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, sStringFormat); //e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, sStringFormat);
e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, textAlignment, StringAlignment.Center);
} }
else else
{ {
@ -333,7 +330,8 @@ namespace Sunny.UI
e.Graphics.FillRectangle(BackColor, e.Bounds); e.Graphics.FillRectangle(BackColor, e.Bounds);
e.Graphics.FillRectangle(backColor, rect); e.Graphics.FillRectangle(backColor, rect);
e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, sStringFormat); //e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, sStringFormat);
e.Graphics.DrawString(showText, e.Font, foreColor, e.Bounds, textAlignment, StringAlignment.Center);
} }
AfterDrawItem?.Invoke(this, Items, e); AfterDrawItem?.Invoke(this, Items, e);

View File

@ -756,27 +756,26 @@ namespace Sunny.UI
//显示Tips圆圈 //显示Tips圆圈
if (ShowTips && MenuHelper.GetTipsText(e.Node).IsValid()) if (ShowTips && MenuHelper.GetTipsText(e.Node).IsValid())
{ {
SizeF tipsSize = e.Graphics.MeasureString(MenuHelper.GetTipsText(e.Node), TempFont); Size tipsSize = TextRenderer.MeasureText(MenuHelper.GetTipsText(e.Node), TempFont);
float sfMax = Math.Max(tipsSize.Width, tipsSize.Height) + 1; int sfMax = Math.Max(tipsSize.Width, tipsSize.Height) + 1;
float tipsLeft = Width - sfMax - 16; int tipsLeft = Width - sfMax - 16;
if (e.Node.Nodes.Count > 0) tipsLeft -= 24; if (e.Node.Nodes.Count > 0) tipsLeft -= 24;
if (Bar.Visible) tipsLeft -= Bar.Width; if (Bar.Visible) tipsLeft -= Bar.Width;
if (TreeNodeSymbols.ContainsKey(e.Node)) tipsLeft -= TreeNodeSymbols[e.Node].Count * 30; if (TreeNodeSymbols.ContainsKey(e.Node)) tipsLeft -= TreeNodeSymbols[e.Node].Count * 30;
int tipsTop = e.Bounds.Y + (ItemHeight - sfMax) / 2;
float tipsTop = e.Bounds.Y + (ItemHeight - sfMax) / 2;
if (MenuHelper[e.Node] != null) if (MenuHelper[e.Node] != null)
{ {
using StringFormat alignment = GDI.SetCenterAlignment(); using StringFormat alignment = GDI.SetCenterAlignment();
if (MenuHelper[e.Node].TipsCustom) if (MenuHelper[e.Node].TipsCustom)
{ {
e.Graphics.FillEllipse(MenuHelper[e.Node].TipsBackColor, tipsLeft, tipsTop, sfMax, sfMax); e.Graphics.FillEllipse(MenuHelper[e.Node].TipsBackColor, tipsLeft - 1, tipsTop, sfMax, sfMax);
e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, MenuHelper[e.Node].TipsForeColor, new RectangleF(tipsLeft, tipsTop, sfMax, sfMax), alignment); e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, MenuHelper[e.Node].TipsForeColor, new Rectangle(tipsLeft, tipsTop, sfMax, sfMax));
} }
else else
{ {
e.Graphics.FillEllipse(TipsColor, tipsLeft, tipsTop, sfMax, sfMax); e.Graphics.FillEllipse(TipsColor, tipsLeft - 1, tipsTop, sfMax, sfMax);
e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, TipsForeColor, new RectangleF(tipsLeft, tipsTop, sfMax, sfMax), alignment); e.Graphics.DrawString(MenuHelper.GetTipsText(e.Node), TempFont, TipsForeColor, new Rectangle(tipsLeft, tipsTop, sfMax, sfMax));
} }
} }
} }