入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
目录
https://www.cnblogs.com/bfyx/p/11364884.html
准备工作
该控件将继承控件UCBtnExt,如果你还对UCBtnExt不了解的下,
请移步 (二)c#Winform自定义控件-按钮 查看
首先我们了解下要做的是什么,我们需要做一个可以自定义填充颜色,有圆角边框,有角标的,有图标的按钮
开始
添加一个用户控件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 // 版权所有 黄正辉 交流群: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 }