UCLEDNum.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. // ***********************************************************************
  2. // Assembly : HZH_Controls
  3. // Created : 09-02-2019
  4. //
  5. // ***********************************************************************
  6. // <copyright file="UCLEDNum.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. /* 显示位置序号
  27. * ****1***
  28. * * *
  29. * 6 2
  30. * * *
  31. * ****7***
  32. * * *
  33. * 5 3
  34. * * *
  35. * ****4***
  36. */
  37. /// <summary>
  38. /// Class UCLEDNum.
  39. /// Implements the <see cref="System.Windows.Forms.UserControl" />
  40. /// </summary>
  41. /// <seealso cref="System.Windows.Forms.UserControl" />
  42. public class UCLEDNum : UserControl
  43. {
  44. /// <summary>
  45. /// The m draw rect
  46. /// </summary>
  47. Rectangle m_drawRect = Rectangle.Empty;
  48. /// <summary>
  49. /// The m nums
  50. /// </summary>
  51. private static Dictionary<char, int[]> m_nums = new Dictionary<char, int[]>();
  52. /// <summary>
  53. /// Initializes static members of the <see cref="UCLEDNum" /> class.
  54. /// </summary>
  55. static UCLEDNum()
  56. {
  57. m_nums['0'] = new int[] { 1, 2, 3, 4, 5, 6 };
  58. m_nums['1'] = new int[] { 2, 3 };
  59. m_nums['2'] = new int[] { 1, 2, 5, 4, 7 };
  60. m_nums['3'] = new int[] { 1, 2, 7, 3, 4 };
  61. m_nums['4'] = new int[] { 2, 3, 6, 7 };
  62. m_nums['5'] = new int[] { 1, 6, 7, 3, 4 };
  63. m_nums['6'] = new int[] { 1, 6, 5, 4, 3, 7 };
  64. m_nums['7'] = new int[] { 1, 2, 3 };
  65. m_nums['8'] = new int[] { 1, 2, 3, 4, 5, 6, 7 };
  66. m_nums['9'] = new int[] { 1, 2, 3, 4, 7, 6 };
  67. m_nums['A'] = new int[] { 1, 2, 3, 5, 6, 7 };
  68. m_nums['b'] = new int[] { 3, 4, 5, 6, 7 };
  69. m_nums['C'] = new int[] { 1, 6, 5, 4 };
  70. m_nums['c'] = new int[] { 7, 5, 4 };
  71. m_nums['d'] = new int[] { 2, 3, 4, 5, 7 };
  72. m_nums['E'] = new int[] { 1, 4, 5, 6, 7 };
  73. m_nums['F'] = new int[] { 1, 5, 6, 7 };
  74. m_nums['H'] = new int[] { 2, 3, 5, 6, 7 };
  75. m_nums['h'] = new int[] { 3, 5, 6, 7 };
  76. m_nums['J'] = new int[] { 2, 3, 4 };
  77. m_nums['L'] = new int[] { 4, 5, 6 };
  78. m_nums['o'] = new int[] { 3, 4, 5, 7 };
  79. m_nums['P'] = new int[] { 1, 2, 5, 6, 7 };
  80. m_nums['r'] = new int[] { 5, 7 };
  81. m_nums['U'] = new int[] { 2, 3, 4, 5, 6 };
  82. m_nums['-'] = new int[] { 7 };
  83. m_nums[':'] = new int[0];
  84. m_nums['.'] = new int[0];
  85. }
  86. /// <summary>
  87. /// Initializes a new instance of the <see cref="UCLEDNum" /> class.
  88. /// </summary>
  89. public UCLEDNum()
  90. {
  91. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  92. this.SetStyle(ControlStyles.DoubleBuffer, true);
  93. this.SetStyle(ControlStyles.ResizeRedraw, true);
  94. this.SetStyle(ControlStyles.Selectable, true);
  95. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  96. this.SetStyle(ControlStyles.UserPaint, true);
  97. SizeChanged += LEDNum_SizeChanged;
  98. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
  99. Size = new System.Drawing.Size(40, 70);
  100. if (m_drawRect == Rectangle.Empty)
  101. m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
  102. }
  103. /// <summary>
  104. /// Handles the SizeChanged event of the LEDNum control.
  105. /// </summary>
  106. /// <param name="sender">The source of the event.</param>
  107. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  108. void LEDNum_SizeChanged(object sender, EventArgs e)
  109. {
  110. m_drawRect = new Rectangle(1, 1, this.Width - 2, this.Height - 2);
  111. }
  112. /// <summary>
  113. /// The m value
  114. /// </summary>
  115. private char m_value = '0';
  116. /// <summary>
  117. /// Gets or sets the value.
  118. /// </summary>
  119. /// <value>The value.</value>
  120. [Description("值"), Category("自定义")]
  121. public char Value
  122. {
  123. get { return m_value; }
  124. set
  125. {
  126. if (!m_nums.ContainsKey(value))
  127. {
  128. return;
  129. }
  130. if (m_value != value)
  131. {
  132. m_value = value;
  133. Refresh();
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Sets the character value.
  139. /// </summary>
  140. /// <value>The character value.</value>
  141. public LEDNumChar ValueChar
  142. {
  143. get
  144. {
  145. return (LEDNumChar)((int)m_value);
  146. }
  147. set
  148. {
  149. Value = (char)value;
  150. }
  151. }
  152. /// <summary>
  153. /// The m line width
  154. /// </summary>
  155. private int m_lineWidth = 8;
  156. /// <summary>
  157. /// Gets or sets the width of the line.
  158. /// </summary>
  159. /// <value>The width of the line.</value>
  160. [Description("线宽度,为了更好的显示效果,请使用偶数"), Category("自定义")]
  161. public int LineWidth
  162. {
  163. get { return m_lineWidth; }
  164. set
  165. {
  166. m_lineWidth = value;
  167. Refresh();
  168. }
  169. }
  170. /// <summary>
  171. /// 获取或设置控件的前景色。
  172. /// </summary>
  173. /// <value>The color of the fore.</value>
  174. /// <PermissionSet>
  175. /// <IPermission class="System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" version="1" Unrestricted="true" />
  176. /// </PermissionSet>
  177. [Description("颜色"), Category("自定义")]
  178. public override System.Drawing.Color ForeColor
  179. {
  180. get
  181. {
  182. return base.ForeColor;
  183. }
  184. set
  185. {
  186. base.ForeColor = value;
  187. }
  188. }
  189. /// <summary>
  190. /// 引发 <see cref="E:System.Windows.Forms.Control.Paint" /> 事件。
  191. /// </summary>
  192. /// <param name="e">包含事件数据的 <see cref="T:System.Windows.Forms.PaintEventArgs" />。</param>
  193. protected override void OnPaint(PaintEventArgs e)
  194. {
  195. base.OnPaint(e);
  196. e.Graphics.SetGDIHigh();
  197. if (m_value == '.')
  198. {
  199. Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Bottom - m_lineWidth * 2, m_lineWidth, m_lineWidth);
  200. e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2);
  201. }
  202. else if (m_value == ':')
  203. {
  204. Rectangle r1 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2, m_lineWidth, m_lineWidth);
  205. e.Graphics.FillRectangle(new SolidBrush(ForeColor), r1);
  206. Rectangle r2 = new Rectangle(m_drawRect.Left + (m_drawRect.Width - m_lineWidth) / 2, m_drawRect.Top + (m_drawRect.Height / 2 - m_lineWidth) / 2 + m_drawRect.Height / 2, m_lineWidth, m_lineWidth);
  207. e.Graphics.FillRectangle(new SolidBrush(ForeColor), r2);
  208. }
  209. else
  210. {
  211. int[] vs = m_nums[m_value];
  212. if (vs.Contains(1))
  213. {
  214. GraphicsPath path = new GraphicsPath();
  215. path.AddLines(new Point[]
  216. {
  217. new Point(m_drawRect.Left + 2, m_drawRect.Top),
  218. new Point(m_drawRect.Right - 2, m_drawRect.Top),
  219. new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Top+m_lineWidth),
  220. new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Top+m_lineWidth),
  221. new Point(m_drawRect.Left + 2, m_drawRect.Top)
  222. });
  223. path.CloseAllFigures();
  224. e.Graphics.FillPath(new SolidBrush(ForeColor), path);
  225. }
  226. if (vs.Contains(2))
  227. {
  228. GraphicsPath path = new GraphicsPath();
  229. path.AddLines(new Point[]
  230. {
  231. new Point(m_drawRect.Right, m_drawRect.Top),
  232. new Point(m_drawRect.Right, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2),
  233. new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2),
  234. new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2),
  235. new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Top+m_lineWidth),
  236. new Point(m_drawRect.Right, m_drawRect.Top)
  237. });
  238. path.CloseAllFigures();
  239. e.Graphics.FillPath(new SolidBrush(ForeColor), path);
  240. }
  241. if (vs.Contains(3))
  242. {
  243. GraphicsPath path = new GraphicsPath();
  244. path.AddLines(new Point[]
  245. {
  246. new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
  247. new Point(m_drawRect.Right, m_drawRect.Bottom),
  248. new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-m_lineWidth),
  249. new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
  250. new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2),
  251. new Point(m_drawRect.Right, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
  252. });
  253. path.CloseAllFigures();
  254. e.Graphics.FillPath(new SolidBrush(ForeColor), path);
  255. }
  256. if (vs.Contains(4))
  257. {
  258. GraphicsPath path = new GraphicsPath();
  259. path.AddLines(new Point[]
  260. {
  261. new Point(m_drawRect.Left + 2, m_drawRect.Bottom),
  262. new Point(m_drawRect.Right - 2, m_drawRect.Bottom),
  263. new Point(m_drawRect.Right - m_lineWidth-2, m_drawRect.Bottom-m_lineWidth),
  264. new Point(m_drawRect.Left + m_lineWidth+2, m_drawRect.Bottom-m_lineWidth),
  265. new Point(m_drawRect.Left + 2, m_drawRect.Bottom)
  266. });
  267. path.CloseAllFigures();
  268. e.Graphics.FillPath(new SolidBrush(ForeColor), path);
  269. }
  270. if (vs.Contains(5))
  271. {
  272. GraphicsPath path = new GraphicsPath();
  273. path.AddLines(new Point[]
  274. {
  275. new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
  276. new Point(m_drawRect.Left, m_drawRect.Bottom),
  277. new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-m_lineWidth),
  278. new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
  279. new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2-m_lineWidth/2),
  280. new Point(m_drawRect.Left, m_drawRect.Bottom-(m_drawRect.Height-m_lineWidth-4)/2),
  281. });
  282. path.CloseAllFigures();
  283. e.Graphics.FillPath(new SolidBrush(ForeColor), path);
  284. }
  285. if (vs.Contains(6))
  286. {
  287. GraphicsPath path = new GraphicsPath();
  288. path.AddLines(new Point[]
  289. {
  290. new Point(m_drawRect.Left, m_drawRect.Top),
  291. new Point(m_drawRect.Left, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2),
  292. new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2+m_lineWidth/2),
  293. new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+(m_drawRect.Height-m_lineWidth-4)/2),
  294. new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Top+m_lineWidth),
  295. new Point(m_drawRect.Left, m_drawRect.Top)
  296. });
  297. path.CloseAllFigures();
  298. e.Graphics.FillPath(new SolidBrush(ForeColor), path);
  299. }
  300. if (vs.Contains(7))
  301. {
  302. GraphicsPath path = new GraphicsPath();
  303. path.AddLines(new Point[]
  304. {
  305. new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1),
  306. new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1),
  307. new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2-m_lineWidth/2+1),
  308. new Point(m_drawRect.Right-m_lineWidth/2, m_drawRect.Height/2+1),
  309. new Point(m_drawRect.Right-m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1),
  310. new Point(m_drawRect.Left+m_lineWidth, m_drawRect.Height/2+m_lineWidth/2+1),
  311. new Point(m_drawRect.Left+m_lineWidth/2, m_drawRect.Height/2+1)
  312. });
  313. path.CloseAllFigures();
  314. e.Graphics.FillPath(new SolidBrush(ForeColor), path);
  315. }
  316. }
  317. }
  318. }
  319. /// <summary>Enum LEDNumChar</summary>
  320. public enum LEDNumChar : int
  321. {
  322. /// <summary>
  323. /// The character 0
  324. /// </summary>
  325. CHAR_0 = (int)'0',
  326. /// <summary>
  327. /// The character 1
  328. /// </summary>
  329. CHAR_1 = (int)'1',
  330. /// <summary>
  331. /// The character 2
  332. /// </summary>
  333. CHAR_2 = (int)'2',
  334. /// <summary>
  335. /// The character 3
  336. /// </summary>
  337. CHAR_3 = (int)'3',
  338. /// <summary>
  339. /// The character 4
  340. /// </summary>
  341. CHAR_4 = (int)'4',
  342. CHAR_5 = (int)'5',
  343. /// <summary>
  344. /// The character 6
  345. /// </summary>
  346. CHAR_6 = (int)'6',
  347. /// <summary>
  348. /// The character 7
  349. /// </summary>
  350. CHAR_7 = (int)'7',
  351. /// <summary>
  352. /// The character 8
  353. /// </summary>
  354. CHAR_8 = (int)'8',
  355. /// <summary>
  356. /// The character 9
  357. /// </summary>
  358. CHAR_9 = (int)'9',
  359. /// <summary>
  360. /// The character a
  361. /// </summary>
  362. CHAR_A = (int)'A',
  363. /// <summary>
  364. /// The character b
  365. /// </summary>
  366. CHAR_b = (int)'b',
  367. /// <summary>
  368. /// The character c
  369. /// </summary>
  370. CHAR_C = (int)'C',
  371. /// <summary>
  372. /// The character c
  373. /// </summary>
  374. CHAR_c = (int)'c',
  375. /// <summary>
  376. /// The character d
  377. /// </summary>
  378. CHAR_d = (int)'d',
  379. /// <summary>
  380. /// The character e
  381. /// </summary>
  382. CHAR_E = (int)'E',
  383. /// <summary>
  384. /// The character f
  385. /// </summary>
  386. CHAR_F = (int)'F',
  387. /// <summary>
  388. /// The character h
  389. /// </summary>
  390. CHAR_H = (int)'H',
  391. /// <summary>
  392. /// The character h
  393. /// </summary>
  394. CHAR_h = (int)'h',
  395. /// <summary>
  396. /// The character j
  397. /// </summary>
  398. CHAR_J = (int)'J',
  399. /// <summary>
  400. /// The character l
  401. /// </summary>
  402. CHAR_L = (int)'L',
  403. /// <summary>
  404. /// The character o
  405. /// </summary>
  406. CHAR_o = (int)'o',
  407. /// <summary>
  408. /// The character p
  409. /// </summary>
  410. CHAR_P = (int)'P',
  411. /// <summary>
  412. /// The character r
  413. /// </summary>
  414. CHAR_r = (int)'r',
  415. /// <summary>
  416. /// The character u
  417. /// </summary>
  418. CHAR_U = (int)'U',
  419. /// <summary>
  420. /// The character horizontal line
  421. /// </summary>
  422. CHAR_HorizontalLine = (int)'-',
  423. /// <summary>
  424. /// The character colon
  425. /// </summary>
  426. CHAR_Colon = (int)':',
  427. /// <summary>
  428. /// The character dot
  429. /// </summary>
  430. CHAR_Dot = (int)'.',
  431. }
  432. }