码迷,mamicode.com
首页 > 其他好文 > 详细

垂直渐变的Button

时间:2018-06-20 10:16:59      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:protected   wing   int   ted   text   str   font   rom   string   

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

标签:protected   wing   int   ted   text   str   font   rom   string   

原文地址:https://www.cnblogs.com/belx/p/9202212.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!