(三)c#Winform自定义控件-有图标的按钮-HZHControls

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492 企鹅群568015492

目录

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 }
View Code
 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 }
View Code

用处及效果

用处:按钮需要显示图标时使用

效果:

 

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

posted @ 2019-08-16 09:38  冰封一夏  阅读(11035)  评论(0编辑  收藏  举报
HZHControls控件库官网:http://hzhcontrols.com