下面来开发一个LED指示灯控件,如下:

设计属性包括:

外环宽度,外环间隙,内环间隙,颜色【五种】,当前值。

由于该LED指示灯基本是完全独立设计的,并不是在某个控件的基础上进行的开发,因此,这里使

用用户控件的方式进行开发。通过GDI+方式对控件进行绘制。GDI的坐标系如下:

首先绘制外环,然后绘制内圆。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace JSControl
{
public partial class LedControl : UserControl
{
public LedControl()
{
InitializeComponent();
this.Size = new Size(40,40);//初始化控件大小
} #region Filed
private float outWidth = 4.0f;
[Browsable(true)]
[Category("自定义属性")]
[Description("外环宽度")]
public float OutWidth
{
get { return outWidth; }
set
{
if (value <= 0 || value > 0.1f * this.Width)
{
return;
}
outWidth = value;
this.Invalidate();
}
} private float outGap = 3.0f;
[Browsable(true)]
[Category("自定义属性")]
[Description("外环间隙")]
public float OutGap
{
get { return outGap; }
set
{
if (value <= 0 || value > 0.1f * this.Width || inGap <= value)
{
return;
}
outGap = value; this.Invalidate();
}
} private float inGap = 8.0f;
[Browsable(true)]
[Category("自定义属性")]
[Description("内环间隙")]
public float InGap
{
get { return inGap; }
set
{
if (value <= 0 || value <= outGap)
{
return;
}
inGap = value;
this.Invalidate();
}
} private Color color1 = Color.Gray;
[Browsable(true)]
[Category("自定义属性")]
[Description("指示灯颜色")]
public Color Color1
{
get { return color1; }
set
{
color1 = value;
this.Invalidate();
}
} private Color color2 = Color.LimeGreen;
[Browsable(true)]
[Category("自定义属性")]
[Description("LimeGreen")]
public Color Color2
{
get { return color2; }
set
{
color2 = value;
this.Invalidate();
}
} private Color color3 = Color.Red;
[Browsable(true)]
[Category("自定义属性")]
[Description("Red")]
public Color Color3
{
get { return color3; }
set
{
color3 = value;
this.Invalidate();
}
} private Color color4 = Color.DarkGoldenrod;
[Browsable(true)]
[Category("自定义属性")]
[Description("DarkGoldenrod")]
public Color Color4
{
get { return color4; }
set
{
color4 = value;
this.Invalidate();
}
} private Color color5 = Color.Blue;
[Browsable(true)]
[Category("自定义属性")]
[Description("Blue")]
public Color Color5
{
get { return color5; }
set
{
color5 = value;
this.Invalidate();
}
} //指示灯颜色索引
private int currentValue = 0;
[Browsable(true)]
[Category("自定义属性")]
[Description("当前值")]
public int CurrentValue
{
get { return currentValue; }
set
{
if (value > 4 || value < 0)
{
return;
}
currentValue = value;
this.Invalidate();
}
}
#endregion #region verride
private Graphics g; private Pen p;//画笔 private SolidBrush sb;//画刷 private int width;//控件宽度 private int height;//控件高度 protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
g = e.Graphics;
width = this.Width;
height = this.Height;
//这里是为了设置渲染效果,消除锯齿,高效果显示
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
if (inGap>0.5f*this.Height||inGap>0.5f*this.Width)
{
return;
}
Color CurrentColor = GetCurrentColor(this.currentValue);
//设置画笔的宽度
p = new Pen(CurrentColor, outWidth);
//先绘制外环
RectangleF rec = new RectangleF(outGap, outGap, this.width - 2 * outGap, this.height - 2 * outGap);
g.DrawEllipse(p, rec);
sb = new SolidBrush(CurrentColor);
//再绘制内圆
rec = new RectangleF(inGap, inGap, this.width - 2 * inGap, this.height - 2 * inGap);
g.FillEllipse(sb, rec); }
#endregion #region Private Methods
/// <summary>
/// 设置控件颜色
/// </summary>
/// <returns></returns>
private Color GetCurrentColor(int currentColor)
{
List<Color> ColorList = new List<Color>();
ColorList.Add(color1);
ColorList.Add(color2);
ColorList.Add(color3);
ColorList.Add(color4);
ColorList.Add(color5);
return ColorList[currentValue];
} #endregion
}
}

this.Invalidate()表示进行重绘,调用它后将会执行

protected override void OnPaint(PaintEventArgs e){}中的代码进行重绘。

生成后,将和其他控件一样可以看到其属性。

C#自定义控件开发(2)—LED指示灯的更多相关文章

  1. newifi mini将led指示灯引出当gpio使用

    之前买了个newifi mini的路由器,CPU是mt7620a的,有7个led指示灯.现在想要把控制led灯的gpio引出来,方便其他驱动或应用的开发. 一.硬件部分 1.联想路由 现在想要把USB ...

  2. iOS 自定义控件开发(中)

    <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> 接上篇iOS自定义控件开发之后,我们尝试另外一种. 在Xcode的右边,会看到如下的图 其中,上面有一个:C ...

  3. iOS 自定义控件开发(上)

    工作需要,最近在进行iOS方面的图表工作.找了很多第三方库都无法实现效果,所以决定自己写一个控件. <iOS 自定义控件开发(上)> <iOS 自定义控件开发(中)> #0 目 ...

  4. C#自定义控件开发

    自定义控件开发 一般而言,Visual Studio 2005中自带的几十种控件已经足够我们使用了,但是,在一些特殊的需求中,可能需要一些特殊的控件来与用户进行交互,这时,就需要我们自己开发新的.满足 ...

  5. led指示灯电路图大全(八款led指示灯电路设计原理图详解)

    led指示灯电路图大全(八款led指示灯电路设计原理图详解) led指示灯电路图(一) 图1所示电路中只有两个元件,R选用1/6--1/8W碳膜电阻或金属膜电阻,阻值在1--300K之间. Ne为氖泡 ...

  6. 自定义控件开发的调试及DesignMode的状态处理

    在开发Winform程序的时候,我们往往需要根据需要做一些自定义的控件模块,这样可以给系统模块重复利用,或者实现更好的效果等功能.但在使用的时候,我们又往往设计时刻发现一些莫名其妙的错误,那么我们该如 ...

  7. ZLComboBox自定义控件开发详解

    [引言]距离上一回写博客已经有一些时日了,之前的爱莲iLinkIT系列主要是讲解了如何用NodeJS来实现一个简单的“文件传送”软件,属于JavaScript中在服务器端的应用. 今天,我们就回归到J ...

  8. .net的自定义JS控件,运用了 面向对象的思想 封装 了 控件(.net自定义控件开发的第一天)

    大家好!我叫刘晶,很高兴你能看到我分享的文章!希望能对你有帮助! 首先我们来看下几个例子 ,就能看到 如何 自定义控件! 业务需求: 制作  一个   属于 自己的    按钮 对象    ,然后 像 ...

  9. s3c6410开发板LED驱动程序设计详细…

    2 下面来看看tiny6410关于LED的原理图如图(1)所示: 图1    LED原理图 3 LED实例,代码如下所示:(代码摘自\光盘4\实验代码\3-3-1\src\main.c) main.c ...

随机推荐

  1. 「学习笔记」斜率优化dp

    目录 算法 例题 任务安排 题意 思路 代码 [SDOI2012]任务安排 题意 思路 代码 任务安排 再改 题意 思路 练习题 [HNOI2008]玩具装箱 思路 代码 [APIO2010]特别行动 ...

  2. pod资源的健康检查-readiness探针的httpGet使用

    livenessProbe:健康状态检查,周期性检查服务是否存活,检查结果失败,将重启容器 readinessProbe:可用性检查,周期性检查服务是否可用,不可用将从service的endpoint ...

  3. yield功能分析

    下面是示例 输出结果: starting... 4 ******************** res: None 4

  4. Linux_more_less总结

    先写结论 : less is more,使用less 优于使用more more 和 less的区别 优于more不能后退,而less 就在其基础上增加了后退功能 less 可以使用键盘上的上下方向键 ...

  5. js之页面列表加载常用方法总结

    导语:最近由于一些事情需要处理,所以没来得及写技术总结了.今天终于可以坐下来好好的梳理一下脉络,说一下那个在日常前端开发过程中,常用到的页面列表加载的方法总结.这里介绍三种方法,分别是分页加载.按钮加 ...

  6. js中的对象和数组的创建

    <!DOCTYPE html><html><head> <title>获取控制</title> <meta charset=" ...

  7. 【debug】 Linux中top的使用

    在我们日常的开发中,我们经常需要查看每个线程的cpu使用情况.其实,在linux中,top也是我们查看cpu使用状况的一个好帮手 top:先查看每一个进程的使用状况 我们可以发现PID:3800这个经 ...

  8. 基于深度学习的文本分类案例:使用LSTM进行情绪分类

    Sentiment classification using LSTM 在这个笔记本中,我们将使用LSTM架构在电影评论数据集上训练一个模型来预测评论的情绪.首先,让我们看看什么是LSTM? LSTM ...

  9. google浏览器个人常用快捷键

    分享一下个人常用快捷键. 说明:字母排序规则遵循字母表(a->z) 快捷键 介绍 ctrl+0 恢复页面到100% ctrl+数字(1~9) 切换至序号对应的标签页 ctrl+d 将当前标签页添 ...

  10. C#并发编程-4 同步

    如果程序用到了并发技术,那就要特别留意这种情况:一段代码需要修改数据,同时其他代码需要访问同一个数据. 这种情况就需要考虑同步地访问数据. 如果下面三个条件都满足,就必须用同步来保护共享的数据. 多段 ...