入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。html
GitHub:https://github.com/kwwwvagaa/NetWinformControlgit
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitgithub
若是以爲寫的還行,請點個 star 支持一下吧c#
http://www.javashuo.com/article/p-hacmmtru-mw.htmlide
該控件將繼承控件UCBtnExt,若是你還對UCBtnExt不瞭解的下,post
請移步 (二)c#Winform自定義控件-按鈕 查看this
首先咱們瞭解下要作的是什麼,咱們須要作一個能夠自定義填充顏色,有圓角邊框,有角標的,有圖標的按鈕url
添加一個用戶控件UCBtnImg 繼承UCBtnExtspa
添加屬性
1 private string _btnText = "自定義按鈕"; 2 /// <summary> 3 /// 按鈕文字 4 /// </summary> 5 [Description("按鈕文字"), Category("自定義")] 6 public new string BtnText 7 { 8 get { return _btnText; } 9 set 10 { 11 _btnText = value; 12 lbl.Text = " " + value; 13 lbl.Refresh(); 14 } 15 } 16 /// <summary> 17 /// 圖片 18 /// </summary> 19 [Description("圖片"), Category("自定義")] 20 public Image Image 21 { 22 get 23 { 24 return this.imageList1.Images[0]; 25 } 26 set 27 { 28 this.imageList1.Images.Clear(); 29 this.imageList1.Images.Add(value); 30 this.lbl.ImageIndex = 0; 31 } 32 }
下面看一下完整代碼
1 // 版權全部 黃正輝 交流羣:568015492 QQ:623128629 2 // 文件名稱:UCBtnImg.cs 3 // 建立日期:2019-08-15 15:58:07 4 // 功能描述:按鈕 5 // 項目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 6 using System; 7 using System.Collections.Generic; 8 using System.ComponentModel; 9 using System.Drawing; 10 using System.Data; 11 using System.Linq; 12 using System.Text; 13 using System.Windows.Forms; 14 15 namespace HZH_Controls.Controls 16 { 17 /// <summary> 18 /// 19 /// </summary> 20 public partial class UCBtnImg : UCBtnExt 21 { 22 private string _btnText = "自定義按鈕"; 23 /// <summary> 24 /// 按鈕文字 25 /// </summary> 26 [Description("按鈕文字"), Category("自定義")] 27 public new string BtnText 28 { 29 get { return _btnText; } 30 set 31 { 32 _btnText = value; 33 lbl.Text = " " + value; 34 lbl.Refresh(); 35 } 36 } 37 /// <summary> 38 /// 圖片 39 /// </summary> 40 [Description("圖片"), Category("自定義")] 41 public Image Image 42 { 43 get 44 { 45 return this.imageList1.Images[0]; 46 } 47 set 48 { 49 this.imageList1.Images.Clear(); 50 this.imageList1.Images.Add(value); 51 this.lbl.ImageIndex = 0; 52 } 53 } 54 55 public UCBtnImg() 56 { 57 InitializeComponent(); 58 base.BtnForeColor = ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102))))); 59 base.BtnFont = new System.Drawing.Font("微軟雅黑", 17F); 60 base.BtnText = " 自定義按鈕"; 61 } 62 } 63 }
1 namespace HZH_Controls.Controls 2 { 3 partial class UCBtnImg 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.components = new System.ComponentModel.Container(); 32 System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCBtnImg)); 33 this.imageList1 = new System.Windows.Forms.ImageList(this.components); 34 this.SuspendLayout(); 35 // 36 // lbl 37 // 38 //this.lbl.Font = new System.Drawing.Font("微軟雅黑", 17F); 39 //this.lbl.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(102)))), ((int)(((byte)(102)))), ((int)(((byte)(102))))); 40 this.lbl.ImageIndex = 0; 41 this.lbl.ImageList = this.imageList1; 42 this.lbl.Padding = new System.Windows.Forms.Padding(10, 0, 0, 0); 43 this.lbl.Text = " 自定義按鈕"; 44 this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft; 45 // 46 // imageList1 47 // 48 this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); 49 this.imageList1.TransparentColor = System.Drawing.Color.Transparent; 50 this.imageList1.Images.SetKeyName(0, "back.png"); 51 // 52 // BtnBack 53 // 54 this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); 55 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 56 this.Name = "BtnBack"; 57 this.ResumeLayout(false); 58 59 } 60 61 #endregion 62 63 private System.Windows.Forms.ImageList imageList1; 64 } 65 }
用處:按鈕須要顯示圖標時使用
效果:
若是你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧