下面来开发一个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. c++基础思维导图2

    c++基础思维导图2 结构体 结构体的基本概念:用户自定义的数据类型 结构体定义和使用 struct 结构体名{结构体成员} struct 结构体名 变量名: struct 结构体名 变量名 = {成 ...

  2. C语言:多功能计算器程序说明书

    好家伙,3000字终于写完了 一.题目:多功能科学计算器 二.内容: (1)概述或引言 开发环境为Visual C++ 目前已实现的功能: (1)解二元一次方程.一元二次方程 (2)进行矩阵相加.相减 ...

  3. 【android 逆向】破解crackme0502

    1.首先将crackme0502 拖入模拟器.打开应用,随意输出字符串 2. 将APK 拖入AndroidKiller 反编译 3.先查看androidmanifest.xml <?xml ve ...

  4. 放弃 Electron,拥抱 WebView2!JavaScript 快速开发独立 EXE 程序

    Electron 不错,但也不是完美的. Electron 带来了很多优秀的桌面软件,但并不一定总是适合我们的需求. 多个选择总是好事! 我使用 Electron 遇到的一些麻烦 1.Electron ...

  5. Erda 开源的迷失和反思

    前言 Erda 是我从2018年初加入上家公司直到今年初离开的四年时间里一直在做的一个云原生 PaaS 平台.在开源之前,Erda 在公司内部的名字代号是 D ,在21年初改名为现在的 Erda 进行 ...

  6. .NET 反向代理 YARP 跨域请求 CORS

    使用过 nginx 的小伙伴应该都知道,这个中间件是可以设置跨域的,作为今天的主角,同样的 反向代理中间件的 YARP 毫无意外也支持了跨域请求设置. 有些小伙伴可能会问了,怎样才算是跨域呢? 在 H ...

  7. 不可错过的效能利器「GitHub 热点速览 v.22.39」

    如果你是一名前端工程师且维护着多个网站,不妨试试本周榜上有名的 HTML-first 的 Qwik,提升网站访问速度只用一招.除了提升网站加载速度的 Qwik,本周周榜上榜的 Whisper 也是一个 ...

  8. 云数据库技术|“重磅升级”后再测 TDSQL-C

    来源:云数据库技术 标题 1.摘要 前段时间,测试了国内主要云原生数据库 PolarDB.TDSQL-C.GaussDB 的性能,参考:<再测云原生数据库性能>.在上次测试结果中,由于地域 ...

  9. 适用于移动端、PC 端 Vue.js 图片预览插件

    1.安装:npm install --save vue-picture-preview 2.使用: (1)入口文件中main.js中全局引入: import Vue from 'vue' import ...

  10. 洛谷P2863 [USACO06JAN]The Cow Prom S (tarjan)

    题目简述:一个有向图,求出这个图点数>1的强连通分量的个数. 那么就是tarjan求强联通分量的模板了. 记得要用一个数组标记节点是否在栈中. 1 #include<bits/stdc++ ...