1. 程式人生 > >C#自定義Button按鈕控件

C#自定義Button按鈕控件

pre span 用戶 gif mov 設置 color 繪制 tex

C#自定義Button按鈕控件

在實際開發中經常可以遇到有的控件並不一定可以滿足需要,因此需要自定義開發,這裏做了一個簡單的按鈕控件,特意帖上來,如有不足之處請見諒!

按鈕素材:

這裏截圖裏V2013的幾個圖標

技術分享圖片

截圖圖標如下,有需要的可以自行:images下載

技術分享圖片

Button按鈕控件設計

1.添加一個用戶自定義控件ButtonEX

自定義控件辦法可以自己繪制線條顏色等等,也可以采用已有的控件作為控件的一個組成部分,本例中使用後者

按鈕組成:lable標簽一個,Dock設置為填充

技術分享圖片

2.添加控件屬性

  1  /// <summary>
  2
/// 控件的默認圖片 3 /// </summary> 4 private Image _imageDefault = null; 5 6 [Description("控件的默認圖片")] 7 public Image ImageDefault 8 { 9 get { return _imageDefault; } 10 set 11 { 12 _imageDefault = value;
13 label.Image = _imageDefault; 14 } 15 } 16 /// <summary> 17 /// 光標移動到控件上方顯示的圖片 18 /// </summary> 19 private Image _imageMove = null; 20 [Description("光標移動到控件上方顯示的圖片")] 21 public Image ImageMove 22
{ 23 get { return _imageMove; } 24 set { _imageMove = value; } 25 } 26 /// <summary> 27 /// 光標離開控件顯示的圖片 28 /// </summary> 29 private Image _imageLeave = null; 30 [Description("光標離開控件顯示的圖片")] 31 public Image ImageLeave 32 { 33 get { return _imageLeave; } 34 set { _imageLeave = value; } 35 } 36 /// <summary> 37 /// 控件的背景色 38 /// </summary> 39 private Color _backColorEX = Color.Transparent; 40 41 [Description("控件的背景色")] 42 public Color BackColorEX 43 { 44 get { return _backColorEX; } 45 set 46 { 47 _backColorEX = value; 48 label.BackColor = _backColorEX; 49 } 50 } 51 52 /// <summary> 53 /// 鼠標移動到控件上方顯示的顏色 54 /// </summary> 55 private Color backColorMove = Color.Transparent; 56 [Description("鼠標移動到控件上方顯示的顏色")] 57 public Color BackColorMove 58 { 59 get { return backColorMove; } 60 set { backColorMove = value; } 61 } 62 /// <summary> 63 /// 鼠標離開控件顯示的背景色 64 /// </summary> 65 private Color backColorLeave = Color.Transparent; 66 [Description("鼠標離開控件顯示的背景色")] 67 public Color BackColorLeave 68 { 69 get { return backColorLeave; } 70 set { backColorLeave = value; } 71 } 72 /// <summary> 73 /// 控件的文字顯示 74 /// </summary> 75 private string textEX = ""; 76 [Description("顯示的文字")] 77 public string TextEX 78 { 79 get { return textEX; } 80 set 81 { 82 textEX = value; 83 label.Text = textEX; 84 } 85 } 86 /// <summary> 87 /// 文字的顏色 88 /// </summary> 89 private Color textColor = Color.Black; 90 [Description("文字的顏色")] 91 public Color TextColor 92 { 93 get { return textColor; } 94 set 95 { 96 textColor = value; 97 label.ForeColor = textColor; 98 } 99 } 100 /// <summary> 101 /// 用於顯示文本的字體 102 /// </summary> 103 private Font fontM = new System.Drawing.Font("宋體", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 104 [Description("用於顯示文本的字體")] 105 public Font FontM 106 { 107 get { return fontM; } 108 set 109 { 110 fontM = value; 111 label.Font = fontM; 112 } 113 } 114

3.添加事件

1         /// <summary>
2         /// 鼠標單擊事件
3         /// </summary>
4         public event EventHandler ButtonClick;    

4.添加鼠標響應事件

 1  /// <summary>
 2         /// 鼠標單擊事件
 3         /// </summary>
 4         /// <param name="sender"></param>
 5         /// <param name="e"></param>
 6         private void label_Click(object sender, EventArgs e)
 7         {
 8             if (ButtonClick != null)
 9             {
10                 ButtonClick(sender, e);
11             }
12         }
13 
14         /// <summary>
15         /// 鼠標移動到控件上顯示的背景色和背景圖
16         /// </summary>
17         /// <param name="sender"></param>
18         /// <param name="e"></param>
19         private void label_MouseMove(object sender, MouseEventArgs e)
20         {
21             if (backColorMove != Color.Transparent)
22             {
23                 BackColorEX = backColorMove;
24             }
25             if (_imageMove != null)
26             {
27                 _imageDefault = _imageMove;
28             }
29         }
30 
31         /// <summary>
32         /// 鼠標離開控件後顯示的背景色和背景圖
33         /// </summary>
34         /// <param name="sender"></param>
35         /// <param name="e"></param>
36         private void label_MouseLeave(object sender, EventArgs e)
37         {
38             if (backColorLeave != Color.Transparent)
39             {
40                 BackColorEX = backColorLeave;
41             }
42             if (_imageLeave != null)
43             {
44                 _imageDefault = _imageLeave;
45             }
46         }

按鈕效果:

技術分享圖片

C#自定義Button按鈕控件