入行已经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
该控件将继承基类控件UCControlBase,如果你还对UCControlBase不了解的下,
请移步 (一)c#Winform自定义控件-基类控件 查看
首先我们了解下要做的是什么,我们需要做一个可以自定义填充颜色,有圆角边框,有角标的按钮
添加一个用户控件,命名为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 点个星星吧