UCThermometer.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 2019-09-10
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCThermometer.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 UCThermometer.
  28. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  29. /// </summary>
  30. /// <seealso cref="System.Windows.Forms.UserControl" />
  31. public class UCThermometer : UserControl
  32. {
  33. /// <summary>
  34. /// The glass tube color
  35. /// </summary>
  36. private Color glassTubeColor = Color.FromArgb(211, 211, 211);
  37. /// <summary>
  38. /// Gets or sets the color of the glass tube.
  39. /// </summary>
  40. /// <value>The color of the glass tube.</value>
  41. [Description("玻璃管颜色"), Category("自定义")]
  42. public Color GlassTubeColor
  43. {
  44. get { return glassTubeColor; }
  45. set
  46. {
  47. glassTubeColor = value;
  48. Refresh();
  49. }
  50. }
  51. /// <summary>
  52. /// The mercury color
  53. /// </summary>
  54. private Color mercuryColor = Color.FromArgb(255, 77, 59);
  55. /// <summary>
  56. /// Gets or sets the color of the mercury.
  57. /// </summary>
  58. /// <value>The color of the mercury.</value>
  59. [Description("水印颜色"), Category("自定义")]
  60. public Color MercuryColor
  61. {
  62. get { return mercuryColor; }
  63. set
  64. {
  65. mercuryColor = value;
  66. Refresh();
  67. }
  68. }
  69. /// <summary>
  70. /// The minimum value
  71. /// </summary>
  72. private decimal minValue = 0;
  73. /// <summary>
  74. /// 左侧刻度最小值
  75. /// </summary>
  76. /// <value>The minimum value.</value>
  77. [Description("左侧刻度最小值"), Category("自定义")]
  78. public decimal MinValue
  79. {
  80. get { return minValue; }
  81. set
  82. {
  83. minValue = value;
  84. Refresh();
  85. }
  86. }
  87. /// <summary>
  88. /// The maximum value
  89. /// </summary>
  90. private decimal maxValue = 100;
  91. /// <summary>
  92. /// 左侧刻度最大值
  93. /// </summary>
  94. /// <value>The maximum value.</value>
  95. [Description("左侧刻度最大值"), Category("自定义")]
  96. public decimal MaxValue
  97. {
  98. get { return maxValue; }
  99. set
  100. {
  101. maxValue = value;
  102. Refresh();
  103. }
  104. }
  105. /// <summary>
  106. /// The m value
  107. /// </summary>
  108. private decimal m_value = 10;
  109. /// <summary>
  110. /// 左侧刻度值
  111. /// </summary>
  112. /// <value>The value.</value>
  113. [Description("左侧刻度值"), Category("自定义")]
  114. public decimal Value
  115. {
  116. get { return m_value; }
  117. set
  118. {
  119. m_value = value;
  120. Refresh();
  121. }
  122. }
  123. /// <summary>
  124. /// The split count
  125. /// </summary>
  126. private int splitCount = 0;
  127. /// <summary>
  128. /// 刻度分隔份数
  129. /// </summary>
  130. /// <value>The split count.</value>
  131. [Description("刻度分隔份数"), Category("自定义")]
  132. public int SplitCount
  133. {
  134. get { return splitCount; }
  135. set
  136. {
  137. if (value <= 0)
  138. return;
  139. splitCount = 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 left temperature unit
  188. /// </summary>
  189. private TemperatureUnit leftTemperatureUnit = TemperatureUnit.C;
  190. /// <summary>
  191. /// 左侧刻度单位,不可为none
  192. /// </summary>
  193. /// <value>The left temperature unit.</value>
  194. [Description("左侧刻度单位,不可为none"), Category("自定义")]
  195. public TemperatureUnit LeftTemperatureUnit
  196. {
  197. get { return leftTemperatureUnit; }
  198. set
  199. {
  200. if (value == TemperatureUnit.None)
  201. return;
  202. leftTemperatureUnit = value;
  203. Refresh();
  204. }
  205. }
  206. /// <summary>
  207. /// The right temperature unit
  208. /// </summary>
  209. private TemperatureUnit rightTemperatureUnit = TemperatureUnit.C;
  210. /// <summary>
  211. /// 右侧刻度单位,当为none时,不显示
  212. /// </summary>
  213. /// <value>The right temperature unit.</value>
  214. [Description("右侧刻度单位,当为none时,不显示"), Category("自定义")]
  215. public TemperatureUnit RightTemperatureUnit
  216. {
  217. get { return rightTemperatureUnit; }
  218. set
  219. {
  220. rightTemperatureUnit = value;
  221. Refresh();
  222. }
  223. }
  224. /// <summary>
  225. /// The m rect working
  226. /// </summary>
  227. Rectangle m_rectWorking;
  228. /// <summary>
  229. /// The m rect left
  230. /// </summary>
  231. Rectangle m_rectLeft;
  232. /// <summary>
  233. /// The m rect right
  234. /// </summary>
  235. Rectangle m_rectRight;
  236. /// <summary>
  237. /// Initializes a new instance of the <see cref="UCThermometer" /> class.
  238. /// </summary>
  239. public UCThermometer()
  240. {
  241. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  242. this.SetStyle(ControlStyles.DoubleBuffer, true);
  243. this.SetStyle(ControlStyles.ResizeRedraw, true);
  244. this.SetStyle(ControlStyles.Selectable, true);
  245. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  246. this.SetStyle(ControlStyles.UserPaint, true);
  247. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  248. this.SizeChanged += UCThermometer_SizeChanged;
  249. this.Size = new Size(70, 200);
  250. }
  251. /// <summary>
  252. /// Handles the SizeChanged event of the UCThermometer control.
  253. /// </summary>
  254. /// <param name="sender">The source of the event.</param>
  255. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  256. void UCThermometer_SizeChanged(object sender, EventArgs e)
  257. {
  258. m_rectWorking = new Rectangle(this.Width / 2 - this.Width / 8, this.Width / 4, this.Width / 4, this.Height - this.Width / 2);
  259. m_rectLeft = new Rectangle(0, m_rectWorking.Top + m_rectWorking.Width / 2, (this.Width - this.Width / 4) / 2 - 2, m_rectWorking.Height - m_rectWorking.Width * 2);
  260. m_rectRight = new Rectangle(this.Width - (this.Width - this.Width / 4) / 2 + 2, m_rectWorking.Top + m_rectWorking.Width / 2, (this.Width - this.Width / 4) / 2 - 2, m_rectWorking.Height - m_rectWorking.Width * 2);
  261. }
  262. /// <summary>
  263. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  264. /// </summary>
  265. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  266. protected override void OnPaint(PaintEventArgs e)
  267. {
  268. base.OnPaint(e);
  269. var g = e.Graphics;
  270. g.SetGDIHigh();
  271. //玻璃管管
  272. GraphicsPath path = new GraphicsPath();
  273. path.AddLine(m_rectWorking.Left, m_rectWorking.Bottom, m_rectWorking.Left, m_rectWorking.Top + m_rectWorking.Width / 2);
  274. path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180, 180);
  275. path.AddLine(m_rectWorking.Right, m_rectWorking.Top + m_rectWorking.Width / 2, m_rectWorking.Right, m_rectWorking.Bottom);
  276. path.CloseAllFigures();
  277. g.FillPath(new SolidBrush(glassTubeColor), path);
  278. //底部
  279. var rectDi = new Rectangle(this.Width / 2 - m_rectWorking.Width, m_rectWorking.Bottom - m_rectWorking.Width - 2, m_rectWorking.Width * 2, m_rectWorking.Width * 2);
  280. g.FillEllipse(new SolidBrush(glassTubeColor), rectDi);
  281. g.FillEllipse(new SolidBrush(mercuryColor), new Rectangle(rectDi.Left + 4, rectDi.Top + 4, rectDi.Width - 8, rectDi.Height - 8));
  282. //刻度
  283. decimal decSplit = (maxValue - minValue) / splitCount;
  284. decimal decSplitHeight = m_rectLeft.Height / splitCount;
  285. for (int i = 0; i <= splitCount; i++)
  286. {
  287. g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Left + 2, (float)(m_rectLeft.Bottom - decSplitHeight * i)), new PointF(m_rectLeft.Right, (float)(m_rectLeft.Bottom - decSplitHeight * i)));
  288. var valueLeft = (minValue + decSplit * i).ToString("0.##");
  289. var sizeLeft = g.MeasureString(valueLeft, Font);
  290. g.DrawString(valueLeft, Font, new SolidBrush(ForeColor), new PointF(m_rectLeft.Left, m_rectLeft.Bottom - (float)decSplitHeight * i - sizeLeft.Height - 1));
  291. if (rightTemperatureUnit != TemperatureUnit.None)
  292. {
  293. g.DrawLine(new Pen(new SolidBrush(Color.Black), 1), new PointF(m_rectRight.Left + 2, (float)(m_rectRight.Bottom - decSplitHeight * i)), new PointF(m_rectRight.Right, (float)(m_rectRight.Bottom - decSplitHeight * i)));
  294. var valueRight = GetRightValue(minValue + decSplit * i).ToString("0.##");
  295. var sizeRight = g.MeasureString(valueRight, Font);
  296. g.DrawString(valueRight, Font, new SolidBrush(ForeColor), new PointF(m_rectRight.Right - sizeRight.Width - 1, m_rectRight.Bottom - (float)decSplitHeight * i - sizeRight.Height - 1));
  297. }
  298. if (i != splitCount)
  299. {
  300. if (decSplitHeight > 40)
  301. {
  302. var decSp1 = decSplitHeight / 10;
  303. for (int j = 1; j < 10; j++)
  304. {
  305. if (j == 5)
  306. {
  307. g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Right - 10, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectLeft.Right, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));
  308. if (rightTemperatureUnit != TemperatureUnit.None)
  309. {
  310. g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectRight.Left + 10, (m_rectRight.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectRight.Left, (m_rectRight.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));
  311. }
  312. }
  313. else
  314. {
  315. g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Right - 5, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectLeft.Right, (m_rectLeft.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));
  316. if (rightTemperatureUnit != TemperatureUnit.None)
  317. {
  318. g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectRight.Left + 5, (m_rectRight.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))), new PointF(m_rectRight.Left, (m_rectRight.Bottom - (float)decSplitHeight * i - ((float)decSp1 * j))));
  319. }
  320. }
  321. }
  322. }
  323. else if (decSplitHeight > 10)
  324. {
  325. g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectLeft.Right - 5, (m_rectLeft.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)), new PointF(m_rectLeft.Right, (m_rectLeft.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)));
  326. if (rightTemperatureUnit != TemperatureUnit.None)
  327. {
  328. g.DrawLine(new Pen(new SolidBrush(ForeColor), 1), new PointF(m_rectRight.Left + 5, (m_rectRight.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)), new PointF(m_rectRight.Left, (m_rectRight.Bottom - (float)decSplitHeight * i - (float)decSplitHeight / 2)));
  329. }
  330. }
  331. }
  332. }
  333. //单位
  334. string strLeftUnit = GetUnitChar(leftTemperatureUnit);
  335. g.DrawString(strLeftUnit, Font, new SolidBrush(ForeColor), new PointF(m_rectLeft.Left + 2, 2));
  336. if (rightTemperatureUnit != TemperatureUnit.None)
  337. {
  338. string strRightUnit = GetUnitChar(rightTemperatureUnit);
  339. var rightSize = g.MeasureString(strRightUnit, Font);
  340. g.DrawString(strRightUnit, Font, new SolidBrush(ForeColor), new PointF(m_rectRight.Right - 2 - rightSize.Width, 2));
  341. }
  342. //值
  343. float fltHeightValue = (float)(Value / (maxValue - minValue) * m_rectLeft.Height);
  344. RectangleF rectValue = new RectangleF(m_rectWorking.Left + 4, m_rectLeft.Top + (m_rectLeft.Height - fltHeightValue), m_rectWorking.Width - 8, fltHeightValue + (m_rectWorking.Height - m_rectWorking.Width / 2 - m_rectLeft.Height));
  345. g.FillRectangle(new SolidBrush(mercuryColor), rectValue);
  346. var sizeValue = g.MeasureString(m_value.ToString("0.##"), Font);
  347. g.DrawString(m_value.ToString("0.##"), Font, new SolidBrush(Color.White), new PointF(rectDi.Left + (rectDi.Width - sizeValue.Width) / 2, rectDi.Top + (rectDi.Height - sizeValue.Height) / 2 + 1));
  348. }
  349. /// <summary>
  350. /// Gets the unit character.
  351. /// </summary>
  352. /// <param name="unit">The unit.</param>
  353. /// <returns>System.String.</returns>
  354. private string GetUnitChar(TemperatureUnit unit)
  355. {
  356. string strUnit = "℃";
  357. switch (unit)
  358. {
  359. case TemperatureUnit.C:
  360. strUnit = "℃";
  361. break;
  362. case TemperatureUnit.F:
  363. strUnit = "℉";
  364. break;
  365. case TemperatureUnit.K:
  366. strUnit = "K";
  367. break;
  368. case TemperatureUnit.R:
  369. strUnit = "°R";
  370. break;
  371. case TemperatureUnit.Re:
  372. strUnit = "°Re";
  373. break;
  374. }
  375. return strUnit;
  376. }
  377. /// <summary>
  378. /// Gets the right value.
  379. /// </summary>
  380. /// <param name="decValue">The decimal value.</param>
  381. /// <returns>System.Decimal.</returns>
  382. private decimal GetRightValue(decimal decValue)
  383. {
  384. //先将左侧的换算为摄氏度
  385. var dec = decValue;
  386. switch (leftTemperatureUnit)
  387. {
  388. case TemperatureUnit.F:
  389. dec = (decValue - 32) / (9M / 5M);
  390. break;
  391. case TemperatureUnit.K:
  392. dec = decValue - 273;
  393. break;
  394. case TemperatureUnit.R:
  395. dec = decValue / (5M / 9M) - 273.15M;
  396. break;
  397. case TemperatureUnit.Re:
  398. dec = decValue / 1.25M;
  399. break;
  400. default:
  401. break;
  402. }
  403. switch (rightTemperatureUnit)
  404. {
  405. case TemperatureUnit.C:
  406. return dec;
  407. case TemperatureUnit.F:
  408. return 9M / 5M * dec + 32;
  409. case TemperatureUnit.K:
  410. return dec + 273;
  411. case TemperatureUnit.R:
  412. return (dec + 273.15M) * (5M / 9M);
  413. case TemperatureUnit.Re:
  414. return dec * 1.25M;
  415. }
  416. return decValue;
  417. }
  418. }
  419. /// <summary>
  420. /// Enum TemperatureUnit
  421. /// </summary>
  422. public enum TemperatureUnit
  423. {
  424. /// <summary>
  425. /// 不显示
  426. /// </summary>
  427. None,
  428. /// <summary>
  429. /// 摄氏度
  430. /// </summary>
  431. C,
  432. /// <summary>
  433. /// 华氏度
  434. /// </summary>
  435. F,
  436. /// <summary>
  437. /// 开氏度
  438. /// </summary>
  439. K,
  440. /// <summary>
  441. /// 兰氏度
  442. /// </summary>
  443. R,
  444. /// <summary>
  445. /// 列氏度
  446. /// </summary>
  447. Re
  448. }
  449. }