(六十二)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

添加属性

 1  /// <summary>
 2         /// The lamp color
 3         /// </summary>
 4         private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
 5
 6         /// <summary>
 7         /// Gets or sets the color of the lamp.
 8         /// </summary>
 9         /// <value>The color of the lamp.</value>
10         [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
11         public Color[] LampColor
12         {
13             get { return lampColor; }
14             set
15             {
16                 if (value == null || value.Length <= 0)
17                     return;
18                 lampColor = value;
19                 Refresh();
20             }
21         }
22
23         /// <summary>
24         /// The lampstand
25         /// </summary>
26         private Color lampstand = Color.FromArgb(105, 105, 105);
27
28         /// <summary>
29         /// Gets or sets the lampstand.
30         /// </summary>
31         /// <value>The lampstand.</value>
32         [Description("灯座颜色"), Category("自定义")]
33         public Color Lampstand
34         {
35             get { return lampstand; }
36             set { lampstand = value; }
37         }
38
39         /// <summary>
40         /// The twinkle speed
41         /// </summary>
42         private int twinkleSpeed = 0;
43
44         /// <summary>
45         /// Gets or sets the twinkle speed.
46         /// </summary>
47         /// <value>The twinkle speed.</value>
48         [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
49         public int TwinkleSpeed
50         {
51             get { return twinkleSpeed; }
52             set
53             {
54                 if (value < 0)
55                     return;
56                 twinkleSpeed = value;
57                 if (value == 0 || lampColor.Length <= 1)
58                 {
59                     timer.Enabled = false;
60                 }
61                 else
62                 {
63                     intColorIndex = 0;
64                     timer.Interval = value;
65                     timer.Enabled = true;
66                 }
67                 Refresh();
68             }
69         }
70         /// <summary>
71         /// The timer
72         /// </summary>
73         Timer timer;
74         /// <summary>
75         /// The int color index
76         /// </summary>
77         int intColorIndex = 0;
78         /// <summary>
79         /// The m rect working
80         /// </summary>
81         Rectangle m_rectWorking;

重绘

 1  protected override void OnPaint(PaintEventArgs e)
 2         {
 3             base.OnPaint(e);
 4             var g = e.Graphics;
 5             g.SetGDIHigh();
 6
 7             Color c1 = lampColor[intColorIndex];
 8             GraphicsPath path = new GraphicsPath();
 9             path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom), new Point(m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width));
10             path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);
11             path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width), new Point(m_rectWorking.Right, m_rectWorking.Bottom));
12             path.CloseAllFigures();
13             g.FillPath(new SolidBrush(c1), path);
14
15             g.FillRectangle(new SolidBrush(lampstand), new Rectangle(5, m_rectWorking.Bottom - 19, this.Width - 10, 10));
16             g.FillRectangle(new SolidBrush(lampstand), new Rectangle(0, m_rectWorking.Bottom - 10, this.Width, 10));
17         }

完整代码

  1 // ***********************************************************************
  2 // Assembly         : HZH_Controls
  3 // Created          : 2019-09-10
  4 //
  5 // ***********************************************************************
  6 // <copyright file="UCAlarmLamp.cs">
  7 //     Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:[email protected]
  8 // </copyright>
  9 //
 10 // Blog: https://www.cnblogs.com/bfyx
 11 // GitHub:https://github.com/kwwwvagaa/NetWinformControl
 12 // gitee:https://gitee.com/kwwwvagaa/net_winform_custom_control.git
 13 //
 14 // If you use this code, please keep this note.
 15 // ***********************************************************************
 16 using System;
 17 using System.Collections.Generic;
 18 using System.Linq;
 19 using System.Text;
 20 using System.Windows.Forms;
 21 using System.Drawing;
 22 using System.Drawing.Drawing2D;
 23 using System.ComponentModel;
 24
 25 namespace HZH_Controls.Controls
 26 {
 27     /// <summary>
 28     /// Class UCAlarmLamp.
 29     /// Implements the <see cref="System.Windows.Forms.UserControl" />
 30     /// </summary>
 31     /// <seealso cref="System.Windows.Forms.UserControl" />
 32     public class UCAlarmLamp : UserControl
 33     {
 34         /// <summary>
 35         /// The lamp color
 36         /// </summary>
 37         private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
 38
 39         /// <summary>
 40         /// Gets or sets the color of the lamp.
 41         /// </summary>
 42         /// <value>The color of the lamp.</value>
 43         [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
 44         public Color[] LampColor
 45         {
 46             get { return lampColor; }
 47             set
 48             {
 49                 if (value == null || value.Length <= 0)
 50                     return;
 51                 lampColor = value;
 52                 Refresh();
 53             }
 54         }
 55
 56         /// <summary>
 57         /// The lampstand
 58         /// </summary>
 59         private Color lampstand = Color.FromArgb(105, 105, 105);
 60
 61         /// <summary>
 62         /// Gets or sets the lampstand.
 63         /// </summary>
 64         /// <value>The lampstand.</value>
 65         [Description("灯座颜色"), Category("自定义")]
 66         public Color Lampstand
 67         {
 68             get { return lampstand; }
 69             set { lampstand = value; }
 70         }
 71
 72         /// <summary>
 73         /// The twinkle speed
 74         /// </summary>
 75         private int twinkleSpeed = 0;
 76
 77         /// <summary>
 78         /// Gets or sets the twinkle speed.
 79         /// </summary>
 80         /// <value>The twinkle speed.</value>
 81         [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
 82         public int TwinkleSpeed
 83         {
 84             get { return twinkleSpeed; }
 85             set
 86             {
 87                 if (value < 0)
 88                     return;
 89                 twinkleSpeed = value;
 90                 if (value == 0 || lampColor.Length <= 1)
 91                 {
 92                     timer.Enabled = false;
 93                 }
 94                 else
 95                 {
 96                     intColorIndex = 0;
 97                     timer.Interval = value;
 98                     timer.Enabled = true;
 99                 }
100                 Refresh();
101             }
102         }
103         /// <summary>
104         /// The timer
105         /// </summary>
106         Timer timer;
107         /// <summary>
108         /// The int color index
109         /// </summary>
110         int intColorIndex = 0;
111         /// <summary>
112         /// The m rect working
113         /// </summary>
114         Rectangle m_rectWorking;
115         /// <summary>
116         /// Initializes a new instance of the <see cref="UCAlarmLamp"/> class.
117         /// </summary>
118         public UCAlarmLamp()
119         {
120             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
121             this.SetStyle(ControlStyles.DoubleBuffer, true);
122             this.SetStyle(ControlStyles.ResizeRedraw, true);
123             this.SetStyle(ControlStyles.Selectable, true);
124             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
125             this.SetStyle(ControlStyles.UserPaint, true);
126             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
127             this.SizeChanged += UCAlarmLamp_SizeChanged;
128             this.Size = new Size(50, 50);
129             timer = new Timer();
130             timer.Interval = 200;
131             timer.Tick += timer_Tick;
132         }
133
134         /// <summary>
135         /// Handles the SizeChanged event of the UCAlarmLamp control.
136         /// </summary>
137         /// <param name="sender">The source of the event.</param>
138         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
139         void UCAlarmLamp_SizeChanged(object sender, EventArgs e)
140         {
141             m_rectWorking = new Rectangle(10, 0, this.Width - 20, this.Height);
142         }
143         /// <summary>
144         /// Handles the Tick event of the timer control.
145         /// </summary>
146         /// <param name="sender">The source of the event.</param>
147         /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
148         void timer_Tick(object sender, EventArgs e)
149         {
150             intColorIndex++;
151             if (intColorIndex >= lampColor.Length)
152                 intColorIndex = 0;
153             Refresh();
154         }
155         /// <summary>
156         /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
157         /// </summary>
158         /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
159         protected override void OnPaint(PaintEventArgs e)
160         {
161             base.OnPaint(e);
162             var g = e.Graphics;
163             g.SetGDIHigh();
164
165             Color c1 = lampColor[intColorIndex];
166             GraphicsPath path = new GraphicsPath();
167             path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom), new Point(m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width));
168             path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);
169             path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width), new Point(m_rectWorking.Right, m_rectWorking.Bottom));
170             path.CloseAllFigures();
171             g.FillPath(new SolidBrush(c1), path);
172
173             g.FillRectangle(new SolidBrush(lampstand), new Rectangle(5, m_rectWorking.Bottom - 19, this.Width - 10, 10));
174             g.FillRectangle(new SolidBrush(lampstand), new Rectangle(0, m_rectWorking.Bottom - 10, this.Width, 10));
175         }
176     }
177 }

最后的话

如果你喜欢的话,请到 https://gitee.com/kwwwvagaa/net_winform_custom_control 点个星星吧

原文地址:https://www.cnblogs.com/webenh/p/11496848.html

时间: 2024-10-12 03:13:12

(六十二)c#Winform自定义控件-警灯(工业)的相关文章

(六十)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_Con

QT开发(六十二)———QT5解析Json文件

QT开发(六十二)---QT5解析Json文件 一.QT5 Json简介 QT4中使用第三方库QJson解析JSON文件. QT5新增加了处理JSON的类,类均以QJson开头,包含在QtCore模块中.QT5新增加六个相关类: QJsonArray 封装 JSON 数组 QJsonDocument 读写 JSON 文档 QJsonObject 封装 JSON 对象 QJsonObject::iterator 用于遍历QJsonObject的STL风格的非const遍历器 QJsonParseE

java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)

java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessController的checkPerssiom方法,访问控制器AccessController的栈检查机制又遍历整个 PerssiomCollection来判断具体拥有什么权限一旦发现栈中一个权限不允许的时候抛出异常否则简单的返回,这个过程实际上比我的描述要复杂 得多,这里我只是简单的一句带过,因为这

六十二、负载均衡集群介绍、LVS介绍、LVS的调度算法、LVS NAT模式搭建

一.负载均衡集群介绍 主流开源软件LVS.keepalived.haproxy.nginx等 其中LVS属于4层(网络OSI 7层模型),nginx属于7层,haproxy既可以认为是4层,也可以当做7层使用. OSI简介:OSI采用了分层的结构化技术,共分七层,物理层.数据链路层.网络层.传输层.会话层.表示层.应用层. keepalived的负载均衡功能其实就是lvs,内置的功能. lvs这种4层的负载均衡是可以分发除80外的其他端口通信的,比如MySQL的,而nginx仅仅支持http,h

剑指offer(六十二)之二叉搜索树的第k个结点

题目描述 给定一颗二叉搜索树,请找出其中的第k大的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. 代码: <span style="color:#cc33cc;">/* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val;

JAVA学习第六十二课 — TCP协议练习

通过练习掌握TCP在进行传输过程中的问题 练习1:创建一个英文大写转换服务器 客户端输入字母数据,发送给服务端,服务端收到后显示到控制台,并将该数据转成大写返回客户端,知道客户端输入over,转换结束 public class Main { public static void main(String[] args) throws IOException{ Text_Transform_Client(); Text_Transform_Server(); } public static void

Android实战简易教程-第六十九枪(自定义控件实现雪花飘落效果)

现在APP要求越来越高了,不只是要求实现功能,颜值的要求也越来越高,下面我们通过自定义控件来实现雪花飘落的效果,可以作为界面背景哦. 1.自定义控件: package com.test.a; import java.util.Random; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Can

python练习六十二:文件处理,往文件中所有添加指定的前缀

往文件中所有添加指定的前缀 方法一:open方法 f_r = open('text.txt') f_w = open('text_new.txt','w+') i = 0 while True: i += 1 line = f_r.readline() if not line: break f_w.write('%02d'%i + '.python'+ ' ' +line) f_r.close() f_w.close() f_wr = open('text_new.txt','r') lines

(十)c#Winform自定义控件-列表

前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control 如果觉得写的还行,请点个 star 支持一下吧 欢迎前来交流探讨: 企鹅群568015492  准备工作 列表控件将被拆分为2部分,一个元素,一个列表,列表需要支持主副标题,图标等 开始 首先定义一个数据源类(其实更好的是应该接受object,然后通过绑定字段反射绑定数据,这样就不需要这个数据源类