Winform自定义控件—Button(上)
在开发中用的最多的相信就是Button控件,但是Button本身是在是太丑陋了,自己还背景图还会产生"黑线",为了使用方便,并且美观,我们采用迂回的方式来实现Button的效果。
在这里使用UserControl+Label进行封装
先来看代码:
ButtonM.cs
1 using System; 2 using System.ComponentModel; 3 using System.Drawing; 4 using System.Windows.Forms; 5 6 namespace landptf.controls 7 { 8 public partial class ButtonM : UserControl 9 { 10 11 public ButtonM() 12 { 13 InitializeComponent(); 14 } 15 /// <summary> 16 /// 控件的默认图片 17 /// </summary> 18 private Image imageM = null; 19 [Description("控件的默认图片")] 20 public Image ImageM 21 { 22 get { return imageM; } 23 set 24 { 25 imageM = value; 26 label.Image = imageM; 27 } 28 } 29 /// <summary> 30 /// 光标移动到控件上方显示的图片 31 /// </summary> 32 private Image imageMove = null; 33 [Description("光标移动到控件上方显示的图片")] 34 public Image ImageMove 35 { 36 get { return imageMove; } 37 set { imageMove = value; } 38 } 39 /// <summary> 40 /// 光标离开控件显示的图片 41 /// </summary> 42 private Image imageLeave = null; 43 [Description("光标离开控件显示的图片")] 44 public Image ImageLeave 45 { 46 get { return imageLeave; } 47 set { imageLeave = value; } 48 } 49 /// <summary> 50 /// 控件的背景色 51 /// </summary> 52 private Color backColorM = Color.Transparent; 53 [Description("控件的背景色")] 54 public Color BackColorM 55 { 56 get { return backColorM; } 57 set 58 { 59 backColorM = value; 60 label.BackColor = backColorM; 61 } 62 } 63 /// <summary> 64 /// 光标移动到控件上方显示的颜色 65 /// </summary> 66 private Color backColorMove = Color.Transparent; 67 [Description("光标移动到控件上方显示的颜色")] 68 public Color BackColorMove 69 { 70 get { return backColorMove; } 71 set { backColorMove = value; } 72 } 73 /// <summary> 74 /// 光标离开控件显示的背景色 75 /// </summary> 76 private Color backColorLeave = Color.Transparent; 77 [Description("光标离开控件显示的背景色")] 78 public Color BackColorLeave 79 { 80 get { return backColorLeave; } 81 set { backColorLeave = value; } 82 } 83 /// <summary> 84 /// 控件的文字提示 85 /// </summary> 86 private string textM = ""; 87 [Description("显示的文字")] 88 public string TextM 89 { 90 get { return textM; } 91 set 92 { 93 textM = value; 94 label.Text = textM; 95 } 96 } 97 /// <summary> 98 /// 文字的颜色 99 /// </summary> 100 private Color textColor = Color.Black; 101 [Description("文字的颜色")] 102 public Color TextColor 103 { 104 get { return textColor; } 105 set 106 { 107 textColor = value; 108 label.ForeColor = textColor; 109 } 110 } 111 /// <summary> 112 /// 用于显示文本的字体 113 /// </summary> 114 private Font fontM = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 115 [Description("用于显示文本的字体")] 116 public Font FontM 117 { 118 get { return fontM; } 119 set 120 { 121 fontM = value; 122 label.Font = fontM; 123 } 124 } 125 /// <summary> 126 /// 单击事件 127 /// </summary> 128 public event EventHandler ButtonClick; 129 /// <summary> 130 /// 单击事件 131 /// </summary> 132 /// <param name="sender"></param> 133 /// <param name="e"></param> 134 private void label_Click(object sender, EventArgs e) 135 { 136 if (ButtonClick != null) 137 { 138 ButtonClick(sender, e); 139 } 140 } 141 142 private void label_MouseMove(object sender, MouseEventArgs e) 143 { 144 if (backColorMove != Color.Transparent) 145 { 146 BackColorM = backColorMove; 147 } 148 if (imageMove != null) 149 { 150 ImageM = imageMove; 151 } 152 } 153 154 private void label_MouseLeave(object sender, EventArgs e) 155 { 156 if (backColorLeave != Color.Transparent) 157 { 158 BackColorM = backColorLeave; 159 } 160 if (imageLeave != null) 161 { 162 ImageM = imageLeave; 163 } 164 } 165 } 166 }
ButtonM.Designer.cs
1 namespace landptf.controls 2 { 3 partial class ButtonM 4 { 5 /// <summary> 6 /// 必需的设计器变量。 7 /// </summary> 8 private System.ComponentModel.IContainer components = null; 9 10 /// <summary> 11 /// 清理所有正在使用的资源。 12 /// </summary> 13 /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param> 14 protected override void Dispose(bool disposing) 15 { 16 if (disposing && (components != null)) 17 { 18 components.Dispose(); 19 } 20 base.Dispose(disposing); 21 } 22 23 #region 组件设计器生成的代码 24 25 /// <summary> 26 /// 设计器支持所需的方法 - 不要 27 /// 使用代码编辑器修改此方法的内容。 28 /// </summary> 29 private void InitializeComponent() 30 { 31 this.label = new System.Windows.Forms.Label(); 32 this.SuspendLayout(); 33 // 34 // label 35 // 36 this.label.Dock = System.Windows.Forms.DockStyle.Fill; 37 this.label.Location = new System.Drawing.Point(0, 0); 38 this.label.Name = "label"; 39 this.label.Size = new System.Drawing.Size(205, 69); 40 this.label.TabIndex = 0; 41 this.label.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 42 this.label.Click += new System.EventHandler(this.label_Click); 43 this.label.MouseLeave += new System.EventHandler(this.label_MouseLeave); 44 this.label.MouseMove += new System.Windows.Forms.MouseEventHandler(this.label_MouseMove); 45 // 46 // ButtonM 47 // 48 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 49 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 50 this.AutoSize = true; 51 this.BackColor = System.Drawing.Color.Transparent; 52 this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; 53 this.Controls.Add(this.label); 54 this.Name = "ButtonM"; 55 this.Size = new System.Drawing.Size(205, 69); 56 this.ResumeLayout(false); 57 58 } 59 60 #endregion 61 62 private System.Windows.Forms.Label label; 63 } 64 }
我们对外提供了设置背景图片,背景色,显示的文本,字体颜色,文本样式,点击事件。在开发中还会遇到其他需要的方法可自行完善。
明天我们在来看一下如何使用自定义控件