本文主要是介绍C#封装带有圆角弧度的Label控件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
效果图:
public partial class RoundLabel : Label
{
Color _borderColor = Color.Blue;
int _radian;
#region 自定义属性
[DefaultValue(typeof(Color), "240, 240, 240"), Description("边框颜色")]
public Color BorderColor
{
get
{
return _borderColor;
}
set
{
_borderColor = value;
base.Invalidate();
}
}
[DefaultValue(typeof(int), "10"), Description("圆角弧度大小")]
public int Radian
{
get
{
return _radian;
}
set
{
_radian = value;
base.Invalidate();
}
}
#endregion
public RoundLabel()
{
InitializeComponent();
this.SetStyle(ControlStyles.UserPaint
| ControlStyles.DoubleBuffer
| ControlStyles.ResizeRedraw
| ControlStyles.AllPaintingInWmPaint
| ControlStyles.OptimizedDoubleBuffer
| ControlStyles.SupportsTransparentBackColor
, true);
}
#region 重写基类事件
protected override void OnCreateControl()
{
base.OnCreateControl();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Rectangle rect = this.DisplayRectangle;
rect.Width -= 1;
rect.Height -= 1;
e.Graphics.DrawPath(new Pen(this._borderColor), CreatePath(rect, _radian, false));
}
#endregion
#region 实现圆角及绘制边框事件
GraphicsPath CreatePath(Rectangle rect, int radius, bool correction)
{
GraphicsPath path = new GraphicsPath();
if (radius <= 0)
{
path.AddRectangle(rect);
}
else
{
path.AddArc(rect.X, rect.Y, radius, radius, 180, 90);
path.AddArc(rect.Right - radius, rect.Y, radius, radius, 270, 90);
path.AddArc(rect.Right - radius, rect.Bottom - radius, radius, radius, 0, 90);
path.AddArc(rect.X, rect.Bottom - radius, radius, radius, 90, 90);
}
path.CloseFigure();
return path;
}
private void DrawBorder(Graphics g, Rectangle rect, int radius)
{
rect.Width -= 1;
rect.Height -= 1;
using (GraphicsPath path = CreatePath(rect, radius, false))
{
using (Pen pen = new Pen(this.BorderColor))
{
g.DrawPath(pen, path);
}
}
}
#endregion
}
这篇关于C#封装带有圆角弧度的Label控件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!