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 ...
随机推荐
- 设置多行文本框不能拓展大小和span标签边框设置
resize: none;/*设置多行文本框,不能拓展大小*/ #span { display: block; border: 1px solid RGB(169,169,169); /* span标 ...
- NodeJS学习笔记 (28)流操作-stream(ok)
模块概览 nodejs的核心模块,基本上都是stream的的实例,比如process.stdout.http.clientRequest. 对于大部分的nodejs开发者来说,平常并不会直接用到str ...
- [Bug]Python3.x SyntaxError: 'ascii' codec can't decode byte 0xe4 in position
安装arch后就没再用python了 昨天管服务器的大佬在跑贝叶斯分类器的时候发现正确率有问题 我赶紧去做优化,然后就有这样的报错 Python 3.6.4 (default, Jan 5 2018, ...
- Lenovo k860i 移植Android 4.4 cm11进度记录【上篇已完结】
2014.5.16 为了验证一下下载的CM11的源码有没有问题,决定编译一下cm官方支持的机器,手上正好有台nexus7 2012,就拿它为例测试一下在mac os x平台的整个编译过程. 1. 最开 ...
- Linux下的ioctl()函数详解
我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围.写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑. ...
- java流1----InputStream、OutputStream、Reader、Writer
字节流和字符流 顾名思义,字节流就是操作的是字节,字符流操作的就是字符.其中字节流又可以分为字节输入流(InputStream)和字节输出流(OutputStream).同样的字符流也可以分为字符输入 ...
- NYOJ 541 最强的战斗力
最强DE 战斗力 时间限制:1000 ms | 内存限制:65535 KB 难度: 描写叙述 春秋战国时期,赵国地大物博,资源很丰富.人民安居乐业.但很多国家对它虎视眈眈.准备联合起来对赵国发起一 ...
- python学习之--SyntaxError: Non-ASCII character '\xe5'
在安装好eclipse之后试了一下 创建了了一个pydev project package.module 在test.py中编写最简单的命令 print "helloworld" ...
- Cordic 算法入门
三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常是通过从已知值(比如sin(π/2)= ...
- 字典(dictionary)与映射(map)
1. 字典:key-value 键值对 反转字典:reverse_dict = dict(zip(D.values(), D.keys())) 前提要保证 D 的 value 不会出现重复,因为字典反 ...