垂直渐变的Button

Posted belx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了垂直渐变的Button相关的知识,希望对你有一定的参考价值。

public partial class RoundButton : Button
    {
        Rectangle r;
        private Brush _myBrush = null;
        private Color _color1 = System.Drawing.Color.FromArgb(255, 255, 192);
        private Color _color2 = System.Drawing.Color.FromArgb(0, 0, 192);

        [Category("设置"), Description("渐变开始颜色")]
        public Color color1
        {
            get { return _color1; }
            set { _color1 = value; }
        }

        [Category("设置"), Description("渐变结束颜色")]
        public Color color2
        {
            get { return _color2; }
            set { _color2 = value; }
        }

        public void ButtonNew()
        {
            r = new Rectangle(0, 0, 150, 80);
            _myBrush = new LinearGradientBrush(r, _color1, _color2, LinearGradientMode.Vertical);
        }

        public Brush MyBrush
        {
            get { return _myBrush; }
            set { _myBrush = value; }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            r = new Rectangle(0, 0, this.Width, this.Height);
            _myBrush = new LinearGradientBrush(r, _color1, _color2, LinearGradientMode.Vertical);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            r = new Rectangle(0, 0, this.Width, this.Height);
            _myBrush = new LinearGradientBrush(r, _color1, _color2, LinearGradientMode.Vertical);
        }

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            r = new Rectangle(0, 0, this.Width, this.Height);
            _myBrush = new LinearGradientBrush(r, _color2, _color1, LinearGradientMode.Vertical);
        }

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);
            Graphics g = pevent.Graphics;
            g.FillRectangle(_myBrush, this.ClientRectangle);
            StringFormat strF = new StringFormat();
            strF.Alignment = StringAlignment.Center;
            strF.LineAlignment = StringAlignment.Center;
            g.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), this.ClientRectangle, strF);
        }
    }

 

以上是关于垂直渐变的Button的主要内容,如果未能解决你的问题,请参考以下文章

垂直渐变内的水平单元格渐变

界面色彩渐变效果的实现

标签颜色之间的交叉渐变随着时间的推移在tkinter中?

如何显示水平和垂直渐变分隔线?

如何使用边框半径和背景线性渐变创建元素 div 顶部垂直

片段着色器不会在 OpenGL GLSL 中创建像光一样的渐变