(二)C# Winform自定義控件-按鈕

準(zhǔn)備工作
該控件將繼承基類控件UCControlBase,如果你還對(duì)UCControlBase不了解的下,
請(qǐng)移步 (一)c#Winform自定義控件-基類控件 查看
首先我們了解下要做的是什么,我們需要做一個(gè)可以自定義填充顏色,有圓角邊框,有角標(biāo)的按鈕
開始
添加一個(gè)用戶控件,命名為UCBtnExt ,繼承 UCControlBase
先來看看我們按鈕需要支持的屬性吧
#region 字段屬性
[Description("是否顯示角標(biāo)"), Category("自定義")]
public bool IsShowTips
{
get
{
return this.lblTips.Visible;
}
set
{
this.lblTips.Visible = value;
}
}
[Description("角標(biāo)文字"), Category("自定義")]
public string TipsText
{
get
{
return this.lblTips.Text;
}
set
{
this.lblTips.Text = value;
}
}
private Color _btnBackColor = Color.White;
[Description("按鈕背景色"), Category("自定義")]
public Color BtnBackColor
{
get { return _btnBackColor; }
set
{
_btnBackColor = value;
this.BackColor = value;
}
}
private Color _btnForeColor = Color.Black;
///
/// 按鈕字體顏色
///
[Description("按鈕字體顏色"), Category("自定義")]
public Color BtnForeColor
{
get { return _btnForeColor; }
set
{
_btnForeColor = value;
this.lbl.ForeColor = value;
}
}
private Font _btnFont = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
///
/// 按鈕字體
///
[Description("按鈕字體"), Category("自定義")]
public Font BtnFont
{
get { return _btnFont; }
set
{
_btnFont = value;
this.lbl.Font = value;
}
}
///
/// 按鈕點(diǎn)擊事件
///
[Description("按鈕點(diǎn)擊事件"), Category("自定義")]
public event EventHandler BtnClick;
private string _btnText;
///
/// 按鈕文字
///
[Description("按鈕文字"), Category("自定義")]
public string BtnText
{
get { return _btnText; }
set
{
_btnText = value;
lbl.Text = value;
}
}
#endregion
有了屬性是不是就更明了呢
還有最后關(guān)鍵的一點(diǎn)東西,就是按鈕的點(diǎn)擊事件
private void lbl_MouseDown(object sender, MouseEventArgs e)
{
if (this.BtnClick != null)
BtnClick(this, e);
}
至此基本上就完工了,下面列出了完整的代碼
// 版權(quán)所有 黃正輝 交流群:568015492 QQ:623128629
// 文件名稱:UCBtnExt.cs
// 創(chuàng)建日期:2019-08-15 15:57:36
// 功能描述:按鈕
// 項(xiàng)目地址: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
{
[DefaultEvent("BtnClick")]
public partial class UCBtnExt : UCControlBase
{
#region 字段屬性
[Description("是否顯示角標(biāo)"), Category("自定義")]
public bool IsShowTips
{
get
{
return this.lblTips.Visible;
}
set
{
this.lblTips.Visible = value;
}
}
[Description("角標(biāo)文字"), Category("自定義")]
public string TipsText
{
get
{
return this.lblTips.Text;
}
set
{
this.lblTips.Text = value;
}
}
private Color _btnBackColor = Color.White;
[Description("按鈕背景色"), Category("自定義")]
public Color BtnBackColor
{
get { return _btnBackColor; }
set
{
_btnBackColor = value;
this.BackColor = value;
}
}
private Color _btnForeColor = Color.Black;
///
/// 按鈕字體顏色
///
[Description("按鈕字體顏色"), Category("自定義")]
public Color BtnForeColor
{
get { return _btnForeColor; }
set
{
_btnForeColor = value;
this.lbl.ForeColor = value;
}
}
private Font _btnFont = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
///
/// 按鈕字體
///
[Description("按鈕字體"), Category("自定義")]
public Font BtnFont
{
get { return _btnFont; }
set
{
_btnFont = value;
this.lbl.Font = value;
}
}
///
/// 按鈕點(diǎn)擊事件
///
[Description("按鈕點(diǎn)擊事件"), Category("自定義")]
public event EventHandler BtnClick;
private string _btnText;
///
/// 按鈕文字
///
[Description("按鈕文字"), Category("自定義")]
public string BtnText
{
get { return _btnText; }
set
{
_btnText = value;
lbl.Text = value;
}
}
#endregion
public UCBtnExt()
{
InitializeComponent();
this.TabStop = false;
}
private void lbl_MouseDown(object sender, MouseEventArgs e)
{
if (this.BtnClick != null)
BtnClick(this, e);
}
}
}
namespace HZH_Controls.Controls
{
public partial class UCBtnExt
{
///
/// 必需的設(shè)計(jì)器變量。
///
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è)計(jì)器生成的代碼
///
/// 設(shè)計(jì)器支持所需的方法 - 不要
/// 使用代碼編輯器修改此方法的內(nèi)容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UCBtnExt));
this.lbl = new System.Windows.Forms.Label();
this.lblTips = new System.Windows.Forms.Label();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.SuspendLayout();
//
// lbl
//
this.lbl.BackColor = System.Drawing.Color.Transparent;
this.lbl.Dock = System.Windows.Forms.DockStyle.Fill;
this.lbl.Font = new System.Drawing.Font("微軟雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.lbl.Location = new System.Drawing.Point(0, 0);
this.lbl.Name = "lbl";
this.lbl.Size = new System.Drawing.Size(184, 60);
this.lbl.TabIndex = 0;
this.lbl.Text = "自定義按鈕";
this.lbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lbl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.lbl_MouseDown);
//
// lblTips
//
this.lblTips.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.lblTips.BackColor = System.Drawing.Color.Transparent;
this.lblTips.Font = new System.Drawing.Font("Arial Unicode MS", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.lblTips.ForeColor = System.Drawing.Color.White;
this.lblTips.ImageIndex = 0;
this.lblTips.ImageList = this.imageList1;
this.lblTips.Location = new System.Drawing.Point(158, 0);
this.lblTips.Name = "lblTips";
this.lblTips.Size = new System.Drawing.Size(24, 24);
this.lblTips.TabIndex = 1;
this.lblTips.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.lblTips.Visible = false;
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.imageList1.Images.SetKeyName(0, "tips.png");
//
// UCBtnExt
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.Transparent;
this.ConerRadius = 5;
this.Controls.Add(this.lblTips);
this.Controls.Add(this.lbl);
this.Cursor = System.Windows.Forms.Cursors.Hand;
this.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
this.IsShowRect = true;
this.IsRadius = true;
this.Margin = new System.Windows.Forms.Padding(0);
this.Name = "UCBtnExt";
this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(247)))), ((int)(((byte)(247)))), ((int)(((byte)(247)))));
this.Size = new System.Drawing.Size(184, 60);
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.Label lbl;
private System.Windows.Forms.Label lblTips;
private System.Windows.Forms.ImageList imageList1;
}
}
用處及效果
用處:按鈕有什么用,我想我不用解釋了吧
效果:
最后的話
如果你喜歡的話,請(qǐng)到 https://gitee.com/kwwwvagaa/net_winform_custom_control 點(diǎn)個(gè)星星吧,另本站轉(zhuǎn)載地址:https://dotnet9.com/5252.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)站常駐編輯。
長(zhǎng)按關(guān)注我,
歡迎技術(shù)交流!
點(diǎn)擊閱讀原文,查看HZHControls站點(diǎn)更多博文。
評(píng)論
圖片
表情
