* 重构DrawString函数

This commit is contained in:
Sunny 2023-05-12 15:23:32 +08:00
parent bd4824a2c0
commit 8b124e2beb

View File

@ -21,12 +21,14 @@
* 2022-01-10: V3.1.0 * 2022-01-10: V3.1.0
* 2022-03-19: V3.1.1 * 2022-03-19: V3.1.1
* 2022-11-26: V3.2.9 线 * 2022-11-26: V3.2.9 线
* 2022-05-12: V3.3.6 DrawString函数
******************************************************************************/ ******************************************************************************/
using System; using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Drawing.Drawing2D; using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Sunny.UI namespace Sunny.UI
{ {
@ -190,51 +192,34 @@ namespace Sunny.UI
{ {
if (Text.IsValid()) if (Text.IsValid())
{ {
sf = g.MeasureString(Text, Font); sf = TextRenderer.MeasureText(Text, Font);
switch (TextAlign) switch (TextAlign)
{ {
case ContentAlignment.BottomLeft: case ContentAlignment.TopLeft:
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height + lineSize) / 2.0f); g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, 0, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, (Height - lineSize) / 2), ContentAlignment.BottomLeft); break;
break; case ContentAlignment.TopCenter:
g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, 0, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, (Height - lineSize) / 2), ContentAlignment.BottomCenter); break;
case ContentAlignment.TopRight:
g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, 0, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, (Height - lineSize) / 2), ContentAlignment.BottomRight); break;
case ContentAlignment.MiddleLeft: case ContentAlignment.MiddleLeft:
x = Padding.Left + TextInterval; x = Padding.Left + TextInterval;
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height - sf.Height) / 2); g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, 0, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, Height), TextAlign); break;
break;
case ContentAlignment.TopLeft:
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height - lineSize) / 2.0f - sf.Height);
break;
case ContentAlignment.BottomCenter:
g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height + lineSize) / 2.0f);
break;
case ContentAlignment.MiddleCenter: case ContentAlignment.MiddleCenter:
x = (Width - sf.Width) / 2 - 2; x = (Width - sf.Width) / 2 - 2;
g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height - sf.Height) / 2); g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, 0, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, Height), TextAlign); break;
break;
case ContentAlignment.TopCenter:
g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height - lineSize) / 2.0f - sf.Height);
break;
case ContentAlignment.BottomRight:
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height + lineSize) / 2.0f);
break;
case ContentAlignment.MiddleRight: case ContentAlignment.MiddleRight:
x = Width - sf.Width - TextInterval - 4 - Padding.Right; x = Width - sf.Width - TextInterval - 4 - Padding.Right;
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height - sf.Height) / 2); g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, 0, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, Height), TextAlign); break;
break; case ContentAlignment.BottomLeft:
g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, (Height + lineSize) / 2, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, Height), ContentAlignment.TopLeft); break;
case ContentAlignment.TopRight: case ContentAlignment.BottomCenter:
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height - lineSize) / 2.0f - sf.Height); g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, (Height + lineSize) / 2, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, Height), ContentAlignment.TopCenter); break;
break; case ContentAlignment.BottomRight:
g.DrawString(Text, Font, foreColor, new Rectangle(Padding.Left + TextInterval + 2, (Height + lineSize) / 2, Width - Padding.Left - textInterval - 2 - Padding.Right - textInterval - 2, Height), ContentAlignment.TopRight); break;
} }
} }
int top = (Height - lineSize) / 2; int top = Height / 2;
if (Text.IsValid()) if (Text.IsValid())
{ {
switch (TextAlign) switch (TextAlign)
@ -248,6 +233,7 @@ namespace Sunny.UI
default: default:
if (LineColorGradient) if (LineColorGradient)
{ {
top = (Height - lineSize) / 2;
using LinearGradientBrush br = new LinearGradientBrush(new Point(0, 0), new Point(Width, 0), LineColor, LineColor2); using LinearGradientBrush br = new LinearGradientBrush(new Point(0, 0), new Point(Width, 0), LineColor, LineColor2);
g.FillRectangle(br, new Rectangle(Padding.Left, top, Width - 2 - Padding.Left - Padding.Right, LineSize)); g.FillRectangle(br, new Rectangle(Padding.Left, top, Width - 2 - Padding.Left - Padding.Right, LineSize));
} }
@ -262,6 +248,7 @@ namespace Sunny.UI
{ {
if (LineColorGradient) if (LineColorGradient)
{ {
top = (Height - lineSize) / 2;
using LinearGradientBrush br = new LinearGradientBrush(new Point(0, 0), new Point(Width, 0), LineColor, LineColor2); using LinearGradientBrush br = new LinearGradientBrush(new Point(0, 0), new Point(Width, 0), LineColor, LineColor2);
g.FillRectangle(br, new Rectangle(Padding.Left, top, Width - 2 - Padding.Left - Padding.Right, LineSize)); g.FillRectangle(br, new Rectangle(Padding.Left, top, Width - 2 - Padding.Left - Padding.Right, LineSize));
} }