(三)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 點個星星吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM