UCProcessLineExt.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-21-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCProcessLineExt.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 UCProcessLineExt.
  29. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. public partial class UCProcessLineExt : UserControl
  33. {
  34. /// <summary>
  35. /// Occurs when [value changed].
  36. /// </summary>
  37. [Description("值变更事件"), Category("自定义")]
  38. public event EventHandler ValueChanged;
  39. /// <summary>
  40. /// Gets or sets the value.
  41. /// </summary>
  42. /// <value>The value.</value>
  43. [Description("当前属性"), Category("自定义")]
  44. public int Value
  45. {
  46. set
  47. {
  48. ucProcessLine1.Value = value;
  49. Refresh();
  50. }
  51. get
  52. {
  53. return ucProcessLine1.Value;
  54. }
  55. }
  56. /// <summary>
  57. /// Gets or sets the maximum value.
  58. /// </summary>
  59. /// <value>The maximum value.</value>
  60. [Description("最大值"), Category("自定义")]
  61. public int MaxValue
  62. {
  63. get { return ucProcessLine1.MaxValue; }
  64. set
  65. {
  66. ucProcessLine1.MaxValue = value;
  67. Refresh();
  68. }
  69. }
  70. /// <summary>
  71. /// Gets or sets the color of the value.
  72. /// </summary>
  73. /// <value>The color of the value.</value>
  74. [Description("值进度条颜色"), Category("自定义")]
  75. public Color ValueColor
  76. {
  77. get { return ucProcessLine1.ValueColor; }
  78. set
  79. {
  80. ucProcessLine1.ValueColor = value;
  81. Refresh();
  82. }
  83. }
  84. /// <summary>
  85. /// Gets or sets the color of the value bg.
  86. /// </summary>
  87. /// <value>The color of the value bg.</value>
  88. [Description("值背景色"), Category("自定义")]
  89. public Color ValueBGColor
  90. {
  91. get { return ucProcessLine1.ValueBGColor; }
  92. set
  93. {
  94. ucProcessLine1.ValueBGColor = value;
  95. Refresh();
  96. }
  97. }
  98. /// <summary>
  99. /// Gets or sets the color of the border.
  100. /// </summary>
  101. /// <value>The color of the border.</value>
  102. [Description("边框颜色"), Category("自定义")]
  103. public Color BorderColor
  104. {
  105. get { return ucProcessLine1.BorderColor; }
  106. set
  107. {
  108. ucProcessLine1.BorderColor = value;
  109. Refresh();
  110. }
  111. }
  112. /// <summary>
  113. /// 获取或设置控件显示的文字的字体。
  114. /// </summary>
  115. /// <value>The font.</value>
  116. /// <PermissionSet>
  117. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  118. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  119. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  120. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  121. /// </PermissionSet>
  122. [Description("值字体"), Category("自定义")]
  123. public override Font Font
  124. {
  125. get
  126. {
  127. return ucProcessLine1.Font;
  128. }
  129. set
  130. {
  131. ucProcessLine1.Font = value;
  132. Refresh();
  133. }
  134. }
  135. /// <summary>
  136. /// 获取或设置控件的前景色。
  137. /// </summary>
  138. /// <value>The color of the fore.</value>
  139. /// <PermissionSet>
  140. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  141. /// </PermissionSet>
  142. [Description("值块颜色"), Category("自定义")]
  143. public override System.Drawing.Color ForeColor
  144. {
  145. get
  146. {
  147. return base.ForeColor;
  148. }
  149. set
  150. {
  151. base.ForeColor = value;
  152. Refresh();
  153. }
  154. }
  155. /// <summary>
  156. /// Initializes a new instance of the <see cref="UCProcessLineExt" /> class.
  157. /// </summary>
  158. public UCProcessLineExt()
  159. {
  160. InitializeComponent();
  161. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  162. this.SetStyle(ControlStyles.DoubleBuffer, true);
  163. this.SetStyle(ControlStyles.ResizeRedraw, true);
  164. this.SetStyle(ControlStyles.Selectable, true);
  165. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  166. this.SetStyle(ControlStyles.UserPaint, true);
  167. ucProcessLine1.ValueChanged += ucProcessLine1_ValueChanged;
  168. }
  169. /// <summary>
  170. /// Handles the ValueChanged event of the ucProcessLine1 control.
  171. /// </summary>
  172. /// <param name="sender">The source of the event.</param>
  173. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  174. void ucProcessLine1_ValueChanged(object sender, EventArgs e)
  175. {
  176. if (ValueChanged != null)
  177. {
  178. ValueChanged(this, e);
  179. }
  180. }
  181. /// <summary>
  182. /// Handles the <see cref="E:Paint" /> event.
  183. /// </summary>
  184. /// <param name="e">The <see cref="PaintEventArgs" /> instance containing the event data.</param>
  185. protected override void OnPaint(PaintEventArgs e)
  186. {
  187. base.OnPaint(e);
  188. e.Graphics.SetGDIHigh();
  189. float fltIndex = (float)this.ucProcessLine1.Value / (float)this.ucProcessLine1.MaxValue;
  190. int x = (int)(fltIndex * this.ucProcessLine1.Width + this.ucProcessLine1.Location.X - 15) - 2;
  191. GraphicsPath path = new GraphicsPath();
  192. Rectangle rect = new Rectangle(x, 1, 30, 20);
  193. int cornerRadius = 2;
  194. path.AddArc(rect.X, rect.Y, cornerRadius * 2, cornerRadius * 2, 180, 90);
  195. path.AddLine(rect.X + cornerRadius, rect.Y, rect.Right - cornerRadius * 2, rect.Y);
  196. path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y, cornerRadius * 2, cornerRadius * 2, 270, 90);
  197. path.AddLine(rect.Right, rect.Y + cornerRadius * 2, rect.Right, rect.Y + rect.Height - cornerRadius * 2);
  198. path.AddArc(rect.X + rect.Width - cornerRadius * 2, rect.Y + rect.Height - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
  199. path.AddLine(rect.Right - cornerRadius * 2, rect.Bottom, rect.Right - cornerRadius * 2 - 5, rect.Bottom);//下
  200. path.AddLine(rect.Right - cornerRadius * 2 - 5, 21, x + 15, ucProcessLine1.Location.Y);
  201. path.AddLine(x + 15, ucProcessLine1.Location.Y, rect.X + cornerRadius * 2 + 5, 21);
  202. path.AddLine(rect.X + cornerRadius * 2 + 5, 20, rect.X + cornerRadius * 2, rect.Bottom);//下
  203. path.AddArc(rect.X, rect.Bottom - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
  204. path.AddLine(rect.X, rect.Bottom - cornerRadius * 2, rect.X, rect.Y + cornerRadius * 2);//上
  205. path.CloseFigure();
  206. e.Graphics.FillPath(new SolidBrush(ForeColor), path);
  207. string strValue = ((float)Value / (float)MaxValue).ToString("0%");
  208. System.Drawing.SizeF sizeF = e.Graphics.MeasureString(strValue, Font);
  209. e.Graphics.DrawString(strValue, Font, new SolidBrush(Color.White), new PointF(x + (30 - sizeF.Width) / 2 + 1, (20 - sizeF.Height) / 2 + 1));
  210. }
  211. }
  212. }