(二)C# Winform自定义控件-按钮
proginn468312
共 22532字,需浏览 46分钟
· 2020-07-28
准备工作
该控件将继承基类控件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
// 创建日期: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;
///
/// 按钮字体颜色
///
[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;
}
}
用处及效果
用处:按钮有什么用,我想我不用解释了吧
效果:
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧,另本站转载地址:https://dotnet9.com/5252.html。
-
作者:冰封一夏 -
出处:http://www.hzhcontrols.com/doc.html -
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 -
GitHub:https://github.com/kwwwvagaa/NetWinformControl -
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
Dotnet9网站常驻编辑。
长按关注我,
欢迎技术交流!
点击阅读原文,查看HZHControls站点更多博文。
评论
轻松学习C#:百度行驶证C++离线SDK接入详解
效果 先看最终效果SDK 拿到完整包如图,687M解压后看看内容发现有个readme.txt,那就先看看内容1:用vs2015打开sln工程,最好用vs2015 comunity版本,可微软官网下载。2:sdk的doc目录有pdf接口文档。3:工程总入口main.cpp、请参考示例实现您的功能。4:
DotNet NB
9
C#中using的使用的几种场景
using在C#中随处可见,比如引入命名空间,这是也是每个C#的cs页面必须存在的,除了引入命名空间还有几种用法,下面将介绍这几种的使用。1、用于引入命名空间 这是最常见最基本的用法, 这样可以在程序中直
dotNET全栈开发
10
深入理解Camera 二 (相机架构概览)
和你一起终身学习,这里是程序员Android经典好文推荐,通过阅读本文,您将收获以下知识点:相机架构概览Android系统利用分层思想,将各层的接口定义与实现分离开来,以接口作为各层的脉络连接整体框架,将具体实现的主导权交由各自有具体实现需求的平台厂商或者Android 开发者,这样既做到把控全局,
程序员Android
10
C# 优雅的处理 TCP 数据
前言Tcp是一个面向连接的流数据传输协议,用人话说就是传输是一个已经建立好连接的管道,数据都在管道里像流水一样流淌到对端。那么数据必然存在几个问题,比如数据如何持续的读取,数据包的边界等。Nagle's算法Nagle 算法的核心思想是,在一个 TCP 连接上,最多只能有一个未被确认的小数据包(小于
dotNET全栈开发
10
阿里二面凉了,难蹦。。。
图解学习网站:https://xiaolincoding.com大家好, 我是小林。分享一位同学阿里巴巴的后端面经,共有 2 面,第一面很顺利过了,可惜挂在第二面。这两面的知识点范围,我帮大家罗列一下:网络:TCP、HTTPmysql:索引应用、索引结构、隔离级别、最左匹配redis:zset、缓存
小林coding
10
mica-mqtt 2.2.12 发布,简化 MqttClientTemplate 构造,方便自定义
一、简介mica-mqtt 基于 java aio 实现的简单、低延迟、高性能 的 mqtt 物联网开源组件。mica-mqtt 更加易于集成到已有服务和二次开发,降低自研物联网平台开发成本。二、功能支持 MQTT v3.1、v3.1.1 以及 v5.0 协议。支持 websocket mqtt 子
JAVA架构日记
0
C#中HttpClient的演进与避坑指南
引子 在互联网时代,与网络通信相关的功能已成为不可或缺的一部分。C# 中的 HttpClient 类是一个强大的工具,用于执行 HTTP 请求和处理响应。虽然HttpClient 类简单易用,但在实际应用中,仍然存在一些需要注意...
llovebo
0
网易二面:CPU狂飙900%,该怎么处理?
? 欢迎加入小哈的星球 ,你将获得: 专属的项目实战 / Java 学习路线 / 一对一提问 / 学习打卡 / 赠书福利 全栈前后端分离博客项目 2.0 版本完结啦, 演示链接:http://116.62.199.48/ ,新项目正在酝酿中。全程手摸...
小哈学Java
0