http://www.hzhcontrols.comhtml
入行已經7,8年了,一直想作一套漂亮點的自定義控件,因而就有了本系列文章。git
GitHub:https://github.com/kwwwvagaa/NetWinformControlgithub
碼雲:https://gitee.com/kwwwvagaa/net_winform_custom_control.gitc#
若是以爲寫的還行,請點個 star 支持一下吧編輯器
http://www.javashuo.com/article/p-hacmmtru-mw.htmlpost
該控件將繼承基類控件UCControlBase,若是你還對UCControlBase不瞭解的下,字體
請移步 (一)c#Winform自定義控件-基類控件 查看this
首先咱們瞭解下要作的是什麼,咱們須要作一個能夠自定義填充顏色,有圓角邊框,有角標的按鈕url
添加一個用戶控件,命名爲UCBtnExt ,繼承 UCControlBase
先來看看咱們按鈕須要支持的屬性吧
1 #region 字段屬性 2 [Description("是否顯示角標"), Category("自定義")] 3 public bool IsShowTips 4 { 5 get 6 { 7 return this.lblTips.Visible; 8 } 9 set 10 { 11 this.lblTips.Visible = value; 12 } 13 } 14 15 [Description("角標文字"), Category("自定義")] 16 public string TipsText 17 { 18 get 19 { 20 return this.lblTips.Text; 21 } 22 set 23 { 24 this.lblTips.Text = value; 25 } 26 } 27 28 private Color _btnBackColor = Color.White; 29 [Description("按鈕背景色"), Category("自定義")] 30 public Color BtnBackColor 31 { 32 get { return _btnBackColor; } 33 set 34 { 35 _btnBackColor = value; 36 this.BackColor = value; 37 } 38 } 39 40 private Color _btnForeColor = Color.Black; 41 /// <summary> 42 /// 按鈕字體顏色 43 /// </summary> 44 [Description("按鈕字體顏色"), Category("自定義")] 45 public Color BtnForeColor 46 { 47 get { return _btnForeColor; } 48 set 49 { 50 _btnForeColor = value; 51 this.lbl.ForeColor = value; 52 } 53 } 54 55 private Font _btnFont = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 56 /// <summary> 57 /// 按鈕字體 58 /// </summary> 59 [Description("按鈕字體"), Category("自定義")] 60 public Font BtnFont 61 { 62 get { return _btnFont; } 63 set 64 { 65 _btnFont = value; 66 this.lbl.Font = value; 67 } 68 } 69 70 /// <summary> 71 /// 按鈕點擊事件 72 /// </summary> 73 [Description("按鈕點擊事件"), Category("自定義")] 74 public event EventHandler BtnClick; 75 76 private string _btnText; 77 /// <summary> 78 /// 按鈕文字 79 /// </summary> 80 [Description("按鈕文字"), Category("自定義")] 81 public string BtnText 82 { 83 get { return _btnText; } 84 set 85 { 86 _btnText = value; 87 lbl.Text = value; 88 } 89 } 90 #endregion
有了屬性是否是就更明瞭呢
還有最後關鍵的一點東西,就是按鈕的點擊事件
1 private void lbl_MouseDown(object sender, MouseEventArgs e) 2 { 3 if (this.BtnClick != null) 4 BtnClick(this, e); 5 }
至此基本上就完工了,下面列出了完整的代碼
1 // 版權全部 黃正輝 交流羣:568015492 QQ:623128629 2 // 文件名稱:UCBtnExt.cs 3 // 建立日期:2019-08-15 15:57:36 4 // 功能描述:按鈕 5 // 項目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 6 7 using System; 8 using System.Collections.Generic; 9 using System.ComponentModel; 10 using System.Drawing; 11 using System.Data; 12 using System.Linq; 13 using System.Text; 14 using System.Windows.Forms; 15 16 namespace HZH_Controls.Controls 17 { 18 [DefaultEvent("BtnClick")] 19 public partial class UCBtnExt : UCControlBase 20 { 21 #region 字段屬性 22 [Description("是否顯示角標"), Category("自定義")] 23 public bool IsShowTips 24 { 25 get 26 { 27 return this.lblTips.Visible; 28 } 29 set 30 { 31 this.lblTips.Visible = value; 32 } 33 } 34 35 [Description("角標文字"), Category("自定義")] 36 public string TipsText 37 { 38 get 39 { 40 return this.lblTips.Text; 41 } 42 set 43 { 44 this.lblTips.Text = value; 45 } 46 } 47 48 private Color _btnBackColor = Color.White; 49 [Description("按鈕背景色"), Category("自定義")] 50 public Color BtnBackColor 51 { 52 get { return _btnBackColor; } 53 set 54 { 55 _btnBackColor = value; 56 this.BackColor = value; 57 } 58 } 59 60 private Color _btnForeColor = Color.Black; 61 /// <summary> 62 /// 按鈕字體顏色 63 /// </summary> 64 [Description("按鈕字體顏色"), Category("自定義")] 65 public Color BtnForeColor 66 { 67 get { return _btnForeColor; } 68 set 69 { 70 _btnForeColor = value; 71 this.lbl.ForeColor = value; 72 } 73 } 74 75 private Font _btnFont = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 76 /// <summary> 77 /// 按鈕字體 78 /// </summary> 79 [Description("按鈕字體"), Category("自定義")] 80 public Font BtnFont 81 { 82 get { return _btnFont; } 83 set 84 { 85 _btnFont = value; 86 this.lbl.Font = value; 87 } 88 } 89 90 /// <summary> 91 /// 按鈕點擊事件 92 /// </summary> 93 [Description("按鈕點擊事件"), Category("自定義")] 94 public event EventHandler BtnClick; 95 96 private string _btnText; 97 /// <summary> 98 /// 按鈕文字 99 /// </summary> 100 [Description("按鈕文字"), Category("自定義")] 101 public string BtnText 102 { 103 get { return _btnText; } 104 set 105 { 106 _btnText = value; 107 lbl.Text = value; 108 } 109 } 110 #endregion 111 public UCBtnExt() 112 { 113 InitializeComponent(); 114 this.TabStop = false; 115 } 116 117 private void lbl_MouseDown(object sender, MouseEventArgs e) 118 { 119 if (this.BtnClick != null) 120 BtnClick(this, e); 121 } 122 } 123 }
1 namespace HZH_Controls.Controls 2 { 3 public partial class UCBtnExt 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(UCBtnExt)); 33 this.lbl = new System.Windows.Forms.Label(); 34 this.lblTips = new System.Windows.Forms.Label(); 35 this.imageList1 = new System.Windows.Forms.ImageList(this.components); 36 this.SuspendLayout(); 37 // 38 // lbl 39 // 40 this.lbl.BackColor = System.Drawing.Color.Transparent; 41 this.lbl.Dock = System.Windows.Forms.DockStyle.Fill; 42 this.lbl.Font = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 43 this.lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft; 44 this.lbl.Location = new System.Drawing.Point(0, 0); 45 this.lbl.Name = "lbl"; 46 this.lbl.Size = new System.Drawing.Size(184, 60); 47 this.lbl.TabIndex = 0; 48 this.lbl.Text = "自定義按鈕"; 49 this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 50 this.lbl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown); 51 // 52 // lblTips 53 // 54 this.lblTips.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); 55 this.lblTips.BackColor = System.Drawing.Color.Transparent; 56 this.lblTips.Font = new System.Drawing.Font("Arial Unicode MS", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); 57 this.lblTips.ForeColor = System.Drawing.Color.White; 58 this.lblTips.ImageIndex = 0; 59 this.lblTips.ImageList = this.imageList1; 60 this.lblTips.Location = new System.Drawing.Point(158, 0); 61 this.lblTips.Name = "lblTips"; 62 this.lblTips.Size = new System.Drawing.Size(24, 24); 63 this.lblTips.TabIndex = 1; 64 this.lblTips.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 65 this.lblTips.Visible = false; 66 // 67 // imageList1 68 // 69 this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream"))); 70 this.imageList1.TransparentColor = System.Drawing.Color.Transparent; 71 this.imageList1.Images.SetKeyName(0, "tips.png"); 72 // 73 // UCBtnExt 74 // 75 this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; 76 this.BackColor = System.Drawing.Color.Transparent; 77 this.ConerRadius = 5; 78 this.Controls.Add(this.lblTips); 79 this.Controls.Add(this.lbl); 80 this.Cursor = System.Windows.Forms.Cursors.Hand; 81 this.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247))))); 82 this.IsShowRect = true; 83 this.IsRadius = true; 84 this.Margin = new System.Windows.Forms.Padding(0); 85 this.Name = "UCBtnExt"; 86 this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247))))); 87 this.Size = new System.Drawing.Size(184, 60); 88 this.ResumeLayout(false); 89 90 } 91 92 #endregion 93 94 public System.Windows.Forms.Label lbl; 95 private System.Windows.Forms.Label lblTips; 96 private System.Windows.Forms.ImageList imageList1; 97 98 99 } 100 }
用處:按鈕有什麼用,我想我不用解釋了吧
效果:
若是你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧