LED.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Drawing.Drawing2D;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace monitor_main_windows
  12. {
  13. public partial class LED : UserControl
  14. {
  15. protected Color m_color = Color.DimGray;
  16. public LED()
  17. {
  18. InitializeComponent();
  19. #region 【1】设置双缓冲等属性
  20. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  21. this.SetStyle(ControlStyles.DoubleBuffer, true);
  22. this.SetStyle(ControlStyles.ResizeRedraw, true);
  23. this.SetStyle(ControlStyles.Selectable, true);
  24. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  25. this.SetStyle(ControlStyles.UserPaint, true);
  26. timer.Enabled = true;
  27. timer.Interval = 500;
  28. timer.Tick += Timer_Tick;
  29. #endregion
  30. }
  31. private void LED_Load(object sender, EventArgs e)
  32. {
  33. }
  34. private void Timer_Tick(object sender, EventArgs e)
  35. {
  36. if(Statu==LED_Statu.Normal)
  37. {
  38. m_color = LedNormalColor;
  39. }
  40. if (Statu == LED_Statu.Error)
  41. {
  42. m_color = m_color == LampColor ? LedErrorColor : LampColor;
  43. }
  44. if (Statu == LED_Statu.Disconnected)
  45. {
  46. m_color = m_color == LampColor ? LedDisconnectedColor : LampColor;
  47. }
  48. this.Invalidate();
  49. }
  50. #region 【2】定义三个字段
  51. private SolidBrush sb;
  52. Timer timer = new Timer();
  53. #endregion
  54. #region 【3】添加一个设置Graphics的方法
  55. /*private void SetGraphics(Graphics g)
  56. {
  57. //设置画布的属性
  58. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
  59. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
  60. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  61. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
  62. }*/
  63. #endregion
  64. #region 【4】根据实际控件分析的结果,创建属性
  65. private string m_label = "控件";
  66. [Category("text属性")]
  67. [Description("")]
  68. public string Label
  69. {
  70. get { return m_label; }
  71. set
  72. {
  73. m_label = value;
  74. Refresh();
  75. }
  76. }
  77. public enum Font_Location
  78. {
  79. Top=0,
  80. Bottom=1,
  81. Left=2,
  82. Right=3,
  83. }
  84. protected Font_Location m_font_location = Font_Location.Bottom;
  85. [Category("字体位置属性")]
  86. [Description("")]
  87. public Font_Location FontLocation
  88. {
  89. get { return m_font_location; }
  90. set
  91. {
  92. m_font_location = value;
  93. Refresh();
  94. }
  95. }
  96. protected bool font_horizontal = true;
  97. [Category("字体位置属性")]
  98. [Description("")]
  99. public bool FontHorizontal
  100. {
  101. get { return font_horizontal; }
  102. set
  103. {
  104. font_horizontal = value;
  105. Refresh();
  106. }
  107. }
  108. public enum LED_Statu
  109. {
  110. Normal=0,
  111. Error,
  112. Disconnected
  113. }
  114. protected LED_Statu m_led_statu = LED_Statu.Error;
  115. public LED_Statu Statu
  116. {
  117. get { return m_led_statu; }
  118. set
  119. {
  120. m_led_statu = value;
  121. Refresh();
  122. }
  123. }
  124. [Category("jason控件属性")]
  125. [Description("TRUE的时候LED指示灯颜色")]
  126. private Color m_ledNormalColor = Color.Green;
  127. public Color LedNormalColor
  128. {
  129. get { return m_ledNormalColor; }
  130. set
  131. {
  132. m_ledNormalColor = value;
  133. this.Invalidate();
  134. }
  135. }
  136. private Color m_ledErrorColor = Color.Red;
  137. [Category("jason控件属性")]
  138. [Description("False的时候LED指示灯颜色")]
  139. public Color LedErrorColor
  140. {
  141. get { return m_ledErrorColor; }
  142. set
  143. {
  144. m_ledErrorColor = value;
  145. this.Invalidate();
  146. }
  147. }
  148. private Color m_ledDisconnectedColor = Color.DimGray;
  149. [Category("jason控件属性")]
  150. [Description("False的时候LED指示灯颜色")]
  151. public Color LedDisconnectedColor
  152. {
  153. get { return m_ledDisconnectedColor; }
  154. set
  155. {
  156. m_ledDisconnectedColor = value;
  157. this.Invalidate();
  158. }
  159. }
  160. private Color m_lampColor = Color.Transparent;
  161. [Category("jason控件属性")]
  162. [Description("LED指示灯演示")]
  163. public Color LampColor
  164. {
  165. get { return m_lampColor; }
  166. set
  167. {
  168. m_lampColor = value;
  169. this.Invalidate();
  170. }
  171. }
  172. private bool isBorder = true;
  173. [Category("jason控件属性")]
  174. [Description("是否有边框")]
  175. public bool IsBorder
  176. {
  177. get { return isBorder; }
  178. set
  179. {
  180. isBorder = value;
  181. this.Invalidate();
  182. }
  183. }
  184. private int borderWidth = 5;
  185. [Category("jason控件属性")]
  186. [Description("圆环的宽度")]
  187. public int BorderWidth
  188. {
  189. get { return borderWidth; }
  190. set
  191. {
  192. borderWidth = value;
  193. this.Invalidate();
  194. }
  195. }
  196. private bool isHighLight = true;
  197. [Category("jason控件属性")]
  198. [Description("是否高亮")]
  199. public bool IsHighLight
  200. {
  201. get { return isHighLight; }
  202. set
  203. {
  204. isHighLight = value;
  205. this.Invalidate();
  206. }
  207. }
  208. private Color centerColor = Color.White;
  209. [Category("jason控件属性")]
  210. [Description("渐变中心的颜色")]
  211. public Color CenterColor
  212. {
  213. get { return centerColor; }
  214. set
  215. {
  216. centerColor = value;
  217. this.Invalidate();
  218. }
  219. }
  220. private bool isFlash = true;
  221. [Category("jason控件属性")]
  222. [Description("是否闪烁")]
  223. public bool IsFlash
  224. {
  225. get { return isFlash; }
  226. set
  227. {
  228. isFlash = value;
  229. this.Invalidate();
  230. }
  231. }
  232. private int flashInterval = 500;
  233. [Category("jason控件属性")]
  234. [Description("闪烁的频率")]
  235. public int FlashInterval
  236. {
  237. get { return flashInterval; }
  238. set
  239. {
  240. flashInterval = value;
  241. timer.Interval = flashInterval;//timer的时间间隔要放在这里
  242. this.Invalidate();
  243. }
  244. }
  245. #endregion
  246. #region 【5】创建重绘的事件
  247. protected override void OnPaint(PaintEventArgs e)
  248. {
  249. base.OnPaint(e);
  250. var g = e.Graphics;
  251. g.SmoothingMode = SmoothingMode.AntiAlias; //图片柔顺模式
  252. g.InterpolationMode = InterpolationMode.HighQualityBicubic;//高质量
  253. g.CompositingQuality = CompositingQuality.HighQuality;//
  254. g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
  255. //g.Clear(this.BackColor);
  256. System.Drawing.SizeF sizeFont = g.MeasureString(Label, this.Font);
  257. #region 1,画一个圆
  258. int bord = 2;
  259. int w1 = this.Width-2*bord ;
  260. int w2 = this.Height - (int)sizeFont.Height - 2*bord;
  261. int LEDWidth = Math.Min(w1, w2);
  262. int cx = (this.Width - LEDWidth) / 2 + 1;
  263. int cy = 1;
  264. int txtX = (this.Width - (int)sizeFont.Width) / 2 + 1;
  265. int txtY = Height - (int)sizeFont.Height;
  266. if (FontHorizontal == false)
  267. {
  268. w1 = this.Width-2*bord;
  269. w2 = this.Height - (int)sizeFont.Width - bord;
  270. LEDWidth = Math.Min(w1, w2);
  271. cx = (this.Width - LEDWidth) / 2 + 1;
  272. cy = (this.Height - (int)sizeFont.Width - bord - LEDWidth) / 2 + 1;
  273. txtX = (this.Width - (int)sizeFont.Height) / 2 + 1;
  274. txtY = Height- (int)sizeFont.Width;
  275. }
  276. if (FontLocation == Font_Location.Top)
  277. {
  278. if (FontHorizontal==true)
  279. {
  280. cy = (Height-(int)sizeFont.Height-bord)/2-LEDWidth/2+ (int)sizeFont.Height + bord;
  281. txtX = (this.Width - (int)sizeFont.Width) / 2 + 1;
  282. txtY = 1;
  283. }
  284. else
  285. {
  286. w1 = this.Width-2*bord;
  287. w2 = this.Height - (int)sizeFont.Width - 2*bord;
  288. LEDWidth = Math.Min(w1, w2);
  289. cx = (this.Width - LEDWidth) / 2 + 1;
  290. cy = (Height - (int)sizeFont.Width - bord) / 2 -LEDWidth/2+ (int)sizeFont.Width + bord;
  291. txtX = (this.Width - (int)sizeFont.Height) / 2 + 1;
  292. txtY = bord;
  293. }
  294. }
  295. if(FontLocation==Font_Location.Left)
  296. {
  297. //横向
  298. if (FontHorizontal==true)
  299. {
  300. w1 = Width - bord - (int)sizeFont.Width - bord;
  301. w2 = Height-2*bord ;
  302. LEDWidth = Math.Min(w1, w2);
  303. cx = bord + (int)sizeFont.Width ;
  304. cy = (Height - LEDWidth) / 2 + 1;
  305. txtX = 1;
  306. txtY = (Height - (int)sizeFont.Height) / 2 + 1;
  307. }
  308. else
  309. {
  310. //竖向
  311. w1 = Width - bord - (int)sizeFont.Height;
  312. w2 = Height-2*bord;
  313. LEDWidth = Math.Min(w1, w2);
  314. cx =(Width-(int)sizeFont.Height-bord)/2-LEDWidth/2+ (int)sizeFont.Height+bord;
  315. cy = (Height-LEDWidth)/2+1 ;
  316. txtX = 1;
  317. txtY = (Height - (int)sizeFont.Width) / 2 + 1;
  318. }
  319. }
  320. if (FontLocation == Font_Location.Right)
  321. {
  322. //横向
  323. if (FontHorizontal == true)
  324. {
  325. w1 = Width - (int)sizeFont.Width - bord;
  326. w2 = Height-2*bord;
  327. LEDWidth = Math.Min(w1, w2);
  328. cx = (Width - (int)sizeFont.Width-LEDWidth)/2+1;
  329. cy = (Height-LEDWidth)/2+1 ;
  330. txtX = Width - (int)sizeFont.Width;
  331. txtY = (Height - (int)sizeFont.Height) / 2 + 1;
  332. }
  333. else
  334. {
  335. //竖向
  336. w1 = Width - (int)sizeFont.Height - bord;
  337. w2 = Height-2*bord;
  338. LEDWidth = Math.Min(w1, w2);
  339. cx = (Width - (int)sizeFont.Height - LEDWidth) / 2 + 1;
  340. cy = (Height - LEDWidth) / 2 + 1;
  341. txtX = Width - (int)sizeFont.Height;
  342. txtY = (Height - (int)sizeFont.Width) / 2 + 1;
  343. }
  344. }
  345. if (LEDWidth <= 0 || cx < 0 || cy < 0)
  346. return;
  347. sb = new SolidBrush(m_color);
  348. RectangleF rec = new RectangleF(cx, cy, LEDWidth, LEDWidth);//创建矩形
  349. g.DrawEllipse(new Pen(m_color), rec);
  350. g.FillEllipse(sb, rec);//画圆
  351. #endregion
  352. #region 2,在圆里面画一个圆环
  353. //如果有边框,那就画一个圆环
  354. /* if (isBorder)//参数这里用字段或属性都可以,如果用属性,程序要都走一些判断的代码
  355. {
  356. p = new Pen(this.BackColor, borderWidth);//使用背景色
  357. //p = new Pen(Color.Red, borderWidth);
  358. float x = 1 + gapWidth + borderWidth * 0.5f;
  359. rec = new RectangleF(x+cx, x, LEDWidth - 2 * x, LEDWidth - 2 * x);
  360. g.DrawEllipse(p, rec);//画圆环
  361. }*/
  362. #endregion
  363. #region 3,渐变色绘制,是否高亮
  364. if (isHighLight)
  365. {
  366. GraphicsPath gp = new GraphicsPath();
  367. //float x = isBorder ? 1 + borderWidth : 1;//使用三元运算来判断,优化代码
  368. //rec = new RectangleF(cx-LEDWidth/2, cy-LEDWidth/2, LEDWidth , LEDWidth);
  369. gp.AddEllipse(rec);//把矩形添加到路径
  370. //渐变色画刷,高亮
  371. PathGradientBrush pgb = new PathGradientBrush(gp);//把路径传入
  372. Color[] surroundColor = new Color[] { m_color };
  373. pgb.CenterColor = this.centerColor;
  374. //设置有多少组颜色来渐变
  375. pgb.SurroundColors = surroundColor;
  376. g.FillPath(pgb, gp);
  377. }
  378. #endregion
  379. if (FontHorizontal == false)
  380. {
  381. g.DrawString(Label, Font, new SolidBrush(Color.DodgerBlue),
  382. new Point(txtX, txtY),new StringFormat(StringFormatFlags.DirectionVertical));
  383. }
  384. else
  385. {
  386. g.DrawString(Label, Font, new SolidBrush(Color.DodgerBlue),
  387. new Point(txtX, txtY));
  388. }
  389. }
  390. #endregion
  391. }
  392. }