(六十二)c#Winform自定义控件-警灯(工业)
官网
前提
入行已经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+,不懂可以先百度了解下
开始
添加一个类UCAlarmLamp,继承自UserControl
添加属性
/// <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 lampstand
/// </summary>
private Color lampstand = Color.FromArgb(, , ); /// <summary>
/// Gets or sets the lampstand.
/// </summary>
/// <value>The lampstand.</value>
[Description("灯座颜色"), Category("自定义")]
public Color Lampstand
{
get { return lampstand; }
set { lampstand = value; }
} /// <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>
/// The m rect working
/// </summary>
Rectangle m_rectWorking;
重绘
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var g = e.Graphics;
g.SetGDIHigh(); Color c1 = lampColor[intColorIndex];
GraphicsPath path = new GraphicsPath();
path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom), new Point(m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width));
path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);
path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width), new Point(m_rectWorking.Right, m_rectWorking.Bottom));
path.CloseAllFigures();
g.FillPath(new SolidBrush(c1), path); g.FillRectangle(new SolidBrush(lampstand), new Rectangle(, m_rectWorking.Bottom - , this.Width - , ));
g.FillRectangle(new SolidBrush(lampstand), new Rectangle(, m_rectWorking.Bottom - , this.Width, ));
}
完整代码
// ***********************************************************************
// Assembly : HZH_Controls
// Created : 2019-09-10
//
// ***********************************************************************
// <copyright file="UCAlarmLamp.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
{
/// <summary>
/// Class UCAlarmLamp.
/// Implements the <see cref="System.Windows.Forms.UserControl" />
/// </summary>
/// <seealso cref="System.Windows.Forms.UserControl" />
public class UCAlarmLamp : UserControl
{
/// <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 lampstand
/// </summary>
private Color lampstand = Color.FromArgb(, , ); /// <summary>
/// Gets or sets the lampstand.
/// </summary>
/// <value>The lampstand.</value>
[Description("灯座颜色"), Category("自定义")]
public Color Lampstand
{
get { return lampstand; }
set { lampstand = value; }
} /// <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>
/// The m rect working
/// </summary>
Rectangle m_rectWorking;
/// <summary>
/// Initializes a new instance of the <see cref="UCAlarmLamp"/> class.
/// </summary>
public UCAlarmLamp()
{
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.SizeChanged += UCAlarmLamp_SizeChanged;
this.Size = new Size(, );
timer = new Timer();
timer.Interval = ;
timer.Tick += timer_Tick;
} /// <summary>
/// Handles the SizeChanged event of the UCAlarmLamp 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 UCAlarmLamp_SizeChanged(object sender, EventArgs e)
{
m_rectWorking = new Rectangle(, , this.Width - , this.Height);
}
/// <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>
/// 引发 <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];
GraphicsPath path = new GraphicsPath();
path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom), new Point(m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width));
path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);
path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width), new Point(m_rectWorking.Right, m_rectWorking.Bottom));
path.CloseAllFigures();
g.FillPath(new SolidBrush(c1), path); g.FillRectangle(new SolidBrush(lampstand), new Rectangle(, m_rectWorking.Bottom - , this.Width - , ));
g.FillRectangle(new SolidBrush(lampstand), new Rectangle(, m_rectWorking.Bottom - , this.Width, ));
}
}
}
最后的话
如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧
(六十二)c#Winform自定义控件-警灯(工业)的更多相关文章
- (六十)c#Winform自定义控件-鼓风机(工业)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- “全栈2019”Java第六十二章:接口与常量详解
难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...
- (二十)c#Winform自定义控件-有后退的窗体
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- (三十)c#Winform自定义控件-文本框(三)
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)
java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...
- (六十七)c#Winform自定义控件-柱状图
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- FastAPI(六十二)实战开发《在线课程学习系统》需求分析
前言 基础的分享我们已经分享了六十篇,那么我们这次分享开始将用一系列的文章分享实战课程.我们分享的系统是在线学习系统.我们会分成不同的模块进行分享.我们的目的是带着大家去用fastapi去实战一次,开 ...
- (五十)c#Winform自定义控件-滑块
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kwwwvagaa/NetWinformControl 码云:ht ...
- Spark2.x(六十二):(Spark2.4)共享变量 - Broadcast原理分析
之前对Broadcast有分析,但是不够深入<Spark2.3(四十三):Spark Broadcast总结>,本章对其实现过程以及原理进行分析. 带着以下几个问题去写本篇文章: 1)dr ...
随机推荐
- Oracle创建设置查询权限用户
用户创建的可以参考博客: https://blog.csdn.net/u014427391/article/details/84889023 Oracle授权表权限给用户: 语法:grant [权限名 ...
- 如何编写一个WebPack的插件原理及实践
_ 阅读目录 一:webpack插件的基本原理 二:理解 Compiler对象 和 Compilation 对象 三:插件中常用的API 四:编写插件实战 回到顶部 一:webpack插件的基本原理 ...
- Go中的并发编程和goroutine
并发编程对于任何语言来说都不是一件简单的事情.Go在设计之初主打高并发,为使用者提供了goroutine,使用的方式虽然简单,但是用好却不是那么容易,我们一起来学习Go中的并发编程. 1. 并行和并发 ...
- ReentrantLock源码分析--jdk1.8
JDK1.8 ArrayList源码分析--jdk1.8LinkedList源码分析--jdk1.8HashMap源码分析--jdk1.8AQS源码分析--jdk1.8ReentrantLock源码分 ...
- Java学习|强引用,软引用,弱引用,幻想引用有什么区别?
在Java语言中,除了基本数据类型外,其他的都是指向各类对象的对象引用:Java中根据其生命周期的长短,将引用分为4类. 1 强引用 特点:我们平常典型编码Object obj = new Objec ...
- 颜色下拉菜单(combox)
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...
- Winform 自定义文本框
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Django-channels 实现WebSocket实例
引入 先安装三个模块 pip install channels pip install channels_redis pip install pywin32 创建一个Django项目和一个app 项目 ...
- spark任务调度模式,动态资源分配
官网链接: http://spark.apache.org/docs/latest/job-scheduling.html 主要介绍: 1 application级调度方式 2 单个applicati ...
- RSA加密的java实现
首先科普一波: RSA的1024位是指公钥及私钥分别是1024bit,也就是1024/8=128 Bytes RSA算法密钥长度的选择是安全性和程序性能平衡的结果,密钥长度越长,安全性越好,加密解密所 ...