(六十二)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 ...
随机推荐
- QT状态机
首先吐槽下网上各种博主不清不楚的讲解 特别容易让新手迷惑 总体思想是这样的:首先要有一个状态机对象, 顾名思义,这玩意就是用来容纳状态的.然后调用状态机的start()函数它就会更具你的逻辑去执行相关 ...
- Why do I write a blog
I believe the most beautiful and elegant answer to this question is from Churchill. "On a peace ...
- java并发编程(五)----(JUC)ReentrantLock
上一节我们了解了Lock接口的一些简单的说明,知道Lock锁的常用形式,那么这节我们正式开始进入JUC锁(java.util.concurrent包下的锁,简称JUC锁).下面我们来看一下Lock最常 ...
- 为什么我们不用JIRA
很多人问我,缺陷管理工具,为什么不用jira?而去自己造轮子开发一款bug记录系统 缄默如我,原因众多.如果只是3-5分钟就能讲的请的时候,我会先列出什么糟点呢? 1. 收费,一个人一个月的费用差不多 ...
- java的八种数据类型
1)四种整数类型(byte.short.int.long): byte:8 位,用于表示最小数据单位,如文件中数据,-128~127 short:16 位,很少用,-32768 ~ 327 ...
- 虚拟机安装CentOS的简短教程
说明: 为什么要学Linux?因为现在互联网产品普遍使用Linux作为服务器系统. 测试工程师要学Linux吗?要,因为你会需要跟服务器打交道. 什么情况下测试工程师会跟服务器打交道?你可能要去部署测 ...
- Spring入门(八):自动装配的歧义性
1. 什么是自动装配的歧义性? 在Spring中,装配bean有以下3种方式: 自动装配 Java配置 xml配置 在这3种方式中,自动装配为我们带来了很大的便利,大大的降低了我们需要手动装配bean ...
- Mybatis中使用PageHelper插件进行分页
分页的场景比较常见,下面主要介绍一下使用PageHelper插件进行分页操作: 一.概述: PageHelper支持对mybatis进行分页操作,项目在github地址: https://github ...
- h5中div边距去除
style样式里面加上 <style> *{ margin:0 ;//外边距为0 padding:0;//内边距为0 } </style>
- springboot 整合shiro
参考: https://blog.csdn.net/fuweilian1/article/details/80309192(推荐) https://blog.csdn.net ...