[C#类] 重写一个Button类

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Drawing;
 6 using System.Drawing.Drawing2D;
 7 using System.Windows.Forms;
 8 using System.ComponentModel;
 9 
10 namespace Control_list
11 {
12     public class MyButton:Button
13     {
14         Rectangle r;
15         private Brush _myBrush = null;
16         private Color _color1 = System.Drawing.Color.FromArgb(255, 255, 192);
17         private Color _color2 = System.Drawing.Color.FromArgb(0, 0, 192);
18         private Color color3;
19         private Color color4;
20 
21 
22         [Category("设置"),Description("渐变开始颜色")]
23         public Color color1
24         {
25             get { return _color1; }
26             set { _color1 = value; }
27         }
28 
29         [Category("设置"), Description("渐变结束颜色")]
30         public Color color2
31         {
32             get { return _color2; }
33             set { _color2 = value; }
34         }
35         public void ButtoonNew()
36         {
37             r = new Rectangle(0, 0, 150, 80);
38             _myBrush = new LinearGradientBrush(r, color1, color2, LinearGradientMode.Vertical);
39         }
40         public Brush MyBrush
41         {
42             get { return _myBrush; }
43             set { _myBrush = value; }
44         }
45 
46         protected override void OnResize(EventArgs e)
47         {
48             base.OnResize(e);
49             r = new Rectangle(0, 0, this.Width, this.Height);
50             MyBrush = new LinearGradientBrush(r, color1, color2, LinearGradientMode.Vertical);
51         }
52 
53         protected override void OnMouseLeave(EventArgs e)
54         {
55             base.OnMouseLeave(e);
56             r = new Rectangle(0, 0, this.Width, this.Height);
57             color1 = color3;
58             color2 = color4;
59             MyBrush = new LinearGradientBrush(r, color1, color2, LinearGradientMode.Vertical);
60         }
61 
62         protected override void OnMouseEnter(EventArgs e)
63         {
64             base.OnMouseEnter(e);
65             color3 = this.color1;
66             color4 = this.color2;
67             //color1 = System.Drawing.Color.FromArgb(255, 255, 136);
68             //color2 = Color.FromArgb(0, 0, 192);
69             r = new Rectangle(0, 0, this.Width, this.Height);
70             MyBrush = new LinearGradientBrush(r, color4, color3, LinearGradientMode.Vertical);
71         }
72 
73         protected override void OnPaint(PaintEventArgs pevent)
74         {
75             base.OnPaint(pevent);
76             Graphics g = pevent.Graphics;
77             g.FillRectangle(MyBrush, this.ClientRectangle);
78             StringFormat strF = new StringFormat();
79             strF.Alignment = StringAlignment.Center;
80             strF.LineAlignment = StringAlignment.Center;
81             g.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), this.ClientRectangle, strF);
82         }
83     }
84 }

原文地址:https://www.cnblogs.com/laoshuboke/p/6060866.html