前提
入行已經(jīng)7,8年了,一直想做一套漂亮點的自定義控件,于是就有了本系列文章。
開源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>
如果覺得寫的還行,請點個 star 支持一下吧
歡迎前來交流探討: 企鵝群568015492?
<https://shang.qq.com/wpa/qunwpa?idkey=6e08741ef16fe53bf0314c1c9e336c4f626047943a8b76bac062361bab6b4f8d>
目錄
https://www.cnblogs.com/bfyx/p/11364884.html
<https://www.cnblogs.com/bfyx/p/11364884.html>
準(zhǔn)備工作
該控件將繼承控件UCBtnExt,如果你還對UCBtnExt不了解的下,
請移步?(二)c#Winform自定義控件-按鈕 <https://www.cnblogs.com/bfyx/p/11361894.html>?查看
首先我們了解下要做的是什么,我們需要做一個可以自定義填充顏色,有圓角邊框,有角標(biāo)的,有圖標(biāo)的按鈕
開始
?添加一個用戶控件UCBtnImg 繼承UCBtnExt
添加屬性
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 // 版權(quán)所有 黃正輝 交流群:568015492 QQ:623128629 2 // 文件名稱:UCBtnImg.cs 3 //
創(chuàng)建日期: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
BtnText28 { 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 } View Code 1 namespace
HZH_Controls.Controls 2 { 3 partial class UCBtnImg 4 { 5 /// <summary> 6
/// 必需的設(shè)計器變量。 7 /// </summary> 8 private System.ComponentModel.IContainer
components =null; 9 10 /// <summary> 11 /// 清理所有正在使用的資源。 12 /// </summary> 13
/// <param name="disposing">如果應(yīng)釋放托管資源,為 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 組件設(shè)計器生成的代碼 24 25 /// <summary> 26 /// 設(shè)計器支持所需的方法 - 不要 27 ///
使用代碼編輯器修改此方法的內(nèi)容。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 } View Code
用處及效果
用處:按鈕需要顯示圖標(biāo)時使用
效果:
?
最后的話
如果你喜歡的話,請到?https://gitee.com/kwwwvagaa/net_winform_custom_control
<https://gitee.com/kwwwvagaa/net_winform_custom_control>?點個星星吧
熱門工具 換一換
感谢您访问我们的网站,您可能还对以下资源感兴趣:
调教肉文小说-国产成本人片免费av-空姐av种子无码-在线观看免费午夜视频-综合久久精品激情-国产成人丝袜视频在线观看软件-大芭区三区四区无码-啊啊好爽啊啊插啊用力啊啊-wanch视频网-国产精品成人a免费观看