UCWave.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-22-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCWave.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.ComponentModel;
  19. using System.Drawing;
  20. using System.Drawing.Drawing2D;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. namespace HZH_Controls.Controls
  25. {
  26. /// <summary>
  27. /// Class UCWave.
  28. /// Implements the <see cref="System.Windows.Forms.Control" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.Control" />
  31. public class UCWave : Control
  32. {
  33. /// <summary>
  34. /// Occurs when [on painted].
  35. /// </summary>
  36. public event PaintEventHandler OnPainted;
  37. /// <summary>
  38. /// The m wave color
  39. /// </summary>
  40. private Color m_waveColor = Color.FromArgb(255, 77, 59);
  41. /// <summary>
  42. /// Gets or sets the color of the wave.
  43. /// </summary>
  44. /// <value>The color of the wave.</value>
  45. [Description("波纹颜色"), Category("自定义")]
  46. public Color WaveColor
  47. {
  48. get { return m_waveColor; }
  49. set { m_waveColor = value; }
  50. }
  51. /// <summary>
  52. /// The m wave width
  53. /// </summary>
  54. private int m_waveWidth = 200;
  55. /// <summary>
  56. /// 为方便计算,强制使用10的倍数
  57. /// </summary>
  58. /// <value>The width of the wave.</value>
  59. [Description("波纹宽度(为方便计算,强制使用10的倍数)"), Category("自定义")]
  60. public int WaveWidth
  61. {
  62. get { return m_waveWidth; }
  63. set
  64. {
  65. m_waveWidth = value;
  66. m_waveWidth = m_waveWidth / 10 * 10;
  67. intLeftX = value * -1;
  68. }
  69. }
  70. /// <summary>
  71. /// The m wave height
  72. /// </summary>
  73. private int m_waveHeight = 30;
  74. /// <summary>
  75. /// 波高
  76. /// </summary>
  77. /// <value>The height of the wave.</value>
  78. [Description("波高"), Category("自定义")]
  79. public int WaveHeight
  80. {
  81. get { return m_waveHeight; }
  82. set { m_waveHeight = value; }
  83. }
  84. /// <summary>
  85. /// The m wave sleep
  86. /// </summary>
  87. private int m_waveSleep = 50;
  88. /// <summary>
  89. /// 波运行速度(运行时间间隔,毫秒)
  90. /// </summary>
  91. /// <value>The wave sleep.</value>
  92. [Description("波运行速度(运行时间间隔,毫秒)"), Category("自定义")]
  93. public int WaveSleep
  94. {
  95. get { return m_waveSleep; }
  96. set
  97. {
  98. if (value <= 0)
  99. return;
  100. m_waveSleep = value;
  101. if (timer != null)
  102. {
  103. timer.Enabled = false;
  104. timer.Interval = value;
  105. timer.Enabled = true;
  106. }
  107. }
  108. }
  109. /// <summary>
  110. /// The timer
  111. /// </summary>
  112. Timer timer = new Timer();
  113. /// <summary>
  114. /// The int left x
  115. /// </summary>
  116. int intLeftX = -200;
  117. /// <summary>
  118. /// Initializes a new instance of the <see cref="UCWave" /> class.
  119. /// </summary>
  120. public UCWave()
  121. {
  122. this.Size = new Size(600, 100);
  123. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  124. this.SetStyle(ControlStyles.DoubleBuffer, true);
  125. this.SetStyle(ControlStyles.ResizeRedraw, true);
  126. this.SetStyle(ControlStyles.Selectable, true);
  127. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  128. this.SetStyle(ControlStyles.UserPaint, true);
  129. timer.Interval = m_waveSleep;
  130. timer.Tick += timer_Tick;
  131. this.VisibleChanged += UCWave_VisibleChanged;
  132. }
  133. /// <summary>
  134. /// Handles the VisibleChanged event of the UCWave control.
  135. /// </summary>
  136. /// <param name="sender">The source of the event.</param>
  137. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  138. void UCWave_VisibleChanged(object sender, EventArgs e)
  139. {
  140. timer.Enabled = this.Visible;
  141. }
  142. /// <summary>
  143. /// Handles the Tick event of the timer control.
  144. /// </summary>
  145. /// <param name="sender">The source of the event.</param>
  146. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  147. void timer_Tick(object sender, EventArgs e)
  148. {
  149. intLeftX -= 10;
  150. if (intLeftX == m_waveWidth * -2)
  151. intLeftX = m_waveWidth * -1;
  152. this.Refresh();
  153. }
  154. /// <summary>
  155. /// Handles the <see cref="E:Paint" /> event.
  156. /// </summary>
  157. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  158. protected override void OnPaint(PaintEventArgs e)
  159. {
  160. base.OnPaint(e);
  161. var g = e.Graphics;
  162. g.SetGDIHigh();
  163. List<Point> lst1 = new List<Point>();
  164. List<Point> lst2 = new List<Point>();
  165. int m_intX = intLeftX;
  166. while (true)
  167. {
  168. lst1.Add(new Point(m_intX, 1));
  169. lst1.Add(new Point(m_intX + m_waveWidth / 2, m_waveHeight));
  170. lst2.Add(new Point(m_intX + m_waveWidth / 2, 1));
  171. lst2.Add(new Point(m_intX + m_waveWidth / 2 + m_waveWidth / 2, m_waveHeight));
  172. m_intX += m_waveWidth;
  173. if (m_intX > this.Width + m_waveWidth)
  174. break;
  175. }
  176. GraphicsPath path1 = new GraphicsPath();
  177. path1.AddCurve(lst1.ToArray(), 0.5F);
  178. path1.AddLine(this.Width + 1, -1, this.Width + 1, this.Height);
  179. path1.AddLine(this.Width + 1, this.Height, -1, this.Height);
  180. path1.AddLine(-1, this.Height, -1, -1);
  181. GraphicsPath path2 = new GraphicsPath();
  182. path2.AddCurve(lst2.ToArray(), 0.5F);
  183. path2.AddLine(this.Width + 1, -1, this.Width + 1, this.Height);
  184. path2.AddLine(this.Width + 1, this.Height, -1, this.Height);
  185. path2.AddLine(-1, this.Height, -1, -1);
  186. g.FillPath(new SolidBrush(Color.FromArgb(220, m_waveColor.R, m_waveColor.G, m_waveColor.B)), path1);
  187. g.FillPath(new SolidBrush(Color.FromArgb(220, m_waveColor.R, m_waveColor.G, m_waveColor.B)), path2);
  188. if (OnPainted != null)
  189. {
  190. OnPainted(this, e);
  191. }
  192. }
  193. }
  194. }