UCSignalLamp.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-09
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCSignalLamp.cs">
  7. // Copyright by Huang Zhenghui(黄正辉) All, QQ group:568015492 QQ:623128629 Email:623128629@qq.com
  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. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCSignalLamp.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCSignalLamp : UserControl
  32. {
  33. /// <summary>
  34. /// The is show border
  35. /// </summary>
  36. private bool isShowBorder = false;
  37. /// <summary>
  38. /// Gets or sets a value indicating whether this instance is show border.
  39. /// </summary>
  40. /// <value><c>true</c> if this instance is show border; otherwise, <c>false</c>.</value>
  41. [Description("是否显示边框"), Category("自定义")]
  42. public bool IsShowBorder
  43. {
  44. get { return isShowBorder; }
  45. set
  46. {
  47. isShowBorder = value;
  48. Refresh();
  49. }
  50. }
  51. /// <summary>
  52. /// The lamp color
  53. /// </summary>
  54. private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
  55. /// <summary>
  56. /// Gets or sets the color of the lamp.
  57. /// </summary>
  58. /// <value>The color of the lamp.</value>
  59. [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
  60. public Color[] LampColor
  61. {
  62. get { return lampColor; }
  63. set
  64. {
  65. if (value == null || value.Length <= 0)
  66. return;
  67. lampColor = value;
  68. Refresh();
  69. }
  70. }
  71. /// <summary>
  72. /// The is highlight
  73. /// </summary>
  74. private bool isHighlight = true;
  75. /// <summary>
  76. /// Gets or sets a value indicating whether this instance is highlight.
  77. /// </summary>
  78. /// <value><c>true</c> if this instance is highlight; otherwise, <c>false</c>.</value>
  79. [Description("是否高亮显示"), Category("自定义")]
  80. public bool IsHighlight
  81. {
  82. get { return isHighlight; }
  83. set
  84. {
  85. isHighlight = value;
  86. Refresh();
  87. }
  88. }
  89. /// <summary>
  90. /// The twinkle speed
  91. /// </summary>
  92. private int twinkleSpeed = 0;
  93. /// <summary>
  94. /// Gets or sets the twinkle speed.
  95. /// </summary>
  96. /// <value>The twinkle speed.</value>
  97. [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
  98. public int TwinkleSpeed
  99. {
  100. get { return twinkleSpeed; }
  101. set
  102. {
  103. if (value < 0)
  104. return;
  105. twinkleSpeed = value;
  106. if (value == 0 || lampColor.Length <= 1)
  107. {
  108. timer.Enabled = false;
  109. }
  110. else
  111. {
  112. intColorIndex = 0;
  113. timer.Interval = value;
  114. timer.Enabled = true;
  115. }
  116. Refresh();
  117. }
  118. }
  119. /// <summary>
  120. /// The timer
  121. /// </summary>
  122. Timer timer;
  123. /// <summary>
  124. /// The int color index
  125. /// </summary>
  126. int intColorIndex = 0;
  127. /// <summary>
  128. /// Initializes a new instance of the <see cref="UCSignalLamp" /> class.
  129. /// </summary>
  130. public UCSignalLamp()
  131. {
  132. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  133. this.SetStyle(ControlStyles.DoubleBuffer, true);
  134. this.SetStyle(ControlStyles.ResizeRedraw, true);
  135. this.SetStyle(ControlStyles.Selectable, true);
  136. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  137. this.SetStyle(ControlStyles.UserPaint, true);
  138. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  139. this.Size = new Size(50, 50);
  140. this.SizeChanged += UCSignalLamp_SizeChanged;
  141. timer = new Timer();
  142. timer.Interval = 200;
  143. timer.Tick += timer_Tick;
  144. }
  145. /// <summary>
  146. /// Handles the Tick event of the timer control.
  147. /// </summary>
  148. /// <param name="sender">The source of the event.</param>
  149. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  150. void timer_Tick(object sender, EventArgs e)
  151. {
  152. intColorIndex++;
  153. if (intColorIndex >= lampColor.Length)
  154. intColorIndex = 0;
  155. Refresh();
  156. }
  157. /// <summary>
  158. /// Handles the SizeChanged event of the UCSignalLamp control.
  159. /// </summary>
  160. /// <param name="sender">The source of the event.</param>
  161. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  162. void UCSignalLamp_SizeChanged(object sender, EventArgs e)
  163. {
  164. var maxSize = Math.Min(this.Width, this.Height);
  165. if (this.Width != maxSize)
  166. this.Width = maxSize;
  167. if (this.Height != maxSize)
  168. this.Height = maxSize;
  169. }
  170. /// <summary>
  171. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  172. /// </summary>
  173. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  174. protected override void OnPaint(PaintEventArgs e)
  175. {
  176. base.OnPaint(e);
  177. var g = e.Graphics;
  178. g.SetGDIHigh();
  179. Color c1 = lampColor[intColorIndex];
  180. g.FillEllipse(new SolidBrush(c1), new Rectangle(this.ClientRectangle.Location, this.ClientRectangle.Size - new Size(1, 1)));
  181. if (isHighlight)
  182. {
  183. GraphicsPath gp = new GraphicsPath();
  184. Rectangle rec = new Rectangle(5, 5, this.Width - 10 - 1, this.Height - 10 - 1);
  185. gp.AddEllipse(rec);
  186. Color[] surroundColor = new Color[] { c1 };
  187. PathGradientBrush pb = new PathGradientBrush(gp);
  188. pb.CenterColor = Color.White;
  189. pb.SurroundColors = surroundColor;
  190. g.FillPath(pb, gp);
  191. }
  192. if (isShowBorder)
  193. {
  194. g.DrawEllipse(new Pen(new SolidBrush(this.BackColor), 2), new Rectangle(4, 4, this.Width - 1 - 8, this.Height - 1 - 8));
  195. }
  196. }
  197. }
  198. }