* UILine:增加线的样式,支持透明背景

This commit is contained in:
Sunny 2022-01-05 12:20:27 +08:00
parent 54ad9892e8
commit b639e6478d
5 changed files with 117 additions and 70 deletions

Binary file not shown.

Binary file not shown.

View File

@ -46,6 +46,7 @@ namespace Sunny.UI.Demo
//
this.uiLine11.Direction = Sunny.UI.UILine.LineDirection.Vertical;
this.uiLine11.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLine11.LineDashStyle = Sunny.UI.UILineDashStyle.DashDotDot;
this.uiLine11.LineSize = 2;
this.uiLine11.Location = new System.Drawing.Point(578, 55);
this.uiLine11.MinimumSize = new System.Drawing.Size(16, 16);
@ -59,6 +60,7 @@ namespace Sunny.UI.Demo
//
this.uiLine10.Direction = Sunny.UI.UILine.LineDirection.Vertical;
this.uiLine10.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLine10.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine10.Location = new System.Drawing.Point(521, 55);
this.uiLine10.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine10.Name = "uiLine10";
@ -70,6 +72,7 @@ namespace Sunny.UI.Demo
// uiLine7
//
this.uiLine7.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLine7.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine7.LineSize = 3;
this.uiLine7.Location = new System.Drawing.Point(30, 413);
this.uiLine7.MinimumSize = new System.Drawing.Size(16, 16);
@ -82,6 +85,7 @@ namespace Sunny.UI.Demo
// uiLine8
//
this.uiLine8.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine8.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine8.LineSize = 2;
this.uiLine8.Location = new System.Drawing.Point(30, 378);
this.uiLine8.MinimumSize = new System.Drawing.Size(16, 16);
@ -94,6 +98,7 @@ namespace Sunny.UI.Demo
// uiLine9
//
this.uiLine9.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLine9.LineDashStyle = Sunny.UI.UILineDashStyle.DashDot;
this.uiLine9.Location = new System.Drawing.Point(30, 340);
this.uiLine9.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine9.Name = "uiLine9";
@ -105,6 +110,7 @@ namespace Sunny.UI.Demo
// uiLine4
//
this.uiLine4.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLine4.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine4.LineSize = 3;
this.uiLine4.Location = new System.Drawing.Point(30, 265);
this.uiLine4.MinimumSize = new System.Drawing.Size(16, 16);
@ -117,6 +123,7 @@ namespace Sunny.UI.Demo
// uiLine5
//
this.uiLine5.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine5.LineDashStyle = Sunny.UI.UILineDashStyle.Dot;
this.uiLine5.LineSize = 2;
this.uiLine5.Location = new System.Drawing.Point(30, 230);
this.uiLine5.MinimumSize = new System.Drawing.Size(16, 16);
@ -128,6 +135,7 @@ namespace Sunny.UI.Demo
// uiLine6
//
this.uiLine6.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLine6.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine6.Location = new System.Drawing.Point(30, 192);
this.uiLine6.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine6.Name = "uiLine6";
@ -139,6 +147,7 @@ namespace Sunny.UI.Demo
// uiLine3
//
this.uiLine3.Font = new System.Drawing.Font("微软雅黑", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLine3.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine3.LineSize = 3;
this.uiLine3.Location = new System.Drawing.Point(30, 113);
this.uiLine3.MinimumSize = new System.Drawing.Size(16, 16);
@ -151,6 +160,7 @@ namespace Sunny.UI.Demo
// uiLine2
//
this.uiLine2.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine2.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine2.LineSize = 2;
this.uiLine2.Location = new System.Drawing.Point(30, 78);
this.uiLine2.MinimumSize = new System.Drawing.Size(16, 16);
@ -163,6 +173,7 @@ namespace Sunny.UI.Demo
// uiLine1
//
this.uiLine1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.uiLine1.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine1.Location = new System.Drawing.Point(30, 55);
this.uiLine1.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine1.Name = "uiLine1";

View File

@ -22,4 +22,15 @@ namespace Sunny.UI
Triangle,
Circle
}
public enum UILineDashStyle
{
Solid,
Dash,
Dot,
DashDot,
DashDotDot,
Custom,
None
}
}

View File

@ -17,6 +17,7 @@
* : 2020-01-01
*
* 2020-01-01: V2.2.0
* 2022-01-05: V3.0.9 线
******************************************************************************/
using System;
@ -138,13 +139,95 @@ namespace Sunny.UI
}
}
private int textInterval = 10;
[DefaultValue(10)]
[Description("文字边距间隔"), Category("SunnyUI")]
public int TextInterval
{
get => textInterval;
set
{
textInterval = value;
Invalidate();
}
}
protected override void OnPaintRect(Graphics g, GraphicsPath path)
{
g.SetDefaultQuality();
}
protected override void OnPaintFore(Graphics g, GraphicsPath path)
{
if (string.IsNullOrEmpty(Text)) return;
SizeF sf = g.MeasureString(Text, Font);
float x = 0;
Pen pen = new Pen(rectColor, lineSize);
if (LineDashStyle != UILineDashStyle.None)
{
pen.DashStyle = (DashStyle)((int)LineDashStyle);
}
if (Direction == LineDirection.Horizontal)
{
switch (TextAlign)
{
case ContentAlignment.BottomLeft:
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height + lineSize) / 2.0f);
break;
case ContentAlignment.MiddleLeft:
x = Padding.Left + TextInterval;
g.DrawString(Text, Font, foreColor, Padding.Left + TextInterval + 2, (Height - sf.Height) / 2);
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:
x = (Width - sf.Width) / 2 - 2;
g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height - sf.Height) / 2);
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:
x = Width - sf.Width - TextInterval - 4 - Padding.Right;
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height - sf.Height) / 2);
break;
case ContentAlignment.TopRight:
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2 - Padding.Right, (Height - lineSize) / 2.0f - sf.Height);
break;
}
int top = (Height - lineSize) / 2;
g.FillRectangle(rectColor, Padding.Left + 1, top, Width - 2 - Padding.Left - Padding.Right, lineSize);
switch (TextAlign)
{
case ContentAlignment.MiddleLeft:
case ContentAlignment.MiddleCenter:
case ContentAlignment.MiddleRight:
g.DrawLine(pen, Padding.Left, top, x, top);
g.DrawLine(pen, x + sf.Width + 2, top, Width - 2 - Padding.Left - Padding.Right, top);
break;
default:
g.DrawLine(pen, Padding.Left, top, Width - 2 - Padding.Left - Padding.Right, top);
break;
}
switch (startCap)
{
case UILineCap.Square:
@ -181,85 +264,27 @@ namespace Sunny.UI
else
{
int left = (Width - lineSize) / 2;
g.FillRectangle(rectColor, left, Padding.Top, lineSize, Height - Padding.Top - Padding.Bottom);
g.DrawLine(pen, left, Padding.Top, left, Height - Padding.Top - Padding.Bottom);
}
pen.Dispose();
}
private int textInterval = 10;
[DefaultValue(10)]
[Description("文字边距间隔"), Category("SunnyUI")]
public int TextInterval
UILineDashStyle lineDashStyle = UILineDashStyle.None;
[Description("线的样式"), Category("SunnyUI")]
[DefaultValue("None")]
public UILineDashStyle LineDashStyle
{
get => textInterval;
get => lineDashStyle;
set
{
textInterval = value;
Invalidate();
}
}
protected override void OnPaintFore(Graphics g, GraphicsPath path)
{
if (string.IsNullOrEmpty(Text)) return;
SizeF sf = g.MeasureString(Text, Font);
if (Direction == LineDirection.Horizontal)
{
switch (TextAlign)
if (lineDashStyle != value)
{
case ContentAlignment.BottomLeft:
g.DrawString(Text, Font, foreColor, TextInterval + 2, (Height + lineSize) / 2.0f);
break;
case ContentAlignment.MiddleLeft:
g.FillRectangle(fillColor, TextInterval, 0, sf.Width + 3, Height);
g.DrawString(Text, Font, foreColor, TextInterval + 2, (Height - sf.Height) / 2);
break;
case ContentAlignment.TopLeft:
g.DrawString(Text, Font, foreColor, 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:
g.FillRectangle(fillColor, (Width - sf.Width) / 2 - 2, 0, sf.Width + 3, Height);
g.DrawString(Text, Font, foreColor, (Width - sf.Width) / 2, (Height - sf.Height) / 2);
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, (Height + lineSize) / 2.0f);
break;
case ContentAlignment.MiddleRight:
g.FillRectangle(fillColor, Width - sf.Width - TextInterval - 4, 0, sf.Width + 3, Height);
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2, (Height - sf.Height) / 2);
break;
case ContentAlignment.TopRight:
g.DrawString(Text, Font, foreColor, Width - sf.Width - TextInterval - 2, (Height - lineSize) / 2.0f - sf.Height);
break;
lineDashStyle = value;
Invalidate();
}
}
// if (Direction == LineDirection.Vertical)
// {
// StringFormat format = new StringFormat();
// format.FormatFlags = StringFormatFlags.DirectionVertical;
// g.DrawString(Text, Font, Brushes.Black, 15, 5, format);
// }
}
/// <summary>