UCConveyor.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-05
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCConveyor.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 UCConveyor.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCConveyor : UserControl
  32. {
  33. /// <summary>
  34. /// The conveyor color
  35. /// </summary>
  36. private Color conveyorColor = Color.FromArgb(255, 77, 59);
  37. /// <summary>
  38. /// Gets or sets the color of the conveyor.
  39. /// </summary>
  40. /// <value>The color of the conveyor.</value>
  41. [Description("传送带颜色"), Category("自定义")]
  42. public Color ConveyorColor
  43. {
  44. get { return conveyorColor; }
  45. set
  46. {
  47. conveyorColor = value;
  48. Refresh();
  49. }
  50. }
  51. /// <summary>
  52. /// The inclination
  53. /// </summary>
  54. private double inclination = 0;
  55. /// <summary>
  56. /// Gets or sets the inclination.
  57. /// </summary>
  58. /// <value>The inclination.</value>
  59. [Description("传送带角度(-90<=value<=90)"), Category("自定义")]
  60. public double Inclination
  61. {
  62. get { return inclination; }
  63. set
  64. {
  65. if (value > 90 || value < -90)
  66. return;
  67. inclination = value;
  68. ResetWorkingRect();
  69. Refresh();
  70. }
  71. }
  72. /// <summary>
  73. /// The conveyor height
  74. /// </summary>
  75. private int conveyorHeight = 50;
  76. /// <summary>
  77. /// Gets or sets the height of the conveyor.
  78. /// </summary>
  79. /// <value>The height of the conveyor.</value>
  80. [Description("传送带高度"), Category("自定义")]
  81. public int ConveyorHeight
  82. {
  83. get { return conveyorHeight; }
  84. set
  85. {
  86. conveyorHeight = value;
  87. ResetWorkingRect();
  88. Refresh();
  89. }
  90. }
  91. /// <summary>
  92. /// The conveyor direction
  93. /// </summary>
  94. private ConveyorDirection conveyorDirection = ConveyorDirection.Forward;
  95. /// <summary>
  96. /// Gets or sets the conveyor direction.
  97. /// </summary>
  98. /// <value>The conveyor direction.</value>
  99. [Description("传送带运行方向"), Category("自定义")]
  100. public ConveyorDirection ConveyorDirection
  101. {
  102. get { return conveyorDirection; }
  103. set
  104. {
  105. conveyorDirection = value;
  106. if (value == HZH_Controls.Controls.ConveyorDirection.None)
  107. {
  108. m_timer.Enabled = false;
  109. Refresh();
  110. }
  111. else
  112. {
  113. m_timer.Enabled = true;
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// The liquid speed
  119. /// </summary>
  120. private int conveyorSpeed = 100;
  121. /// <summary>
  122. /// 传送带运行速度,越小,速度越快Gets or sets the ConveyorSpeed.
  123. /// </summary>
  124. /// <value>The liquid speed.</value>
  125. [Description("传送带运行速度,越小,速度越快"), Category("自定义")]
  126. public int ConveyorSpeed
  127. {
  128. get { return conveyorSpeed; }
  129. set
  130. {
  131. if (value <= 0)
  132. return;
  133. conveyorSpeed = value;
  134. m_timer.Interval = value;
  135. }
  136. }
  137. /// <summary>
  138. /// The m working rect
  139. /// </summary>
  140. Rectangle m_workingRect;
  141. /// <summary>
  142. /// The int line left
  143. /// </summary>
  144. int intLineLeft = 0;
  145. /// <summary>
  146. /// The m timer
  147. /// </summary>
  148. Timer m_timer;
  149. /// <summary>
  150. /// Initializes a new instance of the <see cref="UCConveyor" /> class.
  151. /// </summary>
  152. public UCConveyor()
  153. {
  154. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  155. this.SetStyle(ControlStyles.DoubleBuffer, true);
  156. this.SetStyle(ControlStyles.ResizeRedraw, true);
  157. this.SetStyle(ControlStyles.Selectable, true);
  158. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  159. this.SetStyle(ControlStyles.UserPaint, true);
  160. this.SizeChanged += UCConveyor_SizeChanged;
  161. this.Size = new Size(300, 50);
  162. m_timer = new Timer();
  163. m_timer.Interval = 100;
  164. m_timer.Tick += timer_Tick;
  165. m_timer.Enabled = true;
  166. }
  167. /// <summary>
  168. /// Handles the Tick event of the timer control.
  169. /// </summary>
  170. /// <param name="sender">The source of the event.</param>
  171. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  172. void timer_Tick(object sender, EventArgs e)
  173. {
  174. intLineLeft += 2;
  175. if (intLineLeft > 12)
  176. intLineLeft = 0;
  177. Refresh();
  178. }
  179. /// <summary>
  180. /// Handles the SizeChanged event of the UCConveyor control.
  181. /// </summary>
  182. /// <param name="sender">The source of the event.</param>
  183. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  184. void UCConveyor_SizeChanged(object sender, EventArgs e)
  185. {
  186. ResetWorkingRect();
  187. }
  188. /// <summary>
  189. /// Resets the working rect.
  190. /// </summary>
  191. private void ResetWorkingRect()
  192. {
  193. if (inclination == 90 || inclination == -90)
  194. {
  195. m_workingRect = new Rectangle((this.Width - conveyorHeight) / 2, 1, conveyorHeight, this.Height - 2);
  196. }
  197. else if (inclination == 0)
  198. {
  199. m_workingRect = new Rectangle(1, (this.Height - conveyorHeight) / 2 + 1, this.Width - 2, conveyorHeight);
  200. }
  201. else
  202. {
  203. //根据角度计算需要的高度
  204. int intHeight = (int)(Math.Tan(Math.PI * (Math.Abs(inclination) / 180.00000)) * (this.Width));
  205. if (intHeight >= this.Height)
  206. intHeight = this.Height;
  207. int intWidth = (int)(intHeight / (Math.Tan(Math.PI * (Math.Abs(inclination) / 180.00000))));
  208. intHeight += conveyorHeight;
  209. if (intHeight >= this.Height)
  210. intHeight = this.Height;
  211. m_workingRect = new Rectangle((this.Width - intWidth) / 2 + 1, (this.Height - intHeight) / 2 + 1, intWidth - 2, intHeight - 2);
  212. }
  213. }
  214. /// <summary>
  215. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  216. /// </summary>
  217. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  218. protected override void OnPaint(PaintEventArgs e)
  219. {
  220. base.OnPaint(e);
  221. var g = e.Graphics;
  222. g.SetGDIHigh();
  223. //g.FillRectangle(new SolidBrush(Color.FromArgb(100, conveyorColor)), m_workingRect);
  224. //轴
  225. //左端
  226. var rectLeft = new Rectangle(m_workingRect.Left + 5, (inclination >= 0 ? (m_workingRect.Bottom - conveyorHeight) : m_workingRect.Top) + 5, conveyorHeight - 10, conveyorHeight - 10);
  227. g.FillEllipse(new SolidBrush(conveyorColor), rectLeft);
  228. g.FillEllipse(new SolidBrush(Color.White), new Rectangle(rectLeft.Left + (rectLeft.Width - 6) / 2, rectLeft.Top + (rectLeft.Height - 6) / 2, 6, 6));
  229. //右端
  230. var rectRight = new Rectangle(m_workingRect.Right - conveyorHeight + 5, (inclination >= 0 ? (m_workingRect.Top) : (m_workingRect.Bottom - conveyorHeight)) + 5, conveyorHeight - 10, conveyorHeight - 10);
  231. g.FillEllipse(new SolidBrush(conveyorColor), rectRight);
  232. g.FillEllipse(new SolidBrush(Color.White), new Rectangle(rectRight.Left + (rectRight.Width - 6) / 2, rectRight.Top + (rectRight.Height - 6) / 2, 6, 6));
  233. //传送带
  234. //左端
  235. GraphicsPath path = new GraphicsPath();
  236. GraphicsPath pathRegion = new GraphicsPath();
  237. path.AddArc(new Rectangle(m_workingRect.Left + 3, (inclination >= 0 ? (m_workingRect.Bottom - conveyorHeight) : m_workingRect.Top) + 3, conveyorHeight - 6, conveyorHeight - 6), 90F - (float)inclination, 180F);
  238. pathRegion.AddArc(new Rectangle(m_workingRect.Left, (inclination >= 0 ? (m_workingRect.Bottom - conveyorHeight) : m_workingRect.Top), conveyorHeight, conveyorHeight), 90F - (float)inclination, 180F);
  239. //右端
  240. path.AddArc(new Rectangle(m_workingRect.Right - conveyorHeight + 3, (inclination >= 0 ? (m_workingRect.Top) : (m_workingRect.Bottom - conveyorHeight)) + 3, conveyorHeight - 6, conveyorHeight - 6), 270 - (float)inclination, 180F);
  241. pathRegion.AddArc(new Rectangle(m_workingRect.Right - conveyorHeight, (inclination >= 0 ? (m_workingRect.Top) : (m_workingRect.Bottom - conveyorHeight)), conveyorHeight, conveyorHeight), 270 - (float)inclination, 180F);
  242. path.CloseAllFigures();
  243. base.Region = new System.Drawing.Region(pathRegion);
  244. g.DrawPath(new Pen(new SolidBrush(conveyorColor), 3), path);
  245. //液体流动
  246. if (ConveyorDirection != ConveyorDirection.None)
  247. {
  248. Pen p = new Pen(new SolidBrush(Color.FromArgb(150, this.BackColor)), 4);
  249. p.DashPattern = new float[] { 6, 6 };
  250. p.DashOffset = intLineLeft * (ConveyorDirection == ConveyorDirection.Forward ? -1 : 1);
  251. g.DrawPath(p, path);
  252. }
  253. }
  254. }
  255. /// <summary>
  256. /// Enum ConveyorDirection
  257. /// </summary>
  258. public enum ConveyorDirection
  259. {
  260. /// <summary>
  261. /// The none
  262. /// </summary>
  263. None,
  264. /// <summary>
  265. /// The forward
  266. /// </summary>
  267. Forward,
  268. /// <summary>
  269. /// The backward
  270. /// </summary>
  271. Backward
  272. }
  273. }