C#之用户自定义控件
一、新建用户自定义控件
如下图所示,想通过LED的点击来实现亮和灭使用去控制下位机。
LED亮:

LED灭:

首先新建一个用户控件类,名字为LedControl.cs,如下图所示步骤:


在资源中,添加现有文件中加入图片

加入的图片可以在Resources中看到列表

编译成功后,在工具箱中看到新建出来的用户控件:

二、新建用户控件的源码及注释
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TB562
{
public partial class LedControl : UserControl
{
public LedControl()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); //禁止擦除背景
this.SetStyle(ControlStyles.DoubleBuffer, true); //启用双缓冲,双缓冲为了解决控件刷新闪烁的问题
this.SetStyle(ControlStyles.ResizeRedraw, true); //调整大小时重绘
this.SetStyle(ControlStyles.Selectable, true); //控件可以接收焦点
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);//透明效果
this.SetStyle(ControlStyles.UserPaint, true); //自绘
this.BackColor = Color.Transparent; //获取系统定义的颜色
this.Cursor = Cursors.Hand; //表达鼠标移动到控件上面会变成手势光标
this.Size = new Size(87, 27); //自定义控件的大小尺寸
}
//定义一个公共属性
bool isCheck = false;
/// <summary>
/// 是否选中
/// </summary>
public bool Checked
{
set { isCheck = value;this.Invalidate();} // 用Invalidate()使区域无效就可触发该控件的重画
get { return isCheck; }
}
public enum CheckStyle
{
style1,
style2,
style3,
style4,
};
CheckStyle checkStyle = CheckStyle.style1;//定义多种控件样式选择
/// <summary>
/// 样式
/// </summary>
public CheckStyle CheckStyleX
{
set { checkStyle = value; } //
get { return checkStyle; }
}
public event EventHandler CheckChanged;
//鼠标点击的触发事件
private void LedControl_Click(object sender, EventArgs e)
{
isCheck = !isCheck;
this.Invalidate(); //Invalidate将控件标记为重绘,触发重写OnPaint
}
protected override void OnPaint(PaintEventArgs e) //重写控件的事件
{
Bitmap bitMapOn = null;
Bitmap bitMapOff = null;
if (checkStyle == CheckStyle.style1)
{
bitMapOn = global::TB562.Properties.Resources.ledopen; //资源文件中图片的文件名ledopen定义
bitMapOff = global::TB562.Properties.Resources.ledclose; //资源文件中图片的文件名ledopen定义
}
Graphics g = e.Graphics; //创建画布
Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height);//矩形的位置和大小
if (isCheck)
{
g.DrawImage(bitMapOn, rec); //画LED灯开的图像
}
else
{
g.DrawImage(bitMapOff, rec); //画LED灯关的图像
}
}
}
}
另外要使用自定义控件前还需要先使用,如下:
ledControl1.Enabled = true;
ledControl2.Enabled = true;
ledControl3.Enabled = true;
ledControl4.Enabled = true;
ledControl5.Enabled = true;
ledControl6.Enabled = true;
ledControl7.Enabled = true;
ledControl8.Enabled = true;
ledControl9.Enabled = true;
三、参考文档
https://www.cnblogs.com/dyllove98/archive/2013/07/05/3174536.html
https://www.cnblogs.com/yelanggu/p/6224587.html
http://blog.sina.com.cn/s/blog_752ca76a0100qjub.html
by 羊羊得亿
2018-01-22 ShenZhen
C#之用户自定义控件的更多相关文章
- mvc 母版页、用户自定义控件
母版页(Master) 1.母版页是与Controller无关的,母版页只是一个View文件,而没有任何Controller与之相对应. 2.其实在ASP.NET MVC中View的aspx与母版页并 ...
- WPF中添加Winform用户自定义控件
过程:创建WPF工程->创建Winform用户自定义控件工程->WPF中引用控件->添加到Xaml页面 1.首先在WPF工程的解决方案上右击选择添加新建项目: 选择Windows窗体 ...
- C#用户自定义控件(含源代码)-透明文本框
using System; using System.Collections; using System.ComponentModel; using System.Drawing; using Sys ...
- Winform中用户自定义控件中外部设置子控件属性的方法
假设我们新建立一个用户自定义控件,由一个lable1和pictureBox1组成 此时我们在外部调用该控件,然后想动态改变lable1的值,我们该怎么办? 假设实例化的用户控件名为UserContro ...
- Web用户自定义控件
在新建项的时候,选择Web用户控件,可用来自定义自己的控件,做好后,直接拖到页面即可使用自定义控件与WEB交互,需要在 自定义控件里面 写 属性,如: public string CityID { g ...
- ASP.NET用户自定义控件配置
一直以来开发中碰到要写自定义控件的时候总是习惯性的找度娘,而没有自己记住,结果今天就悲剧了,找了半天才找到,想想还是自己积累起来吧! 第一种配置方式: 配置写在webconfig文件中,位置如下: w ...
- asp.net用户自定义控件传参
asp.net自定义控件传参的方式有2中: ①字段的方式 在自定义控件的.ascx.cs中定义一个字段,然后在调用页面的page_load方法里面传入参数. 如 在自定义控件中设置字段 publ ...
- WinForm用户自定义控件,在主窗体加载时出现闪烁;调用用户控件出现闪烁,需要鼠标才能够显示
转载自:http://www.dotblogs.com.tw/rainmaker/archive/2012/02/22/69811.aspx 解决方案: 在调用用户控件的窗体里面添加一下代码: pro ...
- Silverlight用户自定义控件件中增加属性和方法
下面的例子在用户控件MyCpmzSelect中增加了一个myCaption属性 public static readonly DependencyProperty myCaptionProperty ...
随机推荐
- 旧机器安装ArchLinux的各种问题
昨天突然想到家里还有一台很早之前不用的计算机 于是打算安装一个linux,开学再拿到宿舍用来写代码,怎么说台式机显示屏也比笔记本的大 机器安装问题 屏幕机箱擦干净,该连的东西都连上,然后插电源,本以为 ...
- 由防止表单重复提交引发的一系列问题--servletRequest的复制、body值的获取
@Time:2019年1月4日 16:19:19 @Author:QGuo 背景:最开始打算写个防止表单重复提交的拦截器:网上见到一种不错的方式,比较合适前后端分离,校验在后台实现: 我在此基础上 ...
- 使用 swoole_process 实现 PHP 进程池
swoole_process 主要是用来代替 PHP 的 pcntl 扩展.我们知道 pcntl 是用来进行多进程编程的,而 pcntl 只提供了 fork 这样原始的接口,容易使用错误,并且没有提供 ...
- Apache CXF实战之二 集成Sping与Web容器
本文链接:http://blog.csdn.net/kongxx/article/details/7525481 Apache CXF实战之一 Hello World Web Service 书接上文 ...
- 洛谷 P3467 [POI2008]PLA-Postering
P3467 [POI2008]PLA-Postering 题目描述 All the buildings in the east district of Byteburg were built in a ...
- 基于ArcGIS Flex API实现动态标绘(1.2)
动态标绘API 1.2,相较前一版本号(点击进入),该版本号新增对基本标绘符号的支持,包含: 单点.多点.折线.手绘线.多边形.手绘多边形.矩形,并提供对应的编辑功能. 例如以下图所看到的,对多点的编 ...
- Yocto tips (10): Yocto hellworld 加入一个软件包
Yocto中一个软件包是放在bb文件里的,然后非常多的bb文件集成一个recipe(配方),然后很多的recipe又组成一个meta layer.因此,要加入一个包事实上就是在recipe以下加入一个 ...
- Ubuntu系统下的Mysql安装与使用
摘要 在本篇博文中.笔者将从基础出发.介绍Mysql在Linux环境下的安装和基本使用命令,仅适用于Mysql刚開始学习的人.大牛请绕道-- 安装Mysql数据库 这里介绍最最简单的安装方式,至于编译 ...
- ProFTPD 的 mod_lang模块
ProFTPD 的 mod_lang模块http://www.proftpd.org/docs/modules/mod_lang.html安装该mod_lang模块随ProFTPD一起分发.要在pro ...
- BZOJ3511: 土地划分
[传送门:BZOJ3511] 简要题意: 给出n个点,m条边,每个点有A和B两种形态,一开始1为A,n为B 给出VA[i]和VB[i],表示第i个点选择A和B形态的价值 每条边给出x,y,EA,EB, ...