最近在准备一套自定义控件开发的课程,下面将第一个做的按钮控件分享给大家。

其实这个控件属于自定义控件中的扩展控件,与组合控件和GDI+开发的控件不同,这个控件是继承原生的Button,

这个控件的目的在于把常用的图标集成到控件中,使用会更方便。大家都知道,软件开发UI设计是必不可免的一部分,

所以一个带有图标的按钮会比一个光秃秃的按钮要增色不少,但是大家很多时候不想或者没法找到图标,如果集成在

控件里,是不是会方便很多呢?其实实现原理很简单:

  实现效果如下图所示:

接着上代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms; namespace XktControl
{
public partial class xktButton : Button
{
public xktButton()
{
InitializeComponent();
this.PreSetImage = ButtonPresetImage.None;
this.Size = new Size(, ); } public xktButton(IContainer container)
{
container.Add(this); InitializeComponent();
} #region Enum
/// <summary>
/// EasyButton类型的类型
/// </summary>
public enum ButtonPresetImage
{
/// <summary>
/// 没有图标
/// </summary>
None,
/// <summary>
/// 确认图标
/// </summary>
Check,
/// <summary>
/// 关闭图标
/// </summary>
Close,
/// <summary>
/// 取消图标
/// </summary>
Cancel,
/// <summary>
/// 退后图标
/// </summary>
Back,
/// <summary>
/// 向下图标
/// </summary>
Down,
/// <summary>
/// 前进图标
/// </summary>
Go,
/// <summary>
/// 向上图标
/// </summary>
Up,
/// <summary>
/// 文件夹图标
/// </summary>
Folder,
/// <summary>
/// 刷新图标
/// </summary>
Refresh,
/// <summary>
/// 设置图标
/// </summary>
Setting,
/// <summary>
/// 文件打开图标
/// </summary>
FolderOpen,
/// <summary>
/// 文件删除图标
/// </summary>
DocumentDelete,
/// <summary>
/// 文件图标
/// </summary>
Document,
/// <summary>
/// 文件编辑图标
/// </summary>
DocumentEdit,
/// <summary>
/// 信息图标
/// </summary>
Info,
/// <summary>
/// 文件添加图标
/// </summary>
DocumentAdd,
/// <summary>
/// 全局图标
/// </summary>
Gobal,
/// <summary>
/// 计算图标
/// </summary>
Calculator,
/// <summary>
/// 日期图标
/// </summary>
Calendar,
/// <summary>
/// 打印图标
/// </summary>
Printer
} #endregion #region Fields
ButtonPresetImage _buttontype;
#endregion #region Properties
/// <summary>
/// To show different Image and Text
/// </summary>
[Browsable(true)]
[Description("显示不同的图像")]
[Category("自定义属性")]
public ButtonPresetImage PreSetImage
{
get
{
return _buttontype;
} set
{
_buttontype = value;
switch (_buttontype)
{
case ButtonPresetImage.None:
this.Image = null;
this.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
break;
case ButtonPresetImage.Check:
this.Image = Properties.Resources.check;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Close:
this.Image = Properties.Resources.close;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Cancel:
this.Image = Properties.Resources.cancel;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Back:
this.Image = Properties.Resources.back;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Down:
this.Image = Properties.Resources.down;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Go:
this.Image = Properties.Resources.go;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Up:
this.Image = Properties.Resources.up;
break;
case ButtonPresetImage.Folder:
this.Image = Properties.Resources.folder;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Refresh:
this.Image = Properties.Resources.refresh;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Setting:
this.Image = Properties.Resources.setting;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.FolderOpen:
this.Image = Properties.Resources.folder_open;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.DocumentDelete:
this.Image = Properties.Resources.document_delete;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Document:
this.Image = Properties.Resources.document;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.DocumentEdit:
this.Image = Properties.Resources.document_edit;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Info:
this.Image = Properties.Resources.info;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.DocumentAdd:
this.Image = Properties.Resources.document_add;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Gobal:
this.Image = Properties.Resources.web;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Calculator:
this.Image = Properties.Resources.calculator;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Calendar:
this.Image = Properties.Resources.calendar;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
case ButtonPresetImage.Printer:
this.Image = Properties.Resources.printer;
this.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
break;
default:
break;
} this.Invalidate();
}
} #endregion }
}

 如果大家还有什么不明白的地方,可以关注一下微信公众号:dotNet工控上位机

基于C#开发的扩展按钮控件的更多相关文章

  1. 基于vue开发的element-ui树形控件报错问题解决

    对没错,这次又是ElementUI的问题,在使用ElementUI中的 tree 树形控件时需要动态添加DOM元素,但是在使用文档中给出的案例的时候会报错. 案例:ElementUI树形控件 - 自定 ...

  2. Web应用程序开发,基于Ajax技术的JavaScript树形控件

    感谢http://www.cnblogs.com/dgrew/p/3181769.html#undefined 在Web应用程序开发领域,基于Ajax技术的JavaScript树形控件已经被广泛使用, ...

  3. 安卓开发_复选按钮控件(CheckBox)的简单使用

    复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈 复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性 一.由于 ...

  4. MFC基于对话框风格按钮控件添加图片的方法(大神止步)

    菜鸟还在研究这个东西,大神就不要看了.一直都在觉得用VC或VS建立的对话框总是全灰色感觉太单调了,如果可以在上面添加一些漂亮的图片就好了,今天终于实现了.其实挺简单的,下面就分几个步骤讲一下: 第一步 ...

  5. SNF开发平台WinForm-Grid表格控件大全

    我们在开发系统时,会有很多种控件进行展示,甚至有一些为了方便的一些特殊需求. 那么下面就介绍一些我们在表格控件里常用的方便的控件:   1.Grid表格查询条 Grid表格下拉 3.Grid表格弹框选 ...

  6. 葡萄城首席架构师:前端开发与Web表格控件技术解读

    讲师:Issam Elbaytam,葡萄城集团全球首席架构师(Chief Software Architect of GrapeCity Global).曾任 Data Dynamics.Inc 创始 ...

  7. 基于存储过程的MVC开源分页控件--LYB.NET.SPPager

    摘要 现在基于ASP.NET MVC的分页控件我想大家都不陌生了,百度一下一大箩筐.其中有不少精品,陕北吴旗娃杨涛大哥做的分页控件MVCPager(http://www.webdiyer.com/)算 ...

  8. MFC编程入门之二十三(常用控件:按钮控件的编程实例)

    上一节讲了按钮控件Button.Radio Button和Check Box的基本用法,本节继续讲按钮控件的内容,通过一个实例让大家更清楚按钮控件在实际的软件开发中如何使用. 因为Button控件在前 ...

  9. SNF开发平台WinForm之三-开发-单表选择控件创建-SNF快速开发平台3.3-Spring.Net.Framework

    3.1运行效果: 3.2开发实现: 3.2.1 这个开发与第一个开发操作步骤是一致的,不同之处就是在生成完代码之后,留下如下圈红程序,其它删除. 第一个开发地址:开发-单表表格编辑管理页面 http: ...

随机推荐

  1. 使用editcap.exe分割pcap文件

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  2. 面试题:this指针的指向,以及call、apply应用

    var a = 2; function test(){ var a = 4; console.log(this.a); this.a = 1; } test();//2 //这里为什么是2?因为调用t ...

  3. leetcode218 天际线问题

    来自leetcode题解:扫描线法AlgsCG class Solution { public: vector<vector<int>> getSkyline(vector&l ...

  4. DB2 SQL 错误(SQLCODE:-964,SQLSTATE:57011)处理方法

    故障现象描述: 执行 SQL 语句时,出现类似如下错误消息. 指令 SQL:insert into t_stat_file_temp SQLSTATE:57011,供应商错误代码:-964 DB2 S ...

  5. 深度学习之Seq_seq网络

    知识点 """ 机器翻译: 历史: 1.逐字翻译 2.基于统计学的机器翻译 3.循环网络和编码 翻译过程: 输入 -- > encoder -->向量 --& ...

  6. 小D课堂 - 新版本微服务springcloud+Docker教程_6-03 高级篇幅之zuul常用问题分析

    笔记 3.高级篇幅之Zuul常用问题分析和网关过滤器原理分析 简介:讲解Zuul网关原理和过滤器生命周期,           1.路由名称定义问题         路由映射重复覆盖问题        ...

  7. Netflix Zuul

    Zuul 是在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架.Zuul 相当于是设备和 Netflix 流应用的 Web 网站后端所有请求的前门. ApiGateway服务器 1.pom &l ...

  8. linux定时脚本:删除linux/HDFS上过期文件

    一.定时删除linux上定时的文件 显示20分钟前的文件 -exec ls -l {} \; 删除20分钟前的文件 -exec rm {} \; 显示20天前的文件 -exec ls -l {} \; ...

  9. mysql查看系统参数

    show global variables like ‘innodb_buffer_pool_size’: 查看buffer相关参数 show global variables like 'buffe ...

  10. JAVA是否适合非科班者自学入行?石油工程专业从培训到JAVA入门自学亲身经历

    如今的我已经过了三十而立的年纪,虽然在三十岁我没有立下任何事业,相反,还在茫茫苦海中挣扎. 但是我并不是没有收获.当然,曾经在我拥有大好青春年华的时候选择了迷茫,以至于当我有所明悟的时候,却已经错过了 ...