UCProcessEllipse.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 08-17-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCProcessEllipse.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 UCProcessEllipse.
  29. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  30. /// </summary>
  31. /// <seealso cref="System.Windows.Forms.UserControl" />
  32. public partial class UCProcessEllipse : UserControl
  33. {
  34. /// <summary>
  35. /// Occurs when [value changed].
  36. /// </summary>
  37. [Description("值改变事件"), Category("自定义")]
  38. public event EventHandler ValueChanged;
  39. /// <summary>
  40. /// The m back ellipse color
  41. /// </summary>
  42. private Color m_backEllipseColor = Color.FromArgb(228, 231, 237);
  43. /// <summary>
  44. /// 圆背景色
  45. /// </summary>
  46. /// <value>The color of the back ellipse.</value>
  47. [Description("圆背景色"), Category("自定义")]
  48. public Color BackEllipseColor
  49. {
  50. get { return m_backEllipseColor; }
  51. set
  52. {
  53. m_backEllipseColor = value;
  54. Refresh();
  55. }
  56. }
  57. /// <summary>
  58. /// The m core ellipse color
  59. /// </summary>
  60. private Color m_coreEllipseColor = Color.FromArgb(228, 231, 237);
  61. /// <summary>
  62. /// 内圆颜色,ShowType=Ring 有效
  63. /// </summary>
  64. /// <value>The color of the core ellipse.</value>
  65. [Description("内圆颜色,ShowType=Ring 有效"), Category("自定义")]
  66. public Color CoreEllipseColor
  67. {
  68. get { return m_coreEllipseColor; }
  69. set
  70. {
  71. m_coreEllipseColor = value;
  72. Refresh();
  73. }
  74. }
  75. /// <summary>
  76. /// The m value color
  77. /// </summary>
  78. private Color m_valueColor = Color.FromArgb(255, 77, 59);
  79. /// <summary>
  80. /// Gets or sets the color of the value.
  81. /// </summary>
  82. /// <value>The color of the value.</value>
  83. [Description("值圆颜色"), Category("自定义")]
  84. public Color ValueColor
  85. {
  86. get { return m_valueColor; }
  87. set
  88. {
  89. m_valueColor = value;
  90. Refresh();
  91. }
  92. }
  93. /// <summary>
  94. /// The m is show core ellipse border
  95. /// </summary>
  96. private bool m_isShowCoreEllipseBorder = true;
  97. /// <summary>
  98. /// 内圆是否显示边框,ShowType=Ring 有效
  99. /// </summary>
  100. /// <value><c>true</c> if this instance is show core ellipse border; otherwise, <c>false</c>.</value>
  101. [Description("内圆是否显示边框,ShowType=Ring 有效"), Category("自定义")]
  102. public bool IsShowCoreEllipseBorder
  103. {
  104. get { return m_isShowCoreEllipseBorder; }
  105. set
  106. {
  107. m_isShowCoreEllipseBorder = value;
  108. Refresh();
  109. }
  110. }
  111. /// <summary>
  112. /// The m value type
  113. /// </summary>
  114. private ValueType m_valueType = ValueType.Percent;
  115. /// <summary>
  116. /// 值文字类型
  117. /// </summary>
  118. /// <value>The type of the value.</value>
  119. [Description("值文字类型"), Category("自定义")]
  120. public ValueType ValueType
  121. {
  122. get { return m_valueType; }
  123. set
  124. {
  125. m_valueType = value;
  126. Refresh();
  127. }
  128. }
  129. /// <summary>
  130. /// The m value width
  131. /// </summary>
  132. private int m_valueWidth = 30;
  133. /// <summary>
  134. /// 外圆值宽度
  135. /// </summary>
  136. /// <value>The width of the value.</value>
  137. [Description("外圆值宽度,ShowType=Ring 有效"), Category("自定义")]
  138. public int ValueWidth
  139. {
  140. get { return m_valueWidth; }
  141. set
  142. {
  143. if (value <= 0 || value > Math.Min(this.Width, this.Height))
  144. return;
  145. m_valueWidth = value;
  146. Refresh();
  147. }
  148. }
  149. /// <summary>
  150. /// The m value margin
  151. /// </summary>
  152. private int m_valueMargin = 5;
  153. /// <summary>
  154. /// 外圆值间距
  155. /// </summary>
  156. /// <value>The value margin.</value>
  157. [Description("外圆值间距"), Category("自定义")]
  158. public int ValueMargin
  159. {
  160. get { return m_valueMargin; }
  161. set
  162. {
  163. if (value < 0 || m_valueMargin >= m_valueWidth)
  164. return;
  165. m_valueMargin = value;
  166. Refresh();
  167. }
  168. }
  169. /// <summary>
  170. /// The m maximum value
  171. /// </summary>
  172. private int m_maxValue = 100;
  173. /// <summary>
  174. /// 最大值
  175. /// </summary>
  176. /// <value>The maximum value.</value>
  177. [Description("最大值"), Category("自定义")]
  178. public int MaxValue
  179. {
  180. get { return m_maxValue; }
  181. set
  182. {
  183. if (value > m_value || value <= 0)
  184. return;
  185. m_maxValue = value;
  186. Refresh();
  187. }
  188. }
  189. /// <summary>
  190. /// The m value
  191. /// </summary>
  192. private int m_value = 0;
  193. /// <summary>
  194. /// 当前值
  195. /// </summary>
  196. /// <value>The value.</value>
  197. [Description("当前值"), Category("自定义")]
  198. public int Value
  199. {
  200. get { return m_value; }
  201. set
  202. {
  203. if (m_maxValue < value || value < 0)
  204. return;
  205. m_value = value;
  206. if (ValueChanged != null)
  207. {
  208. ValueChanged(this, null);
  209. }
  210. Refresh();
  211. }
  212. }
  213. /// <summary>
  214. /// The m font
  215. /// </summary>
  216. /// <summary>
  217. /// 获取或设置控件显示的文字的字体。
  218. /// </summary>
  219. /// <value>The font.</value>
  220. /// <PermissionSet>
  221. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  222. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  223. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  224. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  225. /// </PermissionSet>
  226. [Description("文字字体"), Category("自定义")]
  227. public override Font Font
  228. {
  229. get
  230. {
  231. return base.Font;
  232. }
  233. set
  234. {
  235. base.Font = value;
  236. Refresh();
  237. }
  238. }
  239. /// <summary>
  240. /// 获取或设置控件的前景色。
  241. /// </summary>
  242. /// <value>The color of the fore.</value>
  243. /// <PermissionSet>
  244. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  245. /// </PermissionSet>
  246. [Description("文字颜色"), Category("自定义"), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),Localizable(true)]
  247. public override Color ForeColor
  248. {
  249. get
  250. {
  251. return base.ForeColor;
  252. }
  253. set
  254. {
  255. base.ForeColor = value;
  256. Refresh();
  257. }
  258. }
  259. /// <summary>
  260. /// The m show type
  261. /// </summary>
  262. private ShowType m_showType = ShowType.Ring;
  263. /// <summary>
  264. /// Gets or sets the type of the show.
  265. /// </summary>
  266. /// <value>The type of the show.</value>
  267. [Description("显示类型"), Category("自定义")]
  268. public ShowType ShowType
  269. {
  270. get { return m_showType; }
  271. set
  272. {
  273. m_showType = value;
  274. Refresh();
  275. }
  276. }
  277. /// <summary>
  278. /// Initializes a new instance of the <see cref="UCProcessEllipse" /> class.
  279. /// </summary>
  280. public UCProcessEllipse()
  281. {
  282. InitializeComponent();
  283. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  284. this.SetStyle(ControlStyles.DoubleBuffer, true);
  285. this.SetStyle(ControlStyles.ResizeRedraw, true);
  286. this.SetStyle(ControlStyles.Selectable, true);
  287. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  288. this.SetStyle(ControlStyles.UserPaint, true);
  289. ForeColor = Color.White;
  290. Font = new Font("Arial Unicode MS", 20);
  291. }
  292. /// <summary>
  293. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  294. /// </summary>
  295. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  296. protected override void OnPaint(PaintEventArgs e)
  297. {
  298. base.OnPaint(e);
  299. var g = e.Graphics;
  300. g.SetGDIHigh();
  301. int intWidth = Math.Min(this.Size.Width, this.Size.Height) - 1;
  302. //底圆
  303. g.FillEllipse(new SolidBrush(m_backEllipseColor), new Rectangle(new Point(0, 0), new Size(intWidth, intWidth)));
  304. if (m_showType == HZH_Controls.Controls.ShowType.Ring)
  305. {
  306. //中心圆
  307. int intCore = intWidth - m_valueWidth * 2;
  308. g.FillEllipse(new SolidBrush(m_coreEllipseColor), new Rectangle(new Point(m_valueWidth, m_valueWidth), new Size(intCore, intCore)));
  309. //中心圆边框
  310. if (m_isShowCoreEllipseBorder)
  311. {
  312. g.DrawEllipse(new Pen(m_valueColor, 2), new Rectangle(new Point(m_valueWidth + 1, m_valueWidth + 1), new Size(intCore - 1, intCore - 1)));
  313. }
  314. if (m_value > 0 && m_maxValue > 0)
  315. {
  316. float fltPercent = (float)m_value / (float)m_maxValue;
  317. if (fltPercent > 1)
  318. {
  319. fltPercent = 1;
  320. }
  321. g.DrawArc(new Pen(m_valueColor, m_valueWidth - m_valueMargin * 2), new RectangleF(new Point(m_valueWidth / 2 + m_valueMargin / 4, m_valueWidth / 2 + m_valueMargin / 4), new SizeF(intWidth - m_valueWidth - m_valueMargin / 2 + (m_valueMargin == 0 ? 0 : 1), intWidth - m_valueWidth - m_valueMargin / 2 + (m_valueMargin == 0 ? 0 : 1))), -90, fltPercent * 360);
  322. string strValueText = m_valueType == HZH_Controls.Controls.ValueType.Percent ? fltPercent.ToString("0%") : m_value.ToString();
  323. System.Drawing.SizeF _txtSize = g.MeasureString(strValueText, this.Font);
  324. g.DrawString(strValueText, this.Font, new SolidBrush(this.ForeColor), new PointF((intWidth - _txtSize.Width) / 2 + 1, (intWidth - _txtSize.Height) / 2 + 1));
  325. }
  326. }
  327. else
  328. {
  329. if (m_value > 0 && m_maxValue > 0)
  330. {
  331. float fltPercent = (float)m_value / (float)m_maxValue;
  332. if (fltPercent > 1)
  333. {
  334. fltPercent = 1;
  335. }
  336. g.FillPie(new SolidBrush(m_valueColor), new Rectangle(m_valueMargin, m_valueMargin, intWidth - m_valueMargin * 2, intWidth - m_valueMargin * 2), -90, fltPercent * 360);
  337. string strValueText = m_valueType == HZH_Controls.Controls.ValueType.Percent ? fltPercent.ToString("0%") : m_value.ToString();
  338. System.Drawing.SizeF _txtSize = g.MeasureString(strValueText, this.Font);
  339. g.DrawString(strValueText, this.Font, new SolidBrush(this.ForeColor), new PointF((intWidth - _txtSize.Width) / 2 + 1, (intWidth - _txtSize.Height) / 2 + 1));
  340. }
  341. }
  342. }
  343. }
  344. /// <summary>
  345. /// Enum ValueType
  346. /// </summary>
  347. public enum ValueType
  348. {
  349. /// <summary>
  350. /// 百分比
  351. /// </summary>
  352. Percent,
  353. /// <summary>
  354. /// 数值
  355. /// </summary>
  356. Absolute
  357. }
  358. /// <summary>
  359. /// Enum ShowType
  360. /// </summary>
  361. public enum ShowType
  362. {
  363. /// <summary>
  364. /// 圆环
  365. /// </summary>
  366. Ring,
  367. /// <summary>
  368. /// 扇形
  369. /// </summary>
  370. Sector
  371. }
  372. }