C# 圆角按钮

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Web;
using System.Drawing.Drawing2D;

namespace 自定义圆角按钮
{
    public partial class UserControl1 : UserControl
    {
        private bool _isRoundCorner = false;
        [Description("是否圆角"), Category("自定义")]
        public virtual bool IsRoundCorner
        {
            get { return _isRoundCorner; }
            set
            {
                _isRoundCorner = value;
                Refresh();
            }
        }

        private int _roundRadius = 24;
        [Description("圆角半径"), Category("自定义")]
        public virtual int RoundRadius
        {
            get { return _roundRadius; }
            set
            {
                if (value < 1) value = -1;
                _roundRadius = value;
                Refresh();
            }
        }

        private Color _btnBackColor = Color.Green;

        [Description("按钮背景色"), Category("自定义")]
        public Color BtnBackColor
        {
            get { return _btnBackColor; }
            set
            {
                _btnBackColor = value;
                BackColor = value;
            }
        }

        private Color _btnForeColor = Color.White;

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

        private Font _btnFont = new Font("微软雅黑", 12f, FontStyle.Regular, GraphicsUnit.Point, 134);

        [Description("按钮字体"), Category("自定义")]
        public Font BtnFont
        {
            get { return _btnFont;}
            set
            {
                _btnFont = value;
                label1.Font = value;
            }
        }

        private string _btnText;

        [Description("按钮文字"),Category("自定义")]
        public virtual string BtnText
        {
            get { return _btnText; }
            set
            {
                _btnText = value;
                label1.Text = value;
                Refresh();
            }
        }
        public UserControl1()
        {
            InitializeComponent();

            SetStyle(ControlStyles.AllPaintingInWmPaint | 
                     ControlStyles.OptimizedDoubleBuffer |
                     ControlStyles.ResizeRedraw |
                     ControlStyles.Selectable |
                     ControlStyles.SupportsTransparentBackColor |
                     ControlStyles.UserPaint,true);
            TabStop = false;
            label1.MouseEnter += label1_MouseEnter;
            label1.MouseLeave += label1_MoseLeave;
            label1.MouseDown += label1_MouseDown;
        }

        [Description("按钮点击事件"), Category("自定义")]
        public event EventHandler BtnClick;
        Color m_cacheColor = Color.Empty;
        private void label1_MouseDown(object sender, MouseEventArgs e)
        {
            if(BtnClick != null)
            {
                BtnClick(this, e);
            }
        }

        private void label1_MoseLeave(object sender, EventArgs e)
        {
            if (m_cacheColor != Color.Empty)
            {
                BtnBackColor = m_cacheColor;
                m_cacheColor = Color.Empty;
            }
        }

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

        private 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);
        }

        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();
                }
            }
            base.OnPaint(e);
        }
        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);
        }
        private void SetGDIHigh(Graphics g)
        {
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
        }
    }
}

记录一下,以后估计用的上在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值