一、新建用户自定义控件

如下图所示,想通过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#之用户自定义控件的更多相关文章

  1. mvc 母版页、用户自定义控件

    母版页(Master) 1.母版页是与Controller无关的,母版页只是一个View文件,而没有任何Controller与之相对应. 2.其实在ASP.NET MVC中View的aspx与母版页并 ...

  2. WPF中添加Winform用户自定义控件

    过程:创建WPF工程->创建Winform用户自定义控件工程->WPF中引用控件->添加到Xaml页面 1.首先在WPF工程的解决方案上右击选择添加新建项目: 选择Windows窗体 ...

  3. C#用户自定义控件(含源代码)-透明文本框

    using System; using System.Collections; using System.ComponentModel; using System.Drawing; using Sys ...

  4. Winform中用户自定义控件中外部设置子控件属性的方法

    假设我们新建立一个用户自定义控件,由一个lable1和pictureBox1组成 此时我们在外部调用该控件,然后想动态改变lable1的值,我们该怎么办? 假设实例化的用户控件名为UserContro ...

  5. Web用户自定义控件

    在新建项的时候,选择Web用户控件,可用来自定义自己的控件,做好后,直接拖到页面即可使用自定义控件与WEB交互,需要在 自定义控件里面 写 属性,如: public string CityID { g ...

  6. ASP.NET用户自定义控件配置

    一直以来开发中碰到要写自定义控件的时候总是习惯性的找度娘,而没有自己记住,结果今天就悲剧了,找了半天才找到,想想还是自己积累起来吧! 第一种配置方式: 配置写在webconfig文件中,位置如下: w ...

  7. asp.net用户自定义控件传参

    asp.net自定义控件传参的方式有2中: ①字段的方式 在自定义控件的.ascx.cs中定义一个字段,然后在调用页面的page_load方法里面传入参数. 如  在自定义控件中设置字段   publ ...

  8. WinForm用户自定义控件,在主窗体加载时出现闪烁;调用用户控件出现闪烁,需要鼠标才能够显示

    转载自:http://www.dotblogs.com.tw/rainmaker/archive/2012/02/22/69811.aspx 解决方案: 在调用用户控件的窗体里面添加一下代码: pro ...

  9. Silverlight用户自定义控件件中增加属性和方法

    下面的例子在用户控件MyCpmzSelect中增加了一个myCaption属性 public static readonly DependencyProperty myCaptionProperty ...

随机推荐

  1. TF.LSTM实现

    感悟:耗时最多的就是数据格式整理,其本身并不复杂 NN-LSTM-NN-SOFTMAX 数据格式:batch_size =>批大小,n_steps=>要建立多少lstm 0.原始输入数据格 ...

  2. win2003系统同步Linux ntp server批处理

    最后更新时间: 2018/12/15 一般windows配置时间服务器,只需要在windows系统右下角,点时间,里面配置好对应NTP服务器地址就行, 至多再修改一下注册表 HKEY_LOCAL_MA ...

  3. vue使用,问题

    参考链接:https://cn.vuejs.org/v2/guide/index.html *)[Vue warn]: Error in v-on handler: "TypeError: ...

  4. 内存,寄存器和cache的区别与联系

    1. 寄存器是中央处理器内的组成部份.寄存器是有限存贮容量的高速存贮部件,它们可用来暂存指令.数据和位址.在中央处理器的控制部件中,包含的寄存器有指令寄存器(IR)和程序计数器(PC).在中央处理器的 ...

  5. 实战Jquery(四)--标签页效果

            这两天完毕了实战四五六的样例,实例四是标签页的实现方法,实例五是级联菜单下拉框,实例六是窗体效果,都是web层经常使用的效果.越到后面越发认为技术这东西,就是一种思路的展现,懂了要实现 ...

  6. json的键为变量而不是字符串时,怎么写?

    看栗子 /* 首先你创建了一个window的属性叫b, 并给它赋值为'cccddd' * 然后你创建了一个对象"a", 声明了一个它的属性叫b, 并且给b赋值为6 * 注意第一行的 ...

  7. 学习中 常用到的string内置对象方法的总结

    //concat() – 将两个或多个字符的文本组合起来,返回一个新的字符串. var str = "Hello"; var out = str.concat(" Wor ...

  8. Integer 和 int的种种比较

    public static void main(String[] args) { int i = 128; Integer i2 = 128; Integer i3 = new Integer(128 ...

  9. avalon 作用域

    作用域绑定(ms-controller, ms-important) 如果一个页面非常复杂,就需要划分模块,每个模块交由不同的ViewModel去处理.我们就要用到ms-controller与ms-i ...

  10. c#(asp.net) 如何计算两个日期之间相隔天数

    1.DateTime t1 = Convert.ToDateTime("2006-1-6"); DateTime t2 = Convert.ToDateTime("200 ...