(三)C# Winform自定義控件-有圖標(biāo)的按鈕

準(zhǔn)備工作
該控件將繼承控件UCBtnExt,如果你還對UCBtnExt不了解的下,
請移步?(二)c#Winform自定義控件-按鈕?查看
首先我們了解下要做的是什么,我們需要做一個可以自定義填充顏色,有圓角邊框,有角標(biāo)的,有圖標(biāo)的按鈕
開始
添加一個用戶控件UCBtnImg 繼承UCBtnExt
添加屬性
private?string?_btnText?=?"自定義按鈕";
///?
///?按鈕文字
///?
[Description("按鈕文字"),?Category("自定義")]
public?new?string?BtnText
{
????get?{?return?_btnText;?}
????set
????{
????????_btnText?=?value;
????????lbl.Text?=?"????"?+?value;
????????lbl.Refresh();
????}
}
///?
///?圖片
///?
[Description("圖片"),?Category("自定義")]
public?Image?Image
{
????get
????{
????????return?this.imageList1.Images[0];
????}
????set
????{
????????this.imageList1.Images.Clear();
????????this.imageList1.Images.Add(value);
????????this.lbl.ImageIndex?=?0;
????}
}
下面看一下完整代碼
//?版權(quán)所有??黃正輝??交流群:568015492 ? QQ:623128629
//?文件名稱:UCBtnImg.cs
//?創(chuàng)建日期:2019-08-15 15:58:07
//?功能描述:按鈕
//?項目地址:https://gitee.com/kwwwvagaa/net_winform_custom_control
using?System;
using?System.Collections.Generic;
using?System.ComponentModel;
using?System.Drawing;
using?System.Data;
using?System.Linq;
using?System.Text;
using?System.Windows.Forms;
namespace?HZH_Controls.Controls
{
????///?
????///?
????///?
????public?partial?class?UCBtnImg?:?UCBtnExt
????{
????????private?string?_btnText?=?"自定義按鈕";
????????///?
????????///?按鈕文字
????????///?
????????[Description("按鈕文字"),?Category("自定義")]
????????public?new?string?BtnText
????????{
????????????get?{?return?_btnText;?}
????????????set
????????????{
????????????????_btnText?=?value;
????????????????lbl.Text?=?"????"?+?value;
????????????????lbl.Refresh();
????????????}
????????}
????????///?
????????///?圖片
????????///?
????????[Description("圖片"),?Category("自定義")]
????????public?Image?Image
????????{
????????????get
????????????{
????????????????return?this.imageList1.Images[0];
????????????}
????????????set
????????????{
????????????????this.imageList1.Images.Clear();
????????????????this.imageList1.Images.Add(value);
????????????????this.lbl.ImageIndex?=?0;
????????????}
????????}
????????public?UCBtnImg()
????????{
????????????InitializeComponent();
????????????base.BtnForeColor?=?ForeColor?=?System.Drawing.Color.FromArgb(((int)(((byte)(102)))),?((int)(((byte)(102)))),?((int)(((byte)(102)))));
????????????base.BtnFont?=?new?System.Drawing.Font("微軟雅黑",?17F);
????????????base.BtnText?=?"????自定義按鈕";
????????}
????}
}
namespace?HZH_Controls.Controls
{
????partial?class?UCBtnImg
????{
????????///? ?
????????///?必需的設(shè)計器變量。
????????///?
????????private?System.ComponentModel.IContainer?components?=?null;
????????///? ?
????????///?清理所有正在使用的資源。
????????///?
????????///?如果應(yīng)釋放托管資源,為 true;否則為 false。
????????protected?override?void?Dispose(bool?disposing)
????????{
????????????if?(disposing?&&?(components?!=?null))
????????????{
????????????????components.Dispose();
????????????}
????????????base.Dispose(disposing);
????????}
????????#region?組件設(shè)計器生成的代碼
????????///? ?
????????///?設(shè)計器支持所需的方法?-?不要
????????///?使用代碼編輯器修改此方法的內(nèi)容。
????????///?
????????private?void?InitializeComponent()
????????{
????????????this.components?=?new?System.ComponentModel.Container();
????????????System.ComponentModel.ComponentResourceManager?resources?=?new?System.ComponentModel.ComponentResourceManager(typeof(UCBtnImg));
????????????this.imageList1?=?new?System.Windows.Forms.ImageList(this.components);
????????????this.SuspendLayout();
????????????//?
????????????//?lbl
????????????//?
????????????//this.lbl.Font?=?new?System.Drawing.Font("微軟雅黑",?17F);
????????????//this.lbl.ForeColor?=?System.Drawing.Color.FromArgb(((int)(((byte)(102)))),?((int)(((byte)(102)))),?((int)(((byte)(102)))));
????????????this.lbl.ImageIndex?=?0;
????????????this.lbl.ImageList?=?this.imageList1;
????????????this.lbl.Padding?=?new?System.Windows.Forms.Padding(10,?0,?0,?0);
????????????this.lbl.Text?=?"????自定義按鈕";
????????????this.lbl.TextAlign?=?System.Drawing.ContentAlignment.MiddleLeft;
????????????//?
????????????//?imageList1
????????????//?
????????????this.imageList1.ImageStream?=?((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
????????????this.imageList1.TransparentColor?=?System.Drawing.Color.Transparent;
????????????this.imageList1.Images.SetKeyName(0,?"back.png");
????????????//?
????????????//?BtnBack
????????????//?
????????????this.AutoScaleDimensions?=?new?System.Drawing.SizeF(6F,?12F);
????????????this.AutoScaleMode?=?System.Windows.Forms.AutoScaleMode.None;
????????????this.Name?=?"BtnBack";
????????????this.ResumeLayout(false);
????????}
????????#endregion
????????private?System.Windows.Forms.ImageList?imageList1;
????}
}
用處及效果
用處:按鈕需要顯示圖標(biāo)時使用
效果:

最后的話
如果你喜歡的話,請到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點個星星吧,另本站轉(zhuǎn)載地址:https://dotnet9.com/5260.html。
作者:冰封一夏 出處:http://www.hzhcontrols.com/doc.html 本文版權(quán)歸作者所有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明, 且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。 GitHub:https://github.com/kwwwvagaa/NetWinformControl 碼云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
Dotnet9網(wǎng)站常駐編輯。
長按關(guān)注我,
歡迎技術(shù)交流!
點擊閱讀原文,查看HZHControls站點更多博文。
評論
圖片
表情
