UCMeter.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-03
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCMeter.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 UCMeter.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCMeter : UserControl
  32. {
  33. private int splitCount = 10;
  34. /// <summary>
  35. /// Gets or sets the split count.
  36. /// </summary>
  37. /// <value>The split count.</value>
  38. [Description("分隔刻度数量,>1"), Category("自定义")]
  39. public int SplitCount
  40. {
  41. get { return splitCount; }
  42. set
  43. {
  44. if (value < 1)
  45. return;
  46. splitCount = value;
  47. Refresh();
  48. }
  49. }
  50. private int meterDegrees = 150;
  51. /// <summary>
  52. /// Gets or sets the meter degrees.
  53. /// </summary>
  54. /// <value>The meter degrees.</value>
  55. [Description("表盘跨度角度,0-360"), Category("自定义")]
  56. public int MeterDegrees
  57. {
  58. get { return meterDegrees; }
  59. set
  60. {
  61. if (value > 360 || value <= 0)
  62. return;
  63. meterDegrees = value;
  64. Refresh();
  65. }
  66. }
  67. private decimal minValue = 0;
  68. /// <summary>
  69. /// Gets or sets the minimum value.
  70. /// </summary>
  71. /// <value>The minimum value.</value>
  72. [Description("最小值,<MaxValue"), Category("自定义")]
  73. public decimal MinValue
  74. {
  75. get { return minValue; }
  76. set
  77. {
  78. if (value >= maxValue)
  79. return;
  80. minValue = value;
  81. Refresh();
  82. }
  83. }
  84. private decimal maxValue = 100;
  85. /// <summary>
  86. /// Gets or sets the maximum value.
  87. /// </summary>
  88. /// <value>The maximum value.</value>
  89. [Description("最大值,>MinValue"), Category("自定义")]
  90. public decimal MaxValue
  91. {
  92. get { return maxValue; }
  93. set
  94. {
  95. if (value <= minValue)
  96. return;
  97. maxValue = value;
  98. Refresh();
  99. }
  100. }
  101. /// <summary>
  102. /// 获取或设置控件显示的文字的字体。
  103. /// </summary>
  104. /// <value>The font.</value>
  105. /// <PermissionSet>
  106. /// <IPermission class="System.Security.Permissions.EnvironmentPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  107. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  108. /// <IPermission class="System.Security.Permissions.SecurityPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Flags="UnmanagedCode, ControlEvidence" />
  109. /// <IPermission class="System.Diagnostics.PerformanceCounterPermission, System, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  110. /// </PermissionSet>
  111. [Description("刻度字体"), Category("自定义")]
  112. public override Font Font
  113. {
  114. get
  115. {
  116. return base.Font;
  117. }
  118. set
  119. {
  120. base.Font = value;
  121. Refresh();
  122. }
  123. }
  124. private decimal m_value = 0;
  125. /// <summary>
  126. /// Gets or sets the value.
  127. /// </summary>
  128. /// <value>The value.</value>
  129. [Description("值,>=MinValue并且<=MaxValue"), Category("自定义")]
  130. public decimal Value
  131. {
  132. get { return m_value; }
  133. set
  134. {
  135. if (value < minValue || value > maxValue)
  136. return;
  137. m_value = value;
  138. Refresh();
  139. }
  140. }
  141. private MeterTextLocation textLocation = MeterTextLocation.None;
  142. /// <summary>
  143. /// Gets or sets the text location.
  144. /// </summary>
  145. /// <value>The text location.</value>
  146. [Description("值和固定文字显示位置"), Category("自定义")]
  147. public MeterTextLocation TextLocation
  148. {
  149. get { return textLocation; }
  150. set
  151. {
  152. textLocation = value;
  153. Refresh();
  154. }
  155. }
  156. private string fixedText;
  157. /// <summary>
  158. /// Gets or sets the fixed text.
  159. /// </summary>
  160. /// <value>The fixed text.</value>
  161. [Description("固定文字"), Category("自定义")]
  162. public string FixedText
  163. {
  164. get { return fixedText; }
  165. set
  166. {
  167. fixedText = value;
  168. Refresh();
  169. }
  170. }
  171. private Font textFont = DefaultFont;
  172. /// <summary>
  173. /// Gets or sets the text font.
  174. /// </summary>
  175. /// <value>The text font.</value>
  176. [Description("值和固定文字字体"), Category("自定义")]
  177. public Font TextFont
  178. {
  179. get { return textFont; }
  180. set
  181. {
  182. textFont = value;
  183. Refresh();
  184. }
  185. }
  186. private Color externalRoundColor = Color.FromArgb(255, 77, 59);
  187. /// <summary>
  188. /// Gets or sets the color of the external round.
  189. /// </summary>
  190. /// <value>The color of the external round.</value>
  191. [Description("外圆颜色"), Category("自定义")]
  192. public Color ExternalRoundColor
  193. {
  194. get { return externalRoundColor; }
  195. set
  196. {
  197. externalRoundColor = value;
  198. Refresh();
  199. }
  200. }
  201. private Color insideRoundColor = Color.FromArgb(255, 77, 59);
  202. /// <summary>
  203. /// Gets or sets the color of the inside round.
  204. /// </summary>
  205. /// <value>The color of the inside round.</value>
  206. [Description("内圆颜色"), Category("自定义")]
  207. public Color InsideRoundColor
  208. {
  209. get { return insideRoundColor; }
  210. set
  211. {
  212. insideRoundColor = value;
  213. Refresh();
  214. }
  215. }
  216. private Color boundaryLineColor = Color.FromArgb(255, 77, 59);
  217. /// <summary>
  218. /// Gets or sets the color of the boundary line.
  219. /// </summary>
  220. /// <value>The color of the boundary line.</value>
  221. [Description("边界线颜色"), Category("自定义")]
  222. public Color BoundaryLineColor
  223. {
  224. get { return boundaryLineColor; }
  225. set
  226. {
  227. boundaryLineColor = value;
  228. Refresh();
  229. }
  230. }
  231. private Color scaleColor = Color.FromArgb(255, 77, 59);
  232. /// <summary>
  233. /// Gets or sets the color of the scale.
  234. /// </summary>
  235. /// <value>The color of the scale.</value>
  236. [Description("刻度颜色"), Category("自定义")]
  237. public Color ScaleColor
  238. {
  239. get { return scaleColor; }
  240. set
  241. {
  242. scaleColor = value;
  243. Refresh();
  244. }
  245. }
  246. private Color scaleValueColor = Color.FromArgb(255, 77, 59);
  247. /// <summary>
  248. /// Gets or sets the color of the scale value.
  249. /// </summary>
  250. /// <value>The color of the scale value.</value>
  251. [Description("刻度值文字颜色"), Category("自定义")]
  252. public Color ScaleValueColor
  253. {
  254. get { return scaleValueColor; }
  255. set
  256. {
  257. scaleValueColor = value;
  258. Refresh();
  259. }
  260. }
  261. private Color pointerColor = Color.FromArgb(255, 77, 59);
  262. /// <summary>
  263. /// Gets or sets the color of the pointer.
  264. /// </summary>
  265. /// <value>The color of the pointer.</value>
  266. [Description("指针颜色"), Category("自定义")]
  267. public Color PointerColor
  268. {
  269. get { return pointerColor; }
  270. set
  271. {
  272. pointerColor = value;
  273. Refresh();
  274. }
  275. }
  276. private Color textColor = Color.FromArgb(255, 77, 59);
  277. /// <summary>
  278. /// Gets or sets the color of the text.
  279. /// </summary>
  280. /// <value>The color of the text.</value>
  281. [Description("值和固定文字颜色"), Category("自定义")]
  282. public Color TextColor
  283. {
  284. get { return textColor; }
  285. set
  286. {
  287. textColor = value;
  288. Refresh();
  289. }
  290. }
  291. Rectangle m_rectWorking;
  292. public UCMeter()
  293. {
  294. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  295. this.SetStyle(ControlStyles.DoubleBuffer, true);
  296. this.SetStyle(ControlStyles.ResizeRedraw, true);
  297. this.SetStyle(ControlStyles.Selectable, true);
  298. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  299. this.SetStyle(ControlStyles.UserPaint, true);
  300. this.SizeChanged += UCMeter1_SizeChanged;
  301. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  302. this.Size = new Size(350, 200);
  303. }
  304. void UCMeter1_SizeChanged(object sender, EventArgs e)
  305. {
  306. m_rectWorking = new Rectangle(10, 10, this.Width - 20, this.Height - 20);
  307. }
  308. protected override void OnPaint(PaintEventArgs e)
  309. {
  310. base.OnPaint(e);
  311. var g = e.Graphics;
  312. g.SetGDIHigh();
  313. //外圆
  314. float fltStartAngle = -90 - (meterDegrees) / 2 + 360;
  315. var r1 = new Rectangle(m_rectWorking.Location, new Size(m_rectWorking.Width, m_rectWorking.Width));
  316. g.DrawArc(new Pen(new SolidBrush(externalRoundColor), 1), r1, fltStartAngle, meterDegrees);
  317. //内圆
  318. var r2 = new Rectangle(m_rectWorking.Left + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Top + (m_rectWorking.Width - m_rectWorking.Width / 4) / 2, m_rectWorking.Width / 4, m_rectWorking.Width / 4);
  319. g.DrawArc(new Pen(new SolidBrush(insideRoundColor), 1), r2, fltStartAngle, meterDegrees);
  320. //边界线
  321. if (meterDegrees != 360)
  322. {
  323. float fltAngle = fltStartAngle - 180;
  324. float intY = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
  325. float intX = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - m_rectWorking.Width / 8) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
  326. float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Sin(Math.PI * (fltAngle / 180.00F))));
  327. float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - (m_rectWorking.Width / 8 * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
  328. g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 1), new PointF(intX, intY), new PointF(fltX1, fltY1));
  329. g.DrawLine(new Pen(new SolidBrush(boundaryLineColor), 1), new PointF(m_rectWorking.Right - (fltX1 - m_rectWorking.Left), fltY1), new PointF(m_rectWorking.Right - (intX - m_rectWorking.Left), intY));
  330. }
  331. //分割线
  332. int _splitCount = splitCount * 2;
  333. float fltSplitValue = (float)meterDegrees / (float)_splitCount;
  334. for (int i = 0; i <= _splitCount; i++)
  335. {
  336. float fltAngle = (fltStartAngle + fltSplitValue * i - 180) % 360;
  337. float fltY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
  338. float fltX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
  339. float fltY2 = 0;
  340. float fltX2 = 0;
  341. if (i % 2 == 0)
  342. {
  343. fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
  344. fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 10) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
  345. if (!(meterDegrees == 360 && i == _splitCount))
  346. {
  347. decimal decValue = minValue + (maxValue - minValue) / _splitCount * i;
  348. var txtSize = g.MeasureString(decValue.ToString("0.##"), this.Font);
  349. float fltFY1 = (float)(m_rectWorking.Top - txtSize.Height / 2 + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
  350. float fltFX1 = (float)(m_rectWorking.Left - txtSize.Width / 2 + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 20) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
  351. g.DrawString(decValue.ToString("0.##"), Font, new SolidBrush(scaleValueColor), fltFX1, fltFY1);
  352. }
  353. }
  354. else
  355. {
  356. fltY2 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Sin(Math.PI * (fltAngle / 180.00F))));
  357. fltX2 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 5) * Math.Cos(Math.PI * (fltAngle / 180.00F)))));
  358. }
  359. g.DrawLine(new Pen(new SolidBrush(scaleColor), i % 2 == 0 ? 2 : 1), new PointF(fltX1, fltY1), new PointF(fltX2, fltY2));
  360. }
  361. //值文字和固定文字
  362. if (textLocation != MeterTextLocation.None)
  363. {
  364. string str = m_value.ToString("0.##");
  365. var txtSize = g.MeasureString(str, textFont);
  366. float fltY = m_rectWorking.Top + m_rectWorking.Width / 4 - txtSize.Height / 2;
  367. float fltX = m_rectWorking.Left + m_rectWorking.Width / 2 - txtSize.Width / 2;
  368. g.DrawString(str, textFont, new SolidBrush(textColor), new PointF(fltX, fltY));
  369. if (!string.IsNullOrEmpty(fixedText))
  370. {
  371. str = fixedText;
  372. txtSize = g.MeasureString(str, textFont);
  373. fltY = m_rectWorking.Top + m_rectWorking.Width / 4 + txtSize.Height / 2;
  374. fltX = m_rectWorking.Left + m_rectWorking.Width / 2 - txtSize.Width / 2;
  375. g.DrawString(str, textFont, new SolidBrush(textColor), new PointF(fltX, fltY));
  376. }
  377. }
  378. //画指针
  379. g.FillEllipse(new SolidBrush(Color.FromArgb(100, pointerColor.R, pointerColor.G, pointerColor.B)), new Rectangle(m_rectWorking.Left + m_rectWorking.Width / 2 - 10, m_rectWorking.Top + m_rectWorking.Width / 2 - 10, 20, 20));
  380. g.FillEllipse(new SolidBrush(pointerColor), new Rectangle(m_rectWorking.Left + m_rectWorking.Width / 2 - 5, m_rectWorking.Top + m_rectWorking.Width / 2 - 5, 10, 10));
  381. float fltValueAngle = (fltStartAngle + ((float)(m_value - minValue) / (float)(maxValue - minValue)) * (float)meterDegrees - 180) % 360;
  382. float intValueY1 = (float)(m_rectWorking.Top + m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 30) * Math.Sin(Math.PI * (fltValueAngle / 180.00F))));
  383. float intValueX1 = (float)(m_rectWorking.Left + (m_rectWorking.Width / 2 - ((m_rectWorking.Width / 2 - 30) * Math.Cos(Math.PI * (fltValueAngle / 180.00F)))));
  384. g.DrawLine(new Pen(new SolidBrush(pointerColor), 3), intValueX1, intValueY1, m_rectWorking.Left + m_rectWorking.Width / 2, m_rectWorking.Top + m_rectWorking.Width / 2);
  385. }
  386. }
  387. /// <summary>
  388. /// Enum MeterTextLocation
  389. /// </summary>
  390. public enum MeterTextLocation
  391. {
  392. /// <summary>
  393. /// The none
  394. /// </summary>
  395. None,
  396. /// <summary>
  397. /// The top
  398. /// </summary>
  399. Top,
  400. /// <summary>
  401. /// The bottom
  402. /// </summary>
  403. Bottom
  404. }
  405. }