(六十二)c#Winform自定义控件-警灯(工业)
2021-02-03 18:17
前提
入行已经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 ///2 /// The lamp color 3 /// 4 private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) }; 5 6 ///7 /// Gets or sets the color of the lamp. 8 /// 9 ///The color of the lamp. 10 [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")] 11 public Color[] LampColor 12 { 13 get { return lampColor; } 14 set 15 { 16 if (value == null || value.Length 24 /// The lampstand 25 /// 26 private Color lampstand = Color.FromArgb(105, 105, 105); 27 28 ///29 /// Gets or sets the lampstand. 30 /// 31 ///The lampstand. 32 [Description("灯座颜色"), Category("自定义")] 33 public Color Lampstand 34 { 35 get { return lampstand; } 36 set { lampstand = value; } 37 } 38 39 ///40 /// The twinkle speed 41 /// 42 private int twinkleSpeed = 0; 43 44 ///45 /// Gets or sets the twinkle speed. 46 /// 47 ///The twinkle speed. 48 [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")] 49 public int TwinkleSpeed 50 { 51 get { return twinkleSpeed; } 52 set 53 { 54 if (value 71 /// The timer 72 /// 73 Timer timer; 74 ///75 /// The int color index 76 /// 77 int intColorIndex = 0; 78 ///79 /// The m rect working 80 /// 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 //7 // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com 8 // 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 ///28 /// Class UCAlarmLamp. 29 /// Implements the 31 ///30 /// 32 public class UCAlarmLamp : UserControl 33 { 34 /// 35 /// The lamp color 36 /// 37 private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) }; 38 39 ///40 /// Gets or sets the color of the lamp. 41 /// 42 ///The color of the lamp. 43 [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")] 44 public Color[] LampColor 45 { 46 get { return lampColor; } 47 set 48 { 49 if (value == null || value.Length 57 /// The lampstand 58 /// 59 private Color lampstand = Color.FromArgb(105, 105, 105); 60 61 ///62 /// Gets or sets the lampstand. 63 /// 64 ///The lampstand. 65 [Description("灯座颜色"), Category("自定义")] 66 public Color Lampstand 67 { 68 get { return lampstand; } 69 set { lampstand = value; } 70 } 71 72 ///73 /// The twinkle speed 74 /// 75 private int twinkleSpeed = 0; 76 77 ///78 /// Gets or sets the twinkle speed. 79 /// 80 ///The twinkle speed. 81 [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")] 82 public int TwinkleSpeed 83 { 84 get { return twinkleSpeed; } 85 set 86 { 87 if (value 104 /// The timer 105 /// 106 Timer timer; 107 ///108 /// The int color index 109 /// 110 int intColorIndex = 0; 111 ///112 /// The m rect working 113 /// 114 Rectangle m_rectWorking; 115 ///116 /// Initializes a new instance of the 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 ///class. 117 /// 135 /// Handles the SizeChanged event of the UCAlarmLamp control. 136 /// 137 /// The source of the event. 138 /// Theinstance containing the event data. 139 void UCAlarmLamp_SizeChanged(object sender, EventArgs e) 140 { 141 m_rectWorking = new Rectangle(10, 0, this.Width - 20, this.Height); 142 } 143 /// 144 /// Handles the Tick event of the timer control. 145 /// 146 /// The source of the event. 147 /// Theinstance containing the event data. 148 void timer_Tick(object sender, EventArgs e) 149 { 150 intColorIndex++; 151 if (intColorIndex >= lampColor.Length) 152 intColorIndex = 0; 153 Refresh(); 154 } 155 /// 156 /// 引发 158 /// 包含事件数据的事件。 157 /// 。 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 点个星星吧
(六十二)c#Winform自定义控件-警灯(工业)
标签:码云 ddl top ext group work img please port
原文地址:https://www.cnblogs.com/webenh/p/11496848.html
文章标题:(六十二)c#Winform自定义控件-警灯(工业)
文章链接:http://soscw.com/index.php/essay/50536.html