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
    }

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值