UCProcessWave.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-23-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCProcessWave.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.Data;
  21. using System.Linq;
  22. using System.Text;
  23. using System.Windows.Forms;
  24. using System.Drawing.Drawing2D;
  25. namespace HZH_Controls.Controls
  26. {
  27. /// <summary>
  28. /// Class UCProcessWave.
  29. /// Implements the <see cref="HZH_Controls.Controls.UCControlBase" />
  30. /// </summary>
  31. /// <seealso cref="HZH_Controls.Controls.UCControlBase" />
  32. public partial class UCProcessWave : UCControlBase
  33. {
  34. /// <summary>
  35. /// The m is rectangle
  36. /// </summary>
  37. private bool m_isRectangle = false;
  38. /// <summary>
  39. /// Gets or sets a value indicating whether this instance is rectangle.
  40. /// </summary>
  41. /// <value><c>true</c> if this instance is rectangle; otherwise, <c>false</c>.</value>
  42. [Description("是否矩形"), Category("自定义")]
  43. public bool IsRectangle
  44. {
  45. get { return m_isRectangle; }
  46. set
  47. {
  48. m_isRectangle = value;
  49. if (value)
  50. {
  51. base.ConerRadius = 10;
  52. }
  53. else
  54. {
  55. base.ConerRadius = Math.Min(this.Width, this.Height);
  56. }
  57. }
  58. }
  59. #region 不再使用的父类属性 English:Parent class attributes that are no longer used
  60. /// <summary>
  61. /// 圆角角度
  62. /// </summary>
  63. /// <value>The coner radius.</value>
  64. [Browsable(false)]
  65. public new int ConerRadius
  66. {
  67. get;
  68. set;
  69. }
  70. /// <summary>
  71. /// 是否圆角
  72. /// </summary>
  73. /// <value><c>true</c> if this instance is radius; otherwise, <c>false</c>.</value>
  74. [Browsable(false)]
  75. public new bool IsRadius
  76. {
  77. get;
  78. set;
  79. }
  80. /// <summary>
  81. /// 当使用边框时填充颜色,当值为背景色或透明色或空值则不填充
  82. /// </summary>
  83. /// <value>The color of the fill.</value>
  84. [Browsable(false)]
  85. public new Color FillColor
  86. {
  87. get;
  88. set;
  89. }
  90. #endregion
  91. /// <summary>
  92. /// Occurs when [value changed].
  93. /// </summary>
  94. [Description("值变更事件"), Category("自定义")]
  95. public event EventHandler ValueChanged;
  96. /// <summary>
  97. /// The m value
  98. /// </summary>
  99. int m_value = 0;
  100. /// <summary>
  101. /// Gets or sets the value.
  102. /// </summary>
  103. /// <value>The value.</value>
  104. [Description("当前属性"), Category("自定义")]
  105. public int Value
  106. {
  107. set
  108. {
  109. if (value > m_maxValue)
  110. m_value = m_maxValue;
  111. else if (value < 0)
  112. m_value = 0;
  113. else
  114. m_value = value;
  115. if (ValueChanged != null)
  116. ValueChanged(this, null);
  117. ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
  118. Refresh();
  119. }
  120. get
  121. {
  122. return m_value;
  123. }
  124. }
  125. /// <summary>
  126. /// The m maximum value
  127. /// </summary>
  128. private int m_maxValue = 100;
  129. /// <summary>
  130. /// Gets or sets the maximum value.
  131. /// </summary>
  132. /// <value>The maximum value.</value>
  133. [Description("最大值"), Category("自定义")]
  134. public int MaxValue
  135. {
  136. get { return m_maxValue; }
  137. set
  138. {
  139. if (value < m_value)
  140. m_maxValue = m_value;
  141. else
  142. m_maxValue = value;
  143. Refresh();
  144. }
  145. }
  146. /// <summary>
  147. /// 获取或设置控件显示的文字的字体。
  148. /// </summary>
  149. /// <value>The font.</value>
  150. /// <PermissionSet>
  151. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  152. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  153. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  154. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  155. /// </PermissionSet>
  156. public override Font Font
  157. {
  158. get
  159. {
  160. return base.Font;
  161. }
  162. set
  163. {
  164. base.Font = value;
  165. }
  166. }
  167. /// <summary>
  168. /// 获取或设置控件的前景色。
  169. /// </summary>
  170. /// <value>The color of the fore.</value>
  171. /// <PermissionSet>
  172. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  173. /// </PermissionSet>
  174. public override Color ForeColor
  175. {
  176. get
  177. {
  178. return base.ForeColor;
  179. }
  180. set
  181. {
  182. base.ForeColor = value;
  183. }
  184. }
  185. /// <summary>
  186. /// Gets or sets the color of the value.
  187. /// </summary>
  188. /// <value>The color of the value.</value>
  189. [Description("值颜色"), Category("自定义")]
  190. public Color ValueColor
  191. {
  192. get { return this.ucWave1.WaveColor; }
  193. set
  194. {
  195. this.ucWave1.WaveColor = value;
  196. }
  197. }
  198. /// <summary>
  199. /// 边框宽度
  200. /// </summary>
  201. /// <value>The width of the rect.</value>
  202. [Description("边框宽度"), Category("自定义")]
  203. public override int RectWidth
  204. {
  205. get
  206. {
  207. return base.RectWidth;
  208. }
  209. set
  210. {
  211. if (value < 4)
  212. base.RectWidth = 4;
  213. else
  214. base.RectWidth = value;
  215. }
  216. }
  217. /// <summary>
  218. /// Initializes a new instance of the <see cref="UCProcessWave" /> class.
  219. /// </summary>
  220. public UCProcessWave()
  221. {
  222. InitializeComponent();
  223. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  224. this.SetStyle(ControlStyles.DoubleBuffer, true);
  225. this.SetStyle(ControlStyles.ResizeRedraw, true);
  226. this.SetStyle(ControlStyles.Selectable, true);
  227. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  228. this.SetStyle(ControlStyles.UserPaint, true);
  229. base.IsRadius = true;
  230. base.IsShowRect = false;
  231. RectWidth = 4;
  232. RectColor = Color.White;
  233. ForeColor = Color.White;
  234. ucWave1.Height = (int)((double)m_value / (double)m_maxValue * this.Height) + ucWave1.WaveHeight;
  235. this.SizeChanged += UCProcessWave_SizeChanged;
  236. this.ucWave1.OnPainted += ucWave1_Painted;
  237. base.ConerRadius = Math.Min(this.Width, this.Height);
  238. }
  239. /// <summary>
  240. /// Handles the Painted event of the ucWave1 control.
  241. /// </summary>
  242. /// <param name="sender">The source of the event.</param>
  243. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  244. void ucWave1_Painted(object sender, PaintEventArgs e)
  245. {
  246. e.Graphics.SetGDIHigh();
  247. if (IsShowRect)
  248. {
  249. if (m_isRectangle)
  250. {
  251. Color rectColor = RectColor;
  252. Pen pen = new Pen(rectColor, (float)RectWidth);
  253. Rectangle clientRectangle = new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height);
  254. GraphicsPath graphicsPath = new GraphicsPath();
  255. graphicsPath.AddArc(clientRectangle.X, clientRectangle.Y, 10, 10, 180f, 90f);
  256. graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Y, 10, 10, 270f, 90f);
  257. graphicsPath.AddArc(clientRectangle.Width - 10 - 1, clientRectangle.Bottom - 10 - 1, 10, 10, 0f, 90f);
  258. graphicsPath.AddArc(clientRectangle.X, clientRectangle.Bottom - 10 - 1, 10, 10, 90f, 90f);
  259. graphicsPath.CloseFigure();
  260. e.Graphics.DrawPath(pen, graphicsPath);
  261. }
  262. else
  263. {
  264. SolidBrush solidBrush = new SolidBrush(RectColor);
  265. e.Graphics.DrawEllipse(new Pen(solidBrush, RectWidth), new Rectangle(0, this.ucWave1.Height - this.Height, this.Width, this.Height));
  266. }
  267. }
  268. if (!m_isRectangle)
  269. {
  270. //这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
  271. SolidBrush solidBrush1 = new SolidBrush(RectColor);
  272. e.Graphics.DrawEllipse(new Pen(solidBrush1, 2), new Rectangle(-1, this.ucWave1.Height - this.Height - 1, this.Width + 2, this.Height + 2));
  273. }
  274. string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
  275. System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
  276. e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.ucWave1.Height - this.Height) + (this.Height - sizeF.Height) / 2));
  277. }
  278. /// <summary>
  279. /// Handles the SizeChanged event of the UCProcessWave control.
  280. /// </summary>
  281. /// <param name="sender">The source of the event.</param>
  282. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  283. void UCProcessWave_SizeChanged(object sender, EventArgs e)
  284. {
  285. if (!m_isRectangle)
  286. {
  287. base.ConerRadius = Math.Min(this.Width, this.Height);
  288. if (this.Width != this.Height)
  289. {
  290. this.Size = new Size(Math.Min(this.Width, this.Height), Math.Min(this.Width, this.Height));
  291. }
  292. }
  293. }
  294. /// <summary>
  295. /// Handles the <see cref="E:Paint" /> event.
  296. /// </summary>
  297. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  298. protected override void OnPaint(PaintEventArgs e)
  299. {
  300. base.OnPaint(e);
  301. e.Graphics.SetGDIHigh();
  302. if (!m_isRectangle)
  303. {
  304. //这里曲线救国,因为设置了控件区域导致的毛边,通过画一个没有毛边的圆遮挡
  305. SolidBrush solidBrush = new SolidBrush(RectColor);
  306. e.Graphics.DrawEllipse(new Pen(solidBrush, 2), new Rectangle(-1, -1, this.Width + 2, this.Height + 2));
  307. }
  308. string strValue = ((double)m_value / (double)m_maxValue).ToString("0.%");
  309. System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
  310. e.Graphics.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.Height - sizeF.Height) / 2 + 1));
  311. }
  312. }
  313. }