UCBlower.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-09
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCBlower.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 UCBlower.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCBlower : UserControl
  32. {
  33. /// <summary>
  34. /// The entrance direction
  35. /// </summary>
  36. private BlowerEntranceDirection entranceDirection = BlowerEntranceDirection.None;
  37. /// <summary>
  38. /// Gets or sets the entrance direction.
  39. /// </summary>
  40. /// <value>The entrance direction.</value>
  41. [Description("入口方向"), Category("自定义")]
  42. public BlowerEntranceDirection EntranceDirection
  43. {
  44. get { return entranceDirection; }
  45. set
  46. {
  47. entranceDirection = value;
  48. Refresh();
  49. }
  50. }
  51. /// <summary>
  52. /// The exit direction
  53. /// </summary>
  54. private BlowerExitDirection exitDirection = BlowerExitDirection.Right;
  55. /// <summary>
  56. /// Gets or sets the exit direction.
  57. /// </summary>
  58. /// <value>The exit direction.</value>
  59. [Description("出口方向"), Category("自定义")]
  60. public BlowerExitDirection ExitDirection
  61. {
  62. get { return exitDirection; }
  63. set
  64. {
  65. exitDirection = value;
  66. Refresh();
  67. }
  68. }
  69. /// <summary>
  70. /// The blower color
  71. /// </summary>
  72. private Color blowerColor = Color.FromArgb(255, 77, 59);
  73. /// <summary>
  74. /// Gets or sets the color of the blower.
  75. /// </summary>
  76. /// <value>The color of the blower.</value>
  77. [Description("风机颜色"), Category("自定义")]
  78. public Color BlowerColor
  79. {
  80. get { return blowerColor; }
  81. set
  82. {
  83. blowerColor = value;
  84. Refresh();
  85. }
  86. }
  87. /// <summary>
  88. /// The fan color
  89. /// </summary>
  90. private Color fanColor = Color.FromArgb(3, 169, 243);
  91. /// <summary>
  92. /// Gets or sets the color of the fan.
  93. /// </summary>
  94. /// <value>The color of the fan.</value>
  95. [Description("风叶颜色"), Category("自定义")]
  96. public Color FanColor
  97. {
  98. get { return fanColor; }
  99. set
  100. {
  101. fanColor = value;
  102. Refresh();
  103. }
  104. }
  105. /// <summary>
  106. /// The m rect working
  107. /// </summary>
  108. Rectangle m_rectWorking;
  109. /// <summary>
  110. /// Initializes a new instance of the <see cref="UCBlower" /> class.
  111. /// </summary>
  112. public UCBlower()
  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 += UCBlower_SizeChanged;
  122. this.Size = new Size(120, 120);
  123. }
  124. /// <summary>
  125. /// Handles the SizeChanged event of the UCBlower control.
  126. /// </summary>
  127. /// <param name="sender">The source of the event.</param>
  128. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  129. void UCBlower_SizeChanged(object sender, EventArgs e)
  130. {
  131. int intMin = Math.Min(this.Width, this.Height);
  132. m_rectWorking = new Rectangle((this.Width - (intMin / 3 * 2)) / 2, (this.Height - (intMin / 3 * 2)) / 2, (intMin / 3 * 2), (intMin / 3 * 2));
  133. }
  134. /// <summary>
  135. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  136. /// </summary>
  137. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  138. protected override void OnPaint(PaintEventArgs e)
  139. {
  140. base.OnPaint(e);
  141. var g = e.Graphics;
  142. g.SetGDIHigh();
  143. GraphicsPath pathLineIn = new GraphicsPath();
  144. GraphicsPath pathLineOut = new GraphicsPath();
  145. int intLinePenWidth = 0;
  146. switch (exitDirection)
  147. {
  148. case BlowerExitDirection.Left:
  149. g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
  150. intLinePenWidth = m_rectWorking.Height / 2 - 5;
  151. pathLineOut.AddLine(new Point(-10, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2));
  152. g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(1, m_rectWorking.Top - 2), new Point(1, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) + 2));
  153. break;
  154. case BlowerExitDirection.Right:
  155. g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Top, this.Width / 2, m_rectWorking.Height / 2 - 5));
  156. intLinePenWidth = m_rectWorking.Height / 2 - 5;
  157. pathLineOut.AddLine(new Point(this.Width + 10, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) / 2));
  158. g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(this.Width - 2, m_rectWorking.Top - 2), new Point(this.Width - 2, m_rectWorking.Top + (m_rectWorking.Height / 2 - 5) + 2));
  159. break;
  160. case BlowerExitDirection.Up:
  161. g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5), 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
  162. intLinePenWidth = m_rectWorking.Width / 2 - 5;
  163. pathLineOut.AddLine(new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) / 2, -10), new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) / 2, m_rectWorking.Top + m_rectWorking.Height / 2));
  164. g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Right + 2, 1), new Point(m_rectWorking.Right - (m_rectWorking.Width / 2 - 5) - 2, 1));
  165. break;
  166. }
  167. switch (entranceDirection)
  168. {
  169. case BlowerEntranceDirection.Left:
  170. g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(0, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
  171. pathLineIn.AddLine(new Point(-10, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2));
  172. g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(1, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 - 2), new Point(1, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) + 2));
  173. break;
  174. case BlowerEntranceDirection.Right:
  175. g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(this.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5, this.Width / 2, m_rectWorking.Height / 2 - 5));
  176. pathLineIn.AddLine(new Point(this.Width + 10, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2), new Point(m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) / 2));
  177. g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(this.Width - 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 - 2), new Point(this.Width - 2, m_rectWorking.Bottom - m_rectWorking.Height / 2 + 5 + (m_rectWorking.Height / 2 - 5) + 2));
  178. break;
  179. case BlowerEntranceDirection.Up:
  180. g.FillRectangle(new SolidBrush(blowerColor), new Rectangle(m_rectWorking.Left, 0, m_rectWorking.Width / 2 - 5, this.Height / 2));
  181. pathLineIn.AddLine(new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) / 2, -10), new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) / 2, m_rectWorking.Top + m_rectWorking.Height / 2));
  182. g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Left - 2, 1), new Point(m_rectWorking.Left + (m_rectWorking.Width / 2 - 5) + 2, 1));
  183. break;
  184. }
  185. //渐变色
  186. int _intPenWidth = intLinePenWidth;
  187. int intCount = _intPenWidth / 2 / 4;
  188. for (int i = 0; i < intCount; i++)
  189. {
  190. int _penWidth = _intPenWidth / 2 - 4 * i;
  191. if (_penWidth <= 0)
  192. _penWidth = 1;
  193. if (entranceDirection != BlowerEntranceDirection.None)
  194. g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(40, Color.White.R, Color.White.G, Color.White.B)), _penWidth), pathLineIn);
  195. g.DrawPath(new Pen(new SolidBrush(Color.FromArgb(40, Color.White.R, Color.White.G, Color.White.B)), _penWidth), pathLineOut);
  196. if (_penWidth == 1)
  197. break;
  198. }
  199. //底座
  200. GraphicsPath gpDZ = new GraphicsPath();
  201. gpDZ.AddLines(new Point[]
  202. {
  203. new Point( m_rectWorking.Left+m_rectWorking.Width/2,m_rectWorking.Top+m_rectWorking.Height/2),
  204. new Point(m_rectWorking.Left+2,this.Height),
  205. new Point(m_rectWorking.Right-2,this.Height)
  206. });
  207. gpDZ.CloseAllFigures();
  208. g.FillPath(new SolidBrush(blowerColor), gpDZ);
  209. g.FillPath(new SolidBrush(Color.FromArgb(50, Color.White)), gpDZ);
  210. g.DrawLine(new Pen(new SolidBrush(blowerColor), 3), new Point(m_rectWorking.Left, this.Height - 2), new Point(m_rectWorking.Right, this.Height - 2));
  211. //中心
  212. g.FillEllipse(new SolidBrush(blowerColor), m_rectWorking);
  213. g.FillEllipse(new SolidBrush(Color.FromArgb(20, Color.White)), m_rectWorking);
  214. //扇叶
  215. Rectangle _rect = new Rectangle(m_rectWorking.Left + (m_rectWorking.Width - (m_rectWorking.Width / 3 * 2)) / 2, m_rectWorking.Top + (m_rectWorking.Height - (m_rectWorking.Width / 3 * 2)) / 2, (m_rectWorking.Width / 3 * 2), (m_rectWorking.Width / 3 * 2));
  216. int _splitCount = 8;
  217. float fltSplitValue = 360F / (float)_splitCount;
  218. for (int i = 0; i <= _splitCount; i++)
  219. {
  220. float fltAngle = (fltSplitValue * i - 180) % 360;
  221. float fltY1 = (float)(_rect.Top + _rect.Width / 2 - ((_rect.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
  222. float fltX1 = (float)(_rect.Left + (_rect.Width / 2 - ((_rect.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
  223. float fltY2 = 0;
  224. float fltX2 = 0;
  225. fltY2 = (float)(_rect.Top + _rect.Width / 2 - ((_rect.Width / 4) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
  226. fltX2 = (float)(_rect.Left + (_rect.Width / 2 - ((_rect.Width / 4) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
  227. g.DrawLine(new Pen(new SolidBrush(fanColor), 2), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
  228. }
  229. g.FillEllipse(new SolidBrush(fanColor), new Rectangle(_rect.Left + _rect.Width / 2 - _rect.Width / 4 + 2, _rect.Top + _rect.Width / 2 - _rect.Width / 4 + 2, _rect.Width / 2 - 4, _rect.Width / 2 - 4));
  230. g.FillEllipse(new SolidBrush(Color.FromArgb(50, Color.White)), new Rectangle(_rect.Left - 5, _rect.Top - 5, _rect.Width + 10, _rect.Height + 10));
  231. }
  232. }
  233. /// <summary>
  234. /// Enum BlowerEntranceDirection
  235. /// </summary>
  236. public enum BlowerEntranceDirection
  237. {
  238. /// <summary>
  239. /// The none
  240. /// </summary>
  241. None,
  242. /// <summary>
  243. /// The left
  244. /// </summary>
  245. Left,
  246. /// <summary>
  247. /// The right
  248. /// </summary>
  249. Right,
  250. /// <summary>
  251. /// Up
  252. /// </summary>
  253. Up
  254. }
  255. /// <summary>
  256. /// Enum BlowerExitDirection
  257. /// </summary>
  258. public enum BlowerExitDirection
  259. {
  260. /// <summary>
  261. /// The left
  262. /// </summary>
  263. Left,
  264. /// <summary>
  265. /// The right
  266. /// </summary>
  267. Right,
  268. /// <summary>
  269. /// Up
  270. /// </summary>
  271. Up
  272. }
  273. }