+ UISmoothLabel: 平滑文字带边框的标签

This commit is contained in:
Sunny 2022-01-22 22:42:20 +08:00
parent dd15808c62
commit 4d46f8a92f
5 changed files with 337 additions and 1 deletions

Binary file not shown.

Binary file not shown.

View File

@ -45,6 +45,10 @@ namespace Sunny.UI.Demo
this.uiLabel3 = new Sunny.UI.UILabel();
this.uiLabel4 = new Sunny.UI.UILabel();
this.uiLine5 = new Sunny.UI.UILine();
this.label1 = new System.Windows.Forms.Label();
this.uiSmoothLabel1 = new Sunny.UI.UISmoothLabel();
this.uiSmoothLabel2 = new Sunny.UI.UISmoothLabel();
this.uiLine6 = new Sunny.UI.UILine();
this.SuspendLayout();
//
// uiMarkLabel4
@ -89,6 +93,7 @@ namespace Sunny.UI.Demo
// uiLine4
//
this.uiLine4.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine4.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine4.Location = new System.Drawing.Point(30, 208);
this.uiLine4.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine4.Name = "uiLine4";
@ -137,6 +142,7 @@ namespace Sunny.UI.Demo
// uiLine3
//
this.uiLine3.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine3.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine3.Location = new System.Drawing.Point(30, 133);
this.uiLine3.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine3.Name = "uiLine3";
@ -148,6 +154,7 @@ namespace Sunny.UI.Demo
// uiLine2
//
this.uiLine2.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine2.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine2.Location = new System.Drawing.Point(381, 55);
this.uiLine2.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine2.Name = "uiLine2";
@ -159,6 +166,7 @@ namespace Sunny.UI.Demo
// uiLine1
//
this.uiLine1.Font = new System.Drawing.Font("微软雅黑", 12F);
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";
@ -231,6 +239,7 @@ namespace Sunny.UI.Demo
// uiLine5
//
this.uiLine5.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine5.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine5.Location = new System.Drawing.Point(30, 290);
this.uiLine5.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine5.Name = "uiLine5";
@ -239,11 +248,54 @@ namespace Sunny.UI.Demo
this.uiLine5.Text = "UILabel旋转角度";
this.uiLine5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(522, 171);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(55, 21);
this.label1.TabIndex = 52;
this.label1.Text = "label1";
//
// uiSmoothLabel1
//
this.uiSmoothLabel1.Font = new System.Drawing.Font("微软雅黑", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.uiSmoothLabel1.Location = new System.Drawing.Point(381, 313);
this.uiSmoothLabel1.Name = "uiSmoothLabel1";
this.uiSmoothLabel1.Size = new System.Drawing.Size(258, 66);
this.uiSmoothLabel1.TabIndex = 53;
this.uiSmoothLabel1.Text = "SunnyUI";
//
// uiSmoothLabel2
//
this.uiSmoothLabel2.Font = new System.Drawing.Font("Jokerman", 36F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.uiSmoothLabel2.Location = new System.Drawing.Point(381, 386);
this.uiSmoothLabel2.Name = "uiSmoothLabel2";
this.uiSmoothLabel2.Size = new System.Drawing.Size(284, 83);
this.uiSmoothLabel2.TabIndex = 54;
this.uiSmoothLabel2.Text = "SunnyUI";
//
// uiLine6
//
this.uiLine6.Font = new System.Drawing.Font("微软雅黑", 12F);
this.uiLine6.LineDashStyle = Sunny.UI.UILineDashStyle.None;
this.uiLine6.Location = new System.Drawing.Point(381, 290);
this.uiLine6.MinimumSize = new System.Drawing.Size(16, 16);
this.uiLine6.Name = "uiLine6";
this.uiLine6.Size = new System.Drawing.Size(319, 20);
this.uiLine6.TabIndex = 55;
this.uiLine6.Text = "UISmoothLabel";
this.uiLine6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// FLabel
//
this.AllowShowTitle = true;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.ClientSize = new System.Drawing.Size(800, 450);
this.ClientSize = new System.Drawing.Size(800, 504);
this.Controls.Add(this.uiLine6);
this.Controls.Add(this.uiSmoothLabel2);
this.Controls.Add(this.uiSmoothLabel1);
this.Controls.Add(this.label1);
this.Controls.Add(this.uiLine5);
this.Controls.Add(this.uiLabel4);
this.Controls.Add(this.uiLabel3);
@ -287,5 +339,9 @@ namespace Sunny.UI.Demo
private UILabel uiLabel3;
private UILabel uiLabel4;
private UILine uiLine5;
private System.Windows.Forms.Label label1;
private UISmoothLabel uiSmoothLabel1;
private UISmoothLabel uiSmoothLabel2;
private UILine uiLine6;
}
}

View File

@ -0,0 +1,272 @@
/******************************************************************************
* SunnyUI
* CopyRight (C) 2012-2022 ShenYongHua().
* QQ群56829229 QQ17612584 EMailSunnyUI@QQ.Com
*
* Blog: https://www.cnblogs.com/yhuse
* Gitee: https://gitee.com/yhuse/SunnyUI
* GitHub: https://github.com/yhuse/SunnyUI
*
* SunnyUI.dll can be used for free under the GPL-3.0 license.
* If you use this code, please keep this note.
* 使
******************************************************************************
* : UISmoothLabel.cs
* :
* : V3.1
* : 2022-01-22
*
* 2022-01-22: V3.1.0
******************************************************************************/
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Sunny.UI
{
[ToolboxItem(true)]
[DefaultEvent("Click")]
[DefaultProperty("Text")]
public class UISmoothLabel : Label, IStyleInterface
{
public UISmoothLabel()
{
base.Font = UIFontColor.Font(36);
Version = UIGlobal.Version;
drawPath = new GraphicsPath();
drawPen = new Pen(new SolidBrush(rectColor), rectSize);
forecolorBrush = new SolidBrush(ForeColor);
Size = new Size(300, 60);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
forecolorBrush?.Dispose();
drawPath?.Dispose();
drawPen?.Dispose();
}
base.Dispose(disposing);
}
private PointF point;
private SizeF drawSize;
private Pen drawPen;
private GraphicsPath drawPath;
private SolidBrush forecolorBrush;
[Browsable(false), DefaultValue(false)]
public bool IsScaled { get; set; }
public void SetDPIScale()
{
if (!IsScaled)
{
this.SetDPIScaleFont();
IsScaled = true;
}
}
private Color foreColor = UIStyles.GetStyleColor(UIStyle.Blue).LabelForeColor;
// <summary>
/// Tag字符串
/// </summary>
[DefaultValue(null)]
[Description("获取或设置包含有关控件的数据的对象字符串"), Category("SunnyUI")]
public string TagString { get; set; }
/// <summary>
/// 字体颜色
/// </summary>
[Description("字体颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "White")]
public override Color ForeColor
{
get => foreColor;
set
{
foreColor = value;
forecolorBrush.Color = foreColor;
_style = UIStyle.Custom;
Invalidate();
}
}
public string Version { get; }
public void SetStyle(UIStyle style)
{
UIBaseStyle uiColor = UIStyles.GetStyleColor(style);
if (!uiColor.IsCustom()) SetStyleColor(uiColor);
_style = style;
}
/// <summary>
/// 自定义主题风格
/// </summary>
[DefaultValue(false)]
[Description("获取或设置可以自定义主题风格"), Category("SunnyUI")]
public bool StyleCustomMode { get; set; }
public virtual void SetStyleColor(UIBaseStyle uiColor)
{
foreColor = uiColor.ButtonForeColor;
forecolorBrush.Color = foreColor;
rectColor = uiColor.ButtonFillColor;
if (rectSize != 0) drawPen.Color = rectColor;
Invalidate();
}
private UIStyle _style = UIStyle.Blue;
/// <summary>
/// 主题样式
/// </summary>
[DefaultValue(UIStyle.Blue), Description("主题样式"), Category("SunnyUI")]
public UIStyle Style
{
get => _style;
set => SetStyle(value);
}
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
Invalidate();
}
protected override void OnTextAlignChanged(EventArgs e)
{
base.OnTextAlignChanged(e);
Invalidate();
}
protected Color rectColor = UIStyles.GetStyleColor(UIStyle.Blue).RectColor;
/// <summary>
/// 边框颜色
/// </summary>
[Description("边框颜色"), Category("SunnyUI")]
[DefaultValue(typeof(Color), "80, 160, 255")]
public Color RectColor
{
get
{
return rectColor;
}
set
{
if (rectColor != value)
{
rectColor = value;
if (rectSize != 0) drawPen.Color = rectColor;
RectColorChanged?.Invoke(this, null);
_style = UIStyle.Custom;
Invalidate();
}
}
}
public event EventHandler RectColorChanged;
private int rectSize = 2;
/// <summary>
/// 边框颜色
/// </summary>
[Description("边框宽度"), Category("SunnyUI")]
[DefaultValue(2)]
public int RectSize
{
get => rectSize;
set
{
value = Math.Max(0, value);
if (rectSize != value)
{
rectSize = value;
if (value == 0)
{
drawPen.Color = Color.Transparent;
}
else
{
drawPen.Color = RectColor;
drawPen.Width = value;
}
Invalidate();
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
if (Text.IsNullOrEmpty()) return;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
drawSize = e.Graphics.MeasureString(Text, Font, new PointF(), StringFormat.GenericTypographic);
if (AutoSize)
{
point.X = Padding.Left;
point.Y = Padding.Top;
}
else
{
if (TextAlign == ContentAlignment.TopLeft ||
TextAlign == ContentAlignment.MiddleLeft ||
TextAlign == ContentAlignment.BottomLeft)
{
point.X = Padding.Left;
}
else if (TextAlign == ContentAlignment.TopCenter ||
TextAlign == ContentAlignment.MiddleCenter ||
TextAlign == ContentAlignment.BottomCenter)
{
point.X = (Width - drawSize.Width) / 2;
}
else
{
point.X = Width - (Padding.Right + drawSize.Width);
}
if (TextAlign == ContentAlignment.TopLeft ||
TextAlign == ContentAlignment.TopCenter ||
TextAlign == ContentAlignment.TopRight)
{
point.Y = Padding.Top;
}
else if (TextAlign == ContentAlignment.MiddleLeft ||
TextAlign == ContentAlignment.MiddleCenter ||
TextAlign == ContentAlignment.MiddleRight)
{
point.Y = (Height - drawSize.Height) / 2;
}
else
{
point.Y = Height - (Padding.Bottom + drawSize.Height);
}
}
float fontSize = e.Graphics.DpiY * Font.SizeInPoints / 72;
drawPath.Reset();
drawPath.AddString(Text, Font.FontFamily, (int)Font.Style, fontSize,
point, StringFormat.GenericTypographic);
e.Graphics.FillPath(forecolorBrush, drawPath);
e.Graphics.DrawPath(drawPen, drawPath);
}
}
}

View File

@ -711,6 +711,14 @@ namespace Sunny.UI
return new Font("微软雅黑", FontSize, FontStyle.Regular, GraphicsUnit.Point, GdiCharSet);
}
/// <summary>
/// 默认字体
/// </summary>
public static Font Font(float fontSize)
{
return new Font("微软雅黑", fontSize, FontStyle.Regular, GraphicsUnit.Point, GdiCharSet);
}
public static float FontSize = 12;
/// <summary>