UCAlarmLamp.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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: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 UCAlarmLamp.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCAlarmLamp : UserControl
  32. {
  33. /// <summary>
  34. /// The lamp color
  35. /// </summary>
  36. private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) };
  37. /// <summary>
  38. /// Gets or sets the color of the lamp.
  39. /// </summary>
  40. /// <value>The color of the lamp.</value>
  41. [Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]
  42. public Color[] LampColor
  43. {
  44. get { return lampColor; }
  45. set
  46. {
  47. if (value == null || value.Length <= 0)
  48. return;
  49. lampColor = value;
  50. Refresh();
  51. }
  52. }
  53. /// <summary>
  54. /// The lampstand
  55. /// </summary>
  56. private Color lampstand = Color.FromArgb(105, 105, 105);
  57. /// <summary>
  58. /// Gets or sets the lampstand.
  59. /// </summary>
  60. /// <value>The lampstand.</value>
  61. [Description("灯座颜色"), Category("自定义")]
  62. public Color Lampstand
  63. {
  64. get { return lampstand; }
  65. set { lampstand = value; }
  66. }
  67. /// <summary>
  68. /// The twinkle speed
  69. /// </summary>
  70. private int twinkleSpeed = 0;
  71. /// <summary>
  72. /// Gets or sets the twinkle speed.
  73. /// </summary>
  74. /// <value>The twinkle speed.</value>
  75. [Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]
  76. public int TwinkleSpeed
  77. {
  78. get { return twinkleSpeed; }
  79. set
  80. {
  81. if (value < 0)
  82. return;
  83. twinkleSpeed = value;
  84. if (value == 0 || lampColor.Length <= 1)
  85. {
  86. timer.Enabled = false;
  87. }
  88. else
  89. {
  90. intColorIndex = 0;
  91. timer.Interval = value;
  92. timer.Enabled = true;
  93. }
  94. Refresh();
  95. }
  96. }
  97. /// <summary>
  98. /// The timer
  99. /// </summary>
  100. Timer timer;
  101. /// <summary>
  102. /// The int color index
  103. /// </summary>
  104. int intColorIndex = 0;
  105. /// <summary>
  106. /// The m rect working
  107. /// </summary>
  108. Rectangle m_rectWorking;
  109. /// <summary>
  110. /// Initializes a new instance of the <see cref="UCAlarmLamp" /> class.
  111. /// </summary>
  112. public UCAlarmLamp()
  113. {
  114. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  115. this.SetStyle(ControlStyles.DoubleBuffer, true);
  116. this.SetStyle(ControlStyles.ResizeRedraw, true);
  117. this.SetStyle(ControlStyles.Selectable, true);
  118. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  119. this.SetStyle(ControlStyles.UserPaint, true);
  120. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  121. this.SizeChanged += UCAlarmLamp_SizeChanged;
  122. this.Size = new Size(50, 50);
  123. timer = new Timer();
  124. timer.Interval = 200;
  125. timer.Tick += timer_Tick;
  126. }
  127. /// <summary>
  128. /// Handles the SizeChanged event of the UCAlarmLamp control.
  129. /// </summary>
  130. /// <param name="sender">The source of the event.</param>
  131. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  132. void UCAlarmLamp_SizeChanged(object sender, EventArgs e)
  133. {
  134. m_rectWorking = new Rectangle(10, 0, this.Width - 20, this.Height);
  135. }
  136. /// <summary>
  137. /// Handles the Tick event of the timer control.
  138. /// </summary>
  139. /// <param name="sender">The source of the event.</param>
  140. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  141. void timer_Tick(object sender, EventArgs e)
  142. {
  143. intColorIndex++;
  144. if (intColorIndex >= lampColor.Length)
  145. intColorIndex = 0;
  146. Refresh();
  147. }
  148. /// <summary>
  149. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  150. /// </summary>
  151. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  152. protected override void OnPaint(PaintEventArgs e)
  153. {
  154. base.OnPaint(e);
  155. var g = e.Graphics;
  156. g.SetGDIHigh();
  157. Color c1 = lampColor[intColorIndex];
  158. GraphicsPath path = new GraphicsPath();
  159. path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom), new Point(m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width));
  160. path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);
  161. path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width), new Point(m_rectWorking.Right, m_rectWorking.Bottom));
  162. path.CloseAllFigures();
  163. g.FillPath(new SolidBrush(c1), path);
  164. g.FillRectangle(new SolidBrush(lampstand), new Rectangle(5, m_rectWorking.Bottom - 19, this.Width - 10, 10));
  165. g.FillRectangle(new SolidBrush(lampstand), new Rectangle(0, m_rectWorking.Bottom - 10, this.Width, 10));
  166. }
  167. }
  168. }