(六十一)c#Winform自定义控件-信号灯(工业)-HZHControls
官网
前提
入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。
GitHub:https://github.com/kwwwvagaa/NetWinformControl
码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
如果觉得写的还行,请点个 star 支持一下吧
欢迎前来交流探讨: 企鹅群568015492 
麻烦博客下方点个【推荐】,谢谢
NuGet
Install-Package HZH_Controls
目录
https://www.cnblogs.com/bfyx/p/11364884.html
用处及效果

准备工作
依然使用GDI+绘画,这个比较简单,就是画圆
开始
新增一个类UCSignalLamp,继承UserControl
添加属性
/// <summary>
/// The is show border
/// </summary>
private bool isShowBorder = false; /// <summary>
/// Gets or sets a value indicating whether this instance is show border.
/// </summary>
/// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
[Description("是否显示边框"), Category("自定义")]
public bool IsShowBorder
{
get { return isShowBorder; }
set
{
isShowBorder = value;
Refresh();
}
} /// <summary>
/// The lamp color
/// </summary>
private Color[] lampColor = new Color[] { Color.FromArgb(, , ) }; /// <summary>
/// Gets or sets the color of the lamp.
/// </summary>
/// <value>The color of the lamp.</value>
[Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
public Color[] LampColor
{
get { return lampColor; }
set
{
if (value == null || value.Length <= )
return;
lampColor = value;
Refresh();
}
} /// <summary>
/// The is highlight
/// </summary>
private bool isHighlight = true; /// <summary>
/// Gets or sets a value indicating whether this instance is highlight.
/// </summary>
/// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
[Description("是否高亮显示"), Category("自定义")]
public bool IsHighlight
{
get { return isHighlight; }
set
{
isHighlight = value;
Refresh();
}
} /// <summary>
/// The twinkle speed
/// </summary>
private int twinkleSpeed = ; /// <summary>
/// Gets or sets the twinkle speed.
/// </summary>
/// <value>The twinkle speed.</value>
[Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
public int TwinkleSpeed
{
get { return twinkleSpeed; }
set
{
if (value < )
return;
twinkleSpeed = value;
if (value == || lampColor.Length <= )
{
timer.Enabled = false;
}
else
{
intColorIndex = ;
timer.Interval = value;
timer.Enabled = true;
}
Refresh();
}
}
/// <summary>
/// The timer
/// </summary>
Timer timer;
/// <summary>
/// The int color index
/// </summary>
int intColorIndex = ;
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
Color c1 = lampColor[intColorIndex];
g.FillEllipse(new SolidBrush(c1), this.ClientRectangle); if (isHighlight)
{
GraphicsPath gp = new GraphicsPath(); Rectangle rec = new Rectangle(, , this.Width - , this.Height - );
gp.AddEllipse(rec); Color[] surroundColor = new Color[] { c1 };
PathGradientBrush pb = new PathGradientBrush(gp);
pb.CenterColor = Color.White;
pb.SurroundColors = surroundColor;
g.FillPath(pb, gp);
} if (isShowBorder)
{
g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), ), new Rectangle(, , this.Width - , this.Height - ));
}
}
全部代码
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-09
//
// ***********************************************************************
// <copyright file="UCSignalLamp.cs">
// Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
// </copyright>
//
// Blog: https://www.cnblogs.com/bfyx
// GitHub:https://github.com/kwwwvagaa/NetWinformControl
// gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
//
// If you use this code, please keep this note.
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.ComponentModel; namespace HZH_Controls.Controls.FactoryControls.Lamp
{
/// <summary>
/// Class UCSignalLamp.
/// Implements the <see cref="System.Windows.Forms.UserControl" />
/// </summary>
/// <seealso cref="System.Windows.Forms.UserControl" />
public class UCSignalLamp : UserControl
{
/// <summary>
/// The is show border
/// </summary>
private bool isShowBorder = false; /// <summary>
/// Gets or sets a value indicating whether this instance is show border.
/// </summary>
/// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
[Description("是否显示边框"), Category("自定义")]
public bool IsShowBorder
{
get { return isShowBorder; }
set
{
isShowBorder = value;
Refresh();
}
} /// <summary>
/// The lamp color
/// </summary>
private Color[] lampColor = new Color[] { Color.FromArgb(, , ) }; /// <summary>
/// Gets or sets the color of the lamp.
/// </summary>
/// <value>The color of the lamp.</value>
[Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
public Color[] LampColor
{
get { return lampColor; }
set
{
if (value == null || value.Length <= )
return;
lampColor = value;
Refresh();
}
} /// <summary>
/// The is highlight
/// </summary>
private bool isHighlight = true; /// <summary>
/// Gets or sets a value indicating whether this instance is highlight.
/// </summary>
/// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
[Description("是否高亮显示"), Category("自定义")]
public bool IsHighlight
{
get { return isHighlight; }
set
{
isHighlight = value;
Refresh();
}
} /// <summary>
/// The twinkle speed
/// </summary>
private int twinkleSpeed = ; /// <summary>
/// Gets or sets the twinkle speed.
/// </summary>
/// <value>The twinkle speed.</value>
[Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
public int TwinkleSpeed
{
get { return twinkleSpeed; }
set
{
if (value < )
return;
twinkleSpeed = value;
if (value == || lampColor.Length <= )
{
timer.Enabled = false;
}
else
{
intColorIndex = ;
timer.Interval = value;
timer.Enabled = true;
}
Refresh();
}
}
/// <summary>
/// The timer
/// </summary>
Timer timer;
/// <summary>
/// The int color index
/// </summary>
int intColorIndex = ;
/// <summary>
/// Initializes a new instance of the <see cref="UCSignalLamp"/> class.
/// </summary>
public UCSignalLamp()
{
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.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Size = new Size(, );
this.SizeChanged += UCSignalLamp_SizeChanged;
timer = new Timer();
timer.Interval = ;
timer.Tick += timer_Tick;
} /// <summary>
/// Handles the Tick event of the timer control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void timer_Tick(object sender, EventArgs e)
{
intColorIndex++;
if (intColorIndex >= lampColor.Length)
intColorIndex = ;
Refresh();
}
/// <summary>
/// Handles the SizeChanged event of the UCSignalLamp control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
void UCSignalLamp_SizeChanged(object sender, EventArgs e)
{
var maxSize = Math.Min(this.Width, this.Height);
if (this.Width != maxSize)
this.Width = maxSize;
if (this.Height != maxSize)
this.Height = maxSize;
} /// <summary>
/// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
/// </summary>
/// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh();
Color c1 = lampColor[intColorIndex];
g.FillEllipse(new SolidBrush(c1), this.ClientRectangle); if (isHighlight)
{
GraphicsPath gp = new GraphicsPath(); Rectangle rec = new Rectangle(, , this.Width - , this.Height - );
gp.AddEllipse(rec); Color[] surroundColor = new Color[] { c1 };
PathGradientBrush pb = new PathGradientBrush(gp);
pb.CenterColor = Color.White;
pb.SurroundColors = surroundColor;
g.FillPath(pb, gp);
} if (isShowBorder)
{
g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), ), new Rectangle(, , this.Width - , this.Height - ));
}
}
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(六十一)c#Winform自定义控件-信号灯(工业)-HZHControls的更多相关文章
- (十六)c#Winform自定义控件-文本框哪里去了?-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (四十六)c#Winform自定义控件-水波进度条-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (二十六)c#Winform自定义控件-有确定取消的窗体(二)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (八十六)c#Winform自定义控件-表格优化
出处:http://www.hzhcontrols.com/原文:http://www.hzhcontrols.com/blog-149.html本文版权归www.hzhcontrols.com所有欢 ...
- (五十六)c#Winform自定义控件-瓶子(工业)-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (八十一)c#Winform自定义控件-时间轴-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (三十六)c#Winform自定义控件-步骤控件-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (五十一)c#Winform自定义控件-文字提示-HZHControls
官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...
- (六)c#Winform自定义控件-单选框
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
随机推荐
- Django之视图层与模板层
目录 视图层 小白必会三板斧 HttpResponse render redirect JsonResponse 前后端分离 FBV CBV 给CBV加装饰器 模板层 模板语法 模板传值 过滤器 语法 ...
- 2016/10/13 oracle中的round()
语法: ROUND(number,num_digits) 其中Number是需要进行四舍五入的数字:Num_digits为指定的位数,按此位数进行四舍五入,如果 num_digits 大于 0,则四舍 ...
- 解决visual studio换行(回车键)不能代码补全问题
打开工具--选项:将标红的位置改为true即可.
- [ASP.NET Core 3框架揭秘] 文件系统[3]:物理文件系统
ASP.NET Core应用中使用得最多的还是具体的物理文件,比如配置文件.View文件以及作为Web资源的静态文件.物理文件系统由定义在NuGet包"Microsoft.Extension ...
- 消息队列MQ简介
项目中要用到RabbitMQ,领导让我先了解一下.在之前的公司中,用到过消息队列MQ,阿里的那款RocketMQ,当时公司也做了简单的技术分享,自己也看了一些博客.自己在有道云笔记上,做了一些整理,但 ...
- 利用百度AI快速开发出一款“问答机器人”并接入小程序
先看实现效果: 利用百度UNIT预置的智能问答技能和微信小程序,实现语音问答机器人.这里主要介绍小程序功能开发实现过程,分享主要功能实现的子程序模块,都是干货! 想了解UNIT预置技能调用,请参看我之 ...
- 阿里云ECS部署Redis主备哨兵集群遇到的问题
一.部署 详细部署步骤:https://blog.csdn.net/lihongtai/article/details/82826809 Redis5.0版本需要注意的参数配置:https://www ...
- Docker设置镜像加速
一.为什么要设置镜像加速 由于docker的镜像源地址再国外,例如官方地址:https://hub.docker.com/search?q=hyperledger&type=image:因此下 ...
- Python爬虫基础面试题,为2020年初就业做准备
前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:qiaoziheng 一.题目部分1.python中常用的数据结构有哪些 ...
- python字符串与字典转换
经常会遇到字典样式字符串的处理,这里做一下记录. load load针对的是文件,即将文件内的json内容转换为dict import json test_json = json.load(open( ...