知方号

知方号

(二)c#Winform自定义控件<刷新按钮样式>

(二)c#Winform自定义控件

(二)c#Winform自定义控件-按钮 前提

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

官网:https://www.hzhcontrols.cn​​​​​​​

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

码云:HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~

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

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

目录

c#Winform自定义控件-目录_c#winform自定义控件-有图标的按钮-CSDN博客

用处及效果

用处:按钮有什么用,我想我不用解释了吧

效果:

准备工作

该控件将继承基类控件UCControlBase,如果你还对UCControlBase不了解的下,

请移步 (一)c#Winform自定义控件-基类控件  查看

首先我们了解下要做的是什么,我们需要做一个可以自定义填充颜色,有圆角边框,有角标的按钮

开始

添加一个用户控件,命名为UCBtnExt ,继承 UCControlBase

先来看看我们按钮需要支持的属性吧

#region 字段属性 [Description("是否显示角标"), Category("自定义")] public bool IsShowTips { get { return this.lblTips.Visible; } set { this.lblTips.Visible = value; } } [Description("角标文字"), 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; } } /// /// 按钮点击事件 /// [Description("按钮点击事件"), Category("自定义")] public event EventHandler BtnClick; private string _btnText; /// /// 按钮文字 /// [Description("按钮文字"), Category("自定义")] public string BtnText { get { return _btnText; } set { _btnText = value; lbl.Text = value; } } #endregion

有了属性是不是就更明了呢

还有最后关键的一点东西,就是按钮的点击事件

private void lbl_MouseDown(object sender, MouseEventArgs e){ if (this.BtnClick != null) BtnClick(this, e);}

至此基本上就完工了,下面列出了完整的代码

// 版权所有 黄正辉 交流群:568015492 QQ:623128629// 文件名称:UCBtnExt.cs// 创建日期:2023-08-15 15:57:36// 功能描述:按钮// 项目地址:https://gitee.com/kwwwvagaa/net_winform_custom_controlusing 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("是否显示角标"), Category("自定义")] public bool IsShowTips { get { return this.lblTips.Visible; } set { this.lblTips.Visible = value; } } [Description("角标文字"), 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; } } /// /// 按钮点击事件 /// [Description("按钮点击事件"), 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 { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region 组件设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// 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; }} 最后的话

如果你喜欢的话,请到 HZHControls控件库: HZHControls控件库,c#的winform自定义控件,对触屏具有更好的操作支持,项目是基于framework4.0,完全原生控件开发,没有使用任何第三方控件,你可以放心的用在你的项目中(winfromcontrol/winformcontrol/.net)。还有更丰富的工业控件持续增加中~~~ 点个星星吧

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。