C#自定义控件(带角标并且可以改变边框的圆角按钮)

目录

前言

一、添加控件属性

1.添加角标部分

2.添加边框设置部分

3.添加圆角部分

二、添加控件功能

1.添加角标

2.添加边框绘制

3.全部代码

三、效果展示

1.初始状态

2.角标显示

3.显示边框

4.显示圆角

总结


前言

上面文章我们设计了一个圆角按钮,详见http://t.csdn.cn/RBc0d

现在我们在圆角按钮的基础上增加带角标并且可以改变边框的功能。


一、添加控件属性

1.添加角标部分

添加角标控件属性,“是否显示角标”,"角标文字","角标颜色",代码如下:

 /// <summary>
        /// 是否显示角标
        /// </summary>
        [Description("是否显示角标"), Category("自定义")]
        public bool IsShowTips
        {
            get
            {
                return lblTips.Visible;
            }
            set
            {
                lblTips.Visible = value;
                Refresh();
            }
        }

        /// <summary>
        /// 角标文字
        /// </summary>
        [Description("角标文字"), Category("自定义")]
        public string TipsText
        {
            get
            {
                return lblTips.Text;
            }
            set
            {
                lblTips.Text = value;
                Refresh();
            }
        }

    

        /// <summary>
        /// 角标颜色
        /// </summary>
        private Color m_tipsColor = Color.FromArgb(232, 30, 99);
        /// <summary>
        /// 角标颜色
        /// </summary>
        [Description("角标颜色"), Category("自定义")]
        public Color TipsColor
        {
            get { return m_tipsColor; }
            set
            {
                m_tipsColor = value;
                Refresh();
            }
        }

2.添加边框设置部分

添加边框设置属性,“是否显示边框”,"边框宽度","边框颜色",代码如下:


        /// <summary>
        /// 是否显示边框
        /// </summary>
        private bool _isShowRect = false;
        /// <summary>
        /// 是否显示边框
        /// </summary>
        [Description("是否显示边框"), Category("自定义")]
        public virtual bool IsShowRect
        {
            get
            {
                return _isShowRect;
            }
            set
            {
                _isShowRect = value;
                Refresh();
            }
        }
        /// <summary>
        /// 边框颜色
        /// </summary>
        private Color _rectColor = Color.FromArgb(0, 192, 0);//初始颜色为绿色
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Description("边框颜色"), Category("自定义")]
        public virtual Color RectColor
        {
            get
            {
                return _rectColor;
            }
            set
            {
                _rectColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// 边框宽度
        /// </summary>
        private int _rectWidth = 1;
        /// <summary>
        /// 边框宽度
        /// </summary>
        [Description("边框宽度"), Category("自定义")]
        public virtual int RectWidth
        {
            get
            {
                return _rectWidth;
            }
            set
            {
                _rectWidth = value;
                Refresh();
            }
        }

3.添加圆角部分

添加控件圆角部分属性,“是否圆角”,"圆角半径","按钮背景色","按钮字体颜色","按钮字体","按钮显示文字",代码如下:

  /// <summary>
        /// 是否圆角
        /// </summary>
        private bool _isRoundCorner = false;
        /// <summary>
        /// 是否圆角
        /// </summary>
        [Description("是否圆角"), Category("自定义")]
        public virtual bool IsRoundCorner
        {
            get
            {
                return _isRoundCorner;
            }
            set
            {
                _isRoundCorner = value;
                Refresh();
            }
        }
        /// <summary>
        /// 圆角半径
        /// </summary>
        private int _roundRadius = 24;
 
        /// <summary>
        /// 圆角半径
        /// </summary>
        [Description("圆角半径"), Category("自定义")]
        public virtual int RoundRadius
        {
            get
            {
                return _roundRadius;
            }
            set
            {
                if (value < 1)//圆角半径最小为1
                {
                    value = 1;
                }
                _roundRadius = value;
                Refresh();
            }
        }
        /// <summary>
        /// 按钮背景色
        /// </summary>
        private Color _btnBackColor = Color.Green;
        /// <summary>
        /// 按钮背景色
        /// </summary>
        [Description("按钮背景色"), Category("自定义")]
        public Color BtnBackColor
        {
            get { return _btnBackColor; }
            set
            {
                _btnBackColor = value;
                BackColor = value;
            }
        }
 
        /// <summary>
        /// 按钮字体颜色
        /// </summary>
        private Color _btnForeColor = Color.White;
        /// <summary>
        /// 按钮字体颜色
        /// </summary>
        [Description("按钮字体颜色"), Category("自定义")]
        public virtual Color BtnForeColor
        {
            get { return _btnForeColor; }
            set
            {
                _btnForeColor = value;
                lbl.ForeColor = value;
            }
        }
 
        /// <summary>
        /// 按钮字体
        /// </summary>
        private Font _btnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
        /// <summary>
        /// 按钮字体
        /// </summary>
        [Description("按钮字体"), Category("自定义")]
        public Font BtnFont
        {
            get { return _btnFont; }
            set
            {
                _btnFont = value;
                lbl.Font = value;
            }
        }
 
        /// <summary>
        /// 按钮文字
        /// </summary>
        private string _btnText;
        /// <summary>
        /// 按钮文字
        /// </summary>
        [Description("按钮文字"), Category("自定义")]
        public virtual string BtnText
        {
            get { return _btnText; }
            set
            {
                _btnText = value;
                lbl.Text = value;
                Refresh();
            }
        }

二、添加控件功能

1.添加角标

 在设计界面添加Label控件,并命名为lblTips,并添加Paint事件,代码如下:


        /// <summary>
        /// 角标的Paint事件
        /// </summary>
        void lblTips_Paint(object sender, PaintEventArgs e)
        {
            SetGDIHigh(e.Graphics);
            e.Graphics.FillEllipse(new SolidBrush(m_tipsColor), new Rectangle(0, 0, lblTips.Width - 1, lblTips.Height - 1));
            System.Drawing.SizeF sizeEnd = e.Graphics.MeasureString(TipsText, lblTips.Font);
            e.Graphics.DrawString(TipsText, lblTips.Font, new SolidBrush(lblTips.ForeColor), new PointF((lblTips.Width - sizeEnd.Width) / 2, (lblTips.Height - sizeEnd.Height) / 2 + 1));
        }
      /// <summary>
        /// 设置GDI高质量模式抗锯齿
        /// </summary>
        void SetGDIHigh(Graphics g)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高,即消除锯齿
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
        }

2.添加边框绘制

需要重写OnPaint事件,也包含圆角部分,代码如下:

  protected override void OnPaint(PaintEventArgs e)
        {
            if (Visible)
            {
                if (_isRoundCorner)
                {
                    PaintRoundCorner();
                }
                else
                {
                    //关闭圆角后显示为原矩形
                    GraphicsPath g = new GraphicsPath();
                    g.AddRectangle(base.ClientRectangle);//添加一个矩形
                    g.CloseFigure();//闭合当前图形并开始新的图形
                    base.Region = new Region(g);
                }

                GraphicsPath graphicsPath = new GraphicsPath();
                if (_isShowRect)
                {
                    Rectangle clientRectangle = base.ClientRectangle;
                    if (_isRoundCorner)
                    {
                        graphicsPath.AddArc(0, 0, _roundRadius, _roundRadius, 180f, 90f);
                        graphicsPath.AddArc(clientRectangle.Width - _roundRadius - 1, 0, _roundRadius, _roundRadius, 270f, 90f);
                        graphicsPath.AddArc(clientRectangle.Width - _roundRadius - 1, clientRectangle.Height - _roundRadius - 1, _roundRadius, _roundRadius, 0f, 90f);
                        graphicsPath.AddArc(0, clientRectangle.Height - _roundRadius - 1, _roundRadius, _roundRadius, 90f, 90f);
                        graphicsPath.CloseFigure();
                    }
                    else
                    {
                        graphicsPath.AddRectangle(clientRectangle);
                    }
                }
                SetGDIHigh(e.Graphics);
                if (_isShowRect)
                {
                    Color rectColor = _rectColor;
                    Pen pen = new Pen(rectColor, _rectWidth);
                    e.Graphics.DrawPath(pen, graphicsPath);
                }
            }
            base.OnPaint(e);
        }
        /// <summary>
        /// 绘制圆角
        /// </summary>
        private void PaintRoundCorner()
        {
            Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height);
            Rectangle rect2 = new Rectangle(rect.Location, new Size(_roundRadius, _roundRadius));
            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.AddArc(rect2, 180f, 90f);//左上角
            rect2.X = rect.Right - _roundRadius;
            graphicsPath.AddArc(rect2, 270f, 90f);//右上角
            rect2.Y = rect.Bottom - _roundRadius;
            rect2.Width += 1;
            rect2.Height += 1;
            graphicsPath.AddArc(rect2, 360f, 90f);//右下角           
            rect2.X = rect.Left;
            graphicsPath.AddArc(rect2, 90f, 90f);//左下角
            graphicsPath.CloseFigure();
            base.Region = new Region(graphicsPath);
        }

3.全部代码

全部代码如下:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace BtnX
{
    [Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
    public partial class BtnX : UserControl, IContainerControl
    {
        /// <summary>
        /// 是否圆角
        /// </summary>
        private bool _isRoundCorner = false;
        /// <summary>
        /// 是否圆角
        /// </summary>
        [Description("是否圆角"), Category("自定义")]
        public virtual bool IsRoundCorner
        {
            get
            {
                return _isRoundCorner;
            }
            set
            {
                _isRoundCorner = value;
                Refresh();
            }
        }
        /// <summary>
        /// 圆角半径
        /// </summary>
        private int _roundRadius = 24;

        /// <summary>
        /// 圆角半径
        /// </summary>
        [Description("圆角半径"), Category("自定义")]
        public virtual int RoundRadius
        {
            get
            {
                return _roundRadius;
            }
            set
            {
                if (value < 1)//圆角半径最小为1
                {
                    value = 1;
                }
                _roundRadius = value;
                Refresh();
            }
        }
        /// <summary>
        /// 按钮背景色
        /// </summary>
        private Color _btnBackColor = Color.Green;
        /// <summary>
        /// 按钮背景色
        /// </summary>
        [Description("按钮背景色"), Category("自定义")]
        public Color BtnBackColor
        {
            get { return _btnBackColor; }
            set
            {
                _btnBackColor = value;
                BackColor = value;
            }
        }

        /// <summary>
        /// 按钮字体颜色
        /// </summary>
        private Color _btnForeColor = Color.White;
        /// <summary>
        /// 按钮字体颜色
        /// </summary>
        [Description("按钮字体颜色"), Category("自定义")]
        public virtual Color BtnForeColor
        {
            get { return _btnForeColor; }
            set
            {
                _btnForeColor = value;
                lbl.ForeColor = value;
            }
        }

        /// <summary>
        /// 按钮字体
        /// </summary>
        private Font _btnFont = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, 134);
        /// <summary>
        /// 按钮字体
        /// </summary>
        [Description("按钮字体"), Category("自定义")]
        public Font BtnFont
        {
            get { return _btnFont; }
            set
            {
                _btnFont = value;
                lbl.Font = value;
            }
        }

        /// <summary>
        /// 按钮文字
        /// </summary>
        private string _btnText="自定义按钮";
        /// <summary>
        /// 按钮文字
        /// </summary>
        [Description("按钮文字"), Category("自定义")]
        public virtual string BtnText
        {
            get { return _btnText; }
            set
            {
                _btnText = value;
                lbl.Text = value;
                Refresh();
            }
        }



        /// <summary>
        /// 是否显示边框
        /// </summary>
        private bool _isShowRect = false;
        /// <summary>
        /// 是否显示边框
        /// </summary>
        [Description("是否显示边框"), Category("自定义")]
        public virtual bool IsShowRect
        {
            get
            {
                return _isShowRect;
            }
            set
            {
                _isShowRect = value;
                Refresh();
            }
        }
        /// <summary>
        /// 边框颜色
        /// </summary>
        private Color _rectColor = Color.FromArgb(0, 192, 0);//初始颜色为绿色
        /// <summary>
        /// 边框颜色
        /// </summary>
        [Description("边框颜色"), Category("自定义")]
        public virtual Color RectColor
        {
            get
            {
                return _rectColor;
            }
            set
            {
                _rectColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// 边框宽度
        /// </summary>
        private int _rectWidth = 1;
        /// <summary>
        /// 边框宽度
        /// </summary>
        [Description("边框宽度"), Category("自定义")]
        public virtual int RectWidth
        {
            get
            {
                return _rectWidth;
            }
            set
            {
                _rectWidth = value;
                Refresh();
            }
        }
     
        public BtnX()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.UserPaint, true);
            TabStop = false;
            lblTips.Paint += lblTips_Paint;
            lbl.MouseEnter += lbl_MouseEnter;
            lbl.MouseLeave += lbl_MouseLeave;
            lbl.MouseClick += lbl_MouseDown;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            if (Visible)
            {
                if (_isRoundCorner)
                {
                    PaintRoundCorner();
                }
                else
                {
                    //关闭圆角后显示为原矩形
                    GraphicsPath g = new GraphicsPath();
                    g.AddRectangle(base.ClientRectangle);//添加一个矩形
                    g.CloseFigure();//闭合当前图形并开始新的图形
                    base.Region = new Region(g);
                }

                GraphicsPath graphicsPath = new GraphicsPath();
                if (_isShowRect)
                {
                    Rectangle clientRectangle = base.ClientRectangle;
                    if (_isRoundCorner)
                    {
                        graphicsPath.AddArc(0, 0, _roundRadius, _roundRadius, 180f, 90f);
                        graphicsPath.AddArc(clientRectangle.Width - _roundRadius - 1, 0, _roundRadius, _roundRadius, 270f, 90f);
                        graphicsPath.AddArc(clientRectangle.Width - _roundRadius - 1, clientRectangle.Height - _roundRadius - 1, _roundRadius, _roundRadius, 0f, 90f);
                        graphicsPath.AddArc(0, clientRectangle.Height - _roundRadius - 1, _roundRadius, _roundRadius, 90f, 90f);
                        graphicsPath.CloseFigure();
                    }
                    else
                    {
                        graphicsPath.AddRectangle(clientRectangle);
                    }
                }
                SetGDIHigh(e.Graphics);
                if (_isShowRect)
                {
                    Color rectColor = _rectColor;
                    Pen pen = new Pen(rectColor, _rectWidth);
                    e.Graphics.DrawPath(pen, graphicsPath);
                }
            }
            base.OnPaint(e);
        }
        /// <summary>
        /// 绘制圆角
        /// </summary>
        private void PaintRoundCorner()
        {
            Rectangle rect = new Rectangle(-1, -1, base.Width + 1, base.Height);
            Rectangle rect2 = new Rectangle(rect.Location, new Size(_roundRadius, _roundRadius));
            GraphicsPath graphicsPath = new GraphicsPath();
            graphicsPath.AddArc(rect2, 180f, 90f);//左上角
            rect2.X = rect.Right - _roundRadius;
            graphicsPath.AddArc(rect2, 270f, 90f);//右上角
            rect2.Y = rect.Bottom - _roundRadius;
            rect2.Width += 1;
            rect2.Height += 1;
            graphicsPath.AddArc(rect2, 360f, 90f);//右下角           
            rect2.X = rect.Left;
            graphicsPath.AddArc(rect2, 90f, 90f);//左下角
            graphicsPath.CloseFigure();
            base.Region = new Region(graphicsPath);
        }
        /// <summary>
        /// 设置GDI高质量模式抗锯齿
        /// </summary>
        void SetGDIHigh(Graphics g)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;  //使绘图质量最高,即消除锯齿
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
        }

        /// <summary>
        /// 是否显示角标
        /// </summary>
        [Description("是否显示角标"), Category("自定义")]
        public bool IsShowTips
        {
            get
            {
                return lblTips.Visible;
            }
            set
            {
                lblTips.Visible = value;
                Refresh();
            }
        }

        /// <summary>
        /// 角标文字
        /// </summary>
        [Description("角标文字"), Category("自定义")]
        public string TipsText
        {
            get
            {
                return lblTips.Text;
            }
            set
            {
                lblTips.Text = value;
                Refresh();
            }
        }

    

        /// <summary>
        /// 角标颜色
        /// </summary>
        private Color m_tipsColor = Color.FromArgb(232, 30, 99);
        /// <summary>
        /// 角标颜色
        /// </summary>
        [Description("角标颜色"), Category("自定义")]
        public Color TipsColor
        {
            get { return m_tipsColor; }
            set
            {
                m_tipsColor = value;
                Refresh();
            }
        }

        /// <summary>
        /// 按钮点击事件
        /// </summary>
        [Description("按钮点击事件"), Category("自定义")]
        public event EventHandler BtnClick;
        Color m_cacheColor = Color.Empty;

        void lbl_MouseLeave(object sender, EventArgs e)
        {

            if (m_cacheColor != Color.Empty)
            {
                BtnBackColor = m_cacheColor;
                m_cacheColor = Color.Empty;
            }

        }

        void lbl_MouseEnter(object sender, EventArgs e)
        {
            if (BtnBackColor != Color.Empty && BtnBackColor != null)
            {
                m_cacheColor = BtnBackColor;
                BtnBackColor = ChangeColor(BtnBackColor, -0.2f);
            }
        }

        /// <summary>
        /// 改变颜色
        /// </summary>
        Color ChangeColor(Color color, float value)
        {
            float red = color.R;
            float green = color.G;
            float blue = color.B;

            if (value < 0)
            {
                value = 1 + value;
                red *= value;
                green *= value;
                blue *= value;
            }
            else
            {
                red = (255 - red) * value + red;
                green = (255 - green) * value + green;
                blue = (255 - blue) * value + blue;
            }

            if (red < 0) red = 0;

            if (red > 255) red = 255;

            if (green < 0) green = 0;

            if (green > 255) green = 255;

            if (blue < 0) blue = 0;

            if (blue > 255) blue = 255;



            return Color.FromArgb(color.A, (int)red, (int)green, (int)blue);
        }

        /// <summary>
        /// 角标的Paint事件
        /// </summary>
        void lblTips_Paint(object sender, PaintEventArgs e)
        {
            SetGDIHigh(e.Graphics);
            e.Graphics.FillEllipse(new SolidBrush(m_tipsColor), new Rectangle(0, 0, lblTips.Width - 1, lblTips.Height - 1));
            System.Drawing.SizeF sizeEnd = e.Graphics.MeasureString(TipsText, lblTips.Font);
            e.Graphics.DrawString(TipsText, lblTips.Font, new SolidBrush(lblTips.ForeColor), new PointF((lblTips.Width - sizeEnd.Width) / 2, (lblTips.Height - sizeEnd.Height) / 2 + 1));
        }

        /// <summary>
        /// 单击事件
        /// </summary>
        private void lbl_MouseDown(object sender, MouseEventArgs e)
        {
            if (BtnClick != null)
                BtnClick(this, e);
        }


    }
}

三、效果展示

1.初始状态

2.角标显示

3.显示边框

 

4.显示圆角

这样我们就用C#在圆角按钮的基础上增加带角标并且可以改变边框的功能。

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值