官网

http://www.hzhcontrols.com

前提

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

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

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

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

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

目录

https://www.cnblogs.com/bfyx/p/11364884.html

准备工作

该控件将继承基类控件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;
/// <summary>
/// 按钮字体颜色
/// </summary>
[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)()));
/// <summary>
/// 按钮字体
/// </summary>
[Description("按钮字体"), Category("自定义")]
public Font BtnFont
{
get { return _btnFont; }
set
{
_btnFont = value;
this.lbl.Font = value;
}
} /// <summary>
/// 按钮点击事件
/// </summary>
[Description("按钮点击事件"), Category("自定义")]
public event EventHandler BtnClick; private string _btnText;
/// <summary>
/// 按钮文字
/// </summary>
[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
// 创建日期:2019-08-15 15:57:36
// 功能描述:按钮
// 项目地址: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("是否显示角标"), 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;
/// <summary>
/// 按钮字体颜色
/// </summary>
[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)()));
/// <summary>
/// 按钮字体
/// </summary>
[Description("按钮字体"), Category("自定义")]
public Font BtnFont
{
get { return _btnFont; }
set
{
_btnFont = value;
this.lbl.Font = value;
}
} /// <summary>
/// 按钮点击事件
/// </summary>
[Description("按钮点击事件"), Category("自定义")]
public event EventHandler BtnClick; private string _btnText;
/// <summary>
/// 按钮文字
/// </summary>
[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
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region 组件设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
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)()));
this.lbl.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.lbl.Location = new System.Drawing.Point(, );
this.lbl.Name = "lbl";
this.lbl.Size = new System.Drawing.Size(, );
this.lbl.TabIndex = ;
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)()));
this.lblTips.ForeColor = System.Drawing.Color.White;
this.lblTips.ImageIndex = ;
this.lblTips.ImageList = this.imageList1;
this.lblTips.Location = new System.Drawing.Point(, );
this.lblTips.Name = "lblTips";
this.lblTips.Size = new System.Drawing.Size(, );
this.lblTips.TabIndex = ;
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(, "tips.png");
//
// UCBtnExt
//
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.BackColor = System.Drawing.Color.Transparent;
this.ConerRadius = ;
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)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.IsShowRect = true;
this.IsRadius = true;
this.Margin = new System.Windows.Forms.Padding();
this.Name = "UCBtnExt";
this.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)()))), ((int)(((byte)()))), ((int)(((byte)()))));
this.Size = new System.Drawing.Size(, );
this.ResumeLayout(false); } #endregion public System.Windows.Forms.Label lbl;
private System.Windows.Forms.Label lblTips;
private System.Windows.Forms.ImageList imageList1; }
}

用处及效果

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

效果:

最后的话

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

(二)c#Winform自定义控件-按钮的更多相关文章

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

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. c#Winform自定义控件-目录

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  3. (四)c#Winform自定义控件-选择按钮组

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  4. winform 自定义控件(高手)

    高手推荐:https://www.cnblogs.com/bfyx/p/11364884.html   c#Winform自定义控件-目录   前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件 ...

  5. (八十二)c#Winform自定义控件-穿梭框

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...

  6. (三十二)c#Winform自定义控件-表格

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  7. (二十)c#Winform自定义控件-有后退的窗体

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  8. (二十五)c#Winform自定义控件-有确定取消的窗体(一)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  9. (二十六)c#Winform自定义控件-有确定取消的窗体(二)

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

随机推荐

  1. python笔记(2)--字符串

    一.字符串 字符串是不可变序列,具有序列的公共操作方法,具体操作见python笔记(1)--序列(列表 元组 range) 1.创建字符串 单引号:'Hello , I am Logan ! ' 双引 ...

  2. Excel催化剂开源第1波-自定义函数的源代码全公开

    Excel催化剂插件从2018年1月1日开始运营,到今天刚好一周年,在过去一年时间里,感谢社区里的许多友人们的关心和鼓励,得以坚持下来,并收获一定的用户量和粉丝数和少量的经济收入回报和个人知名度的提升 ...

  3. Linux操作系统和Windows操作系统的区别

    1.免费与收费 在中国,windows和linux都是免费的,至少对个人用户是如此,如果那天国内windows真的严打盗版了,那linux的春天就到了!但现在linux依然是任重道远,前路漫漫. 2. ...

  4. HTML --- <a href=”#”>与 <a href=”javascript:void(0)” 的区别

    <a href=”#”>中的“#”其实是锚点的意思,默认为#top,所以当页面比较长的时候,使用这种方式会让页面刷新到页首(页面的最上部) javascript:void(0)其实是一个死 ...

  5. 前端jQuery学习(一)

    把最近学习的前端框架jQuery整理一下.你可能听说过jQuery,因为他是JavaScript世界中使用最广泛的一个库. 江湖传言,全世界大约有80~90%的网站直接或间接地使用了jQuery.鉴于 ...

  6. decode函数的几种用法

    1:使用decode判断字符串是否一样 DECODE(value,if1,then1,if2,then2,if3,then3,...,else) 含义为 IF 条件=值1 THEN RETURN(va ...

  7. 1.4.3 ID遍历爬虫(每天一更)

    # -*- coding: utf-8 -*- ''' Created on 2019年5月7日 @author: 薛卫卫 ''' import itertools import urllib.req ...

  8. vue更改了数据但是视图却没有更新

    今日总结:让钩子函数背锅的一上午,跪求原谅 需求:页面刷新时,保留当前页面的选中状态 解决思路:在data里设置一下初始值,保证首次进入有默认选中状态,其次再操作时,把选中的状态存sessionSto ...

  9. vue 初始化table数据,数据闪现的问题

    使用的iview,很简单的一个table,可以扩展显示,我这里则是更改了一下,显示的也是表格,内容为明细数据. 原以为很简单的可以直接调用方法,进行数据的渲染,但是没想到,数据只是一闪而过. 百思不得 ...

  10. 【iOS】UIImageView 点击事件

    UIImageView 并不像 UIButton 那样点击鼠标就可以关联点击事件,也不像 Android 里有 onClickListener,这个时候就需要借助 UITapGestureRecogn ...