UCProcessLine.cs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-20-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCProcessLine.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 UCProcessLine.
  29. /// Implements the <see cref="System.Windows.Forms.Control" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.Control" />
  32. [DefaultEvent("ValueChanged")]
  33. public class UCProcessLine : Control
  34. {
  35. /// <summary>
  36. /// Occurs when [value changed].
  37. /// </summary>
  38. [Description("值变更事件"), Category("自定义")]
  39. public event EventHandler ValueChanged;
  40. /// <summary>
  41. /// The m value
  42. /// </summary>
  43. int m_value = 0;
  44. /// <summary>
  45. /// Gets or sets the value.
  46. /// </summary>
  47. /// <value>The value.</value>
  48. [Description("当前属性"), Category("自定义")]
  49. public int Value
  50. {
  51. set
  52. {
  53. if (value > m_maxValue)
  54. m_value = m_maxValue;
  55. else if (value < 0)
  56. m_value = 0;
  57. else
  58. m_value = value;
  59. if (ValueChanged != null)
  60. ValueChanged(this, null);
  61. Refresh();
  62. }
  63. get
  64. {
  65. return m_value;
  66. }
  67. }
  68. /// <summary>
  69. /// The m maximum value
  70. /// </summary>
  71. private int m_maxValue = 100;
  72. /// <summary>
  73. /// Gets or sets the maximum value.
  74. /// </summary>
  75. /// <value>The maximum value.</value>
  76. [Description("最大值"), Category("自定义")]
  77. public int MaxValue
  78. {
  79. get { return m_maxValue; }
  80. set
  81. {
  82. if (value < m_value)
  83. m_maxValue = m_value;
  84. else
  85. m_maxValue = value;
  86. Refresh();
  87. }
  88. }
  89. /// <summary>
  90. /// The m value color
  91. /// </summary>
  92. Color m_valueColor = Color.FromArgb(255, 77, 59);
  93. /// <summary>
  94. /// Gets or sets the color of the value.
  95. /// </summary>
  96. /// <value>The color of the value.</value>
  97. [Description("值进度条颜色"), Category("自定义")]
  98. public Color ValueColor
  99. {
  100. get { return m_valueColor; }
  101. set
  102. {
  103. m_valueColor = value;
  104. Refresh();
  105. }
  106. }
  107. /// <summary>
  108. /// The m value bg color
  109. /// </summary>
  110. private Color m_valueBGColor = Color.FromArgb(228, 231, 237);
  111. /// <summary>
  112. /// Gets or sets the color of the value bg.
  113. /// </summary>
  114. /// <value>The color of the value bg.</value>
  115. [Description("值背景色"), Category("自定义")]
  116. public Color ValueBGColor
  117. {
  118. get { return m_valueBGColor; }
  119. set
  120. {
  121. m_valueBGColor = value;
  122. Refresh();
  123. }
  124. }
  125. /// <summary>
  126. /// The m border color
  127. /// </summary>
  128. private Color m_borderColor = Color.FromArgb(228, 231, 237);
  129. /// <summary>
  130. /// Gets or sets the color of the border.
  131. /// </summary>
  132. /// <value>The color of the border.</value>
  133. [Description("边框颜色"), Category("自定义")]
  134. public Color BorderColor
  135. {
  136. get { return m_borderColor; }
  137. set
  138. {
  139. m_borderColor = value;
  140. Refresh();
  141. }
  142. }
  143. /// <summary>
  144. /// 获取或设置控件显示的文字的字体。
  145. /// </summary>
  146. /// <value>The font.</value>
  147. /// <PermissionSet>
  148. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  149. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  150. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  151. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  152. /// </PermissionSet>
  153. [Description("值字体"), Category("自定义")]
  154. public override Font Font
  155. {
  156. get
  157. {
  158. return base.Font;
  159. }
  160. set
  161. {
  162. base.Font = value;
  163. Refresh();
  164. }
  165. }
  166. /// <summary>
  167. /// 获取或设置控件的前景色。
  168. /// </summary>
  169. /// <value>The color of the fore.</value>
  170. /// <PermissionSet>
  171. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  172. /// </PermissionSet>
  173. [Description("值字体颜色"), Category("自定义")]
  174. public override System.Drawing.Color ForeColor
  175. {
  176. get
  177. {
  178. return base.ForeColor;
  179. }
  180. set
  181. {
  182. base.ForeColor = value;
  183. Refresh();
  184. }
  185. }
  186. /// <summary>
  187. /// The m value text type
  188. /// </summary>
  189. private ValueTextType m_valueTextType = ValueTextType.Percent;
  190. /// <summary>
  191. /// Gets or sets the type of the value text.
  192. /// </summary>
  193. /// <value>The type of the value text.</value>
  194. [Description("值显示样式"), Category("自定义")]
  195. public ValueTextType ValueTextType
  196. {
  197. get { return m_valueTextType; }
  198. set
  199. {
  200. m_valueTextType = value;
  201. Refresh();
  202. }
  203. }
  204. /// <summary>
  205. /// Initializes a new instance of the <see cref="UCProcessLine" /> class.
  206. /// </summary>
  207. public UCProcessLine()
  208. {
  209. Size = new Size(200, 15);
  210. ForeColor = Color.White;
  211. Font = new Font("Arial Unicode MS", 10);
  212. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  213. this.SetStyle(ControlStyles.DoubleBuffer, true);
  214. this.SetStyle(ControlStyles.ResizeRedraw, true);
  215. this.SetStyle(ControlStyles.Selectable, true);
  216. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  217. this.SetStyle(ControlStyles.UserPaint, true);
  218. }
  219. /// <summary>
  220. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  221. /// </summary>
  222. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  223. protected override void OnPaint(PaintEventArgs e)
  224. {
  225. base.OnPaint(e);
  226. Graphics g = e.Graphics;
  227. g.SetGDIHigh();
  228. Brush sb = new SolidBrush(m_valueBGColor);
  229. g.FillRectangle(sb, new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y, base.ClientRectangle.Width - 3, base.ClientRectangle.Height - 2));
  230. GraphicsPath path1 = ControlHelper.CreateRoundedRectanglePath(new Rectangle(base.ClientRectangle.X, base.ClientRectangle.Y + 1, base.ClientRectangle.Width - 3, base.ClientRectangle.Height - 4), 2);
  231. g.DrawPath(new Pen(m_borderColor, 1), path1);
  232. LinearGradientBrush lgb = new LinearGradientBrush(new Point(0, 0), new Point(0, base.ClientRectangle.Height - 3), m_valueColor, Color.FromArgb(250, m_valueColor.R, m_valueColor.G, m_valueColor.B));
  233. g.FillPath(lgb, ControlHelper.CreateRoundedRectanglePath(new Rectangle(0, (base.ClientRectangle.Height - (base.ClientRectangle.Height - 3)) / 2, (base.ClientRectangle.Width - 3) * Value / m_maxValue, base.ClientRectangle.Height - 4), 2));
  234. string strValue = string.Empty;
  235. if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Percent)
  236. strValue = ((float)Value / (float)m_maxValue).ToString("0%");
  237. else if (m_valueTextType == HZH_Controls.Controls.ValueTextType.Absolute)
  238. strValue = Value + "/" + m_maxValue;
  239. if (!string.IsNullOrEmpty(strValue))
  240. {
  241. System.Drawing.SizeF sizeF = g.MeasureString(strValue, Font);
  242. g.DrawString(strValue, Font, new SolidBrush(ForeColor), new PointF((this.Width - sizeF.Width) / 2, (this.Height - sizeF.Height) / 2 + 1));
  243. }
  244. }
  245. }
  246. /// <summary>
  247. /// Enum ValueTextType
  248. /// </summary>
  249. public enum ValueTextType
  250. {
  251. /// <summary>
  252. /// The none
  253. /// </summary>
  254. None,
  255. /// <summary>
  256. /// 百分比
  257. /// </summary>
  258. Percent,
  259. /// <summary>
  260. /// 数值
  261. /// </summary>
  262. Absolute
  263. }
  264. }