(六十二)c#Winform自定义控件-警灯(工业)

前提入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。GitHub:https://github.com/kwwwvagaa/NetWinformControl码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git如果觉得写的还行,请点个star支持一下吧欢迎前来交流探讨:企鹅群568015492麻烦博客下...

(六十二)c#Winform自定义控件-警灯(工业)

前提

入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章。

GitHub:https://github.com/kwwwvagaa/NetWinformControl

码云:https://gitee.com/kwwwvagaa/net_winform_custom_control.git

如果觉得写的还行,请点个 star 支持一下吧

欢迎前来交流探讨: 企鹅群568015492企鹅群568015492

麻烦博客下方点个【推荐】,谢谢

NuGet

Install-Package HZH_Controls

目录

https://www.cnblogs.com/bfyx/p/11364884.html

用处及效果

准备工作

依然GDI ,不懂可以先百度了解下

开始

添加一个类UCAlarmLamp,继承自UserControl

添加属性

 1  /// <summary> 2/// The lamp color 3/// </summary> 4private Color[] lampColor = new Color[] { Color.FromArgb(255, 77, 59) }; 5  6/// <summary> 7/// Gets or sets the color of the lamp. 8/// </summary> 9/// <value>The color of the lamp.</value>10[Description("灯颜色,当需要闪烁时,至少需要2个及以上颜色,不需要闪烁则至少需要1个颜色"), Category("自定义")]11public Color[] LampColor12   {13 get { return lampColor; }14 set15 {16  if (value == null || value.Length <= 0)17return;18  lampColor = value;19  Refresh();20 }21   }22 23/// <summary>24/// The lampstand25/// </summary>26private Color lampstand = Color.FromArgb(105, 105, 105);27 28/// <summary>29/// Gets or sets the lampstand.30/// </summary>31/// <value>The lampstand.</value>32[Description("灯座颜色"), Category("自定义")]33public Color Lampstand34   {35 get { return lampstand; }36 set { lampstand = value; }37   }38 39/// <summary>40/// The twinkle speed41/// </summary>42private int twinkleSpeed = 0;43 44/// <summary>45/// Gets or sets the twinkle speed.46/// </summary>47/// <value>The twinkle speed.</value>48[Description("闪烁间隔时间(毫秒),当为0时不闪烁"), Category("自定义")]49public int TwinkleSpeed50   {51 get { return twinkleSpeed; }52 set53 {54  if (value < 0)55return;56  twinkleSpeed = value;57  if (value == 0 || lampColor.Length <= 1)58  {59timer.Enabled = false;60  }61  else62  {63intColorIndex = 0;64timer.Interval = value;65timer.Enabled = true;66  }67  Refresh();68 }69   }70/// <summary>71/// The timer72/// </summary>73   Timer timer;74/// <summary>75/// The int color index76/// </summary>77int intColorIndex = 0;78/// <summary>79/// The m rect working80/// </summary>81Rectangle m_rectWorking;

重绘

 1  protected override void OnPaint(PaintEventArgs e) 2   { 3 base.OnPaint(e); 4 var g = e.Graphics; 5 g.SetGDIHigh(); 6  7 Color c1 = lampColor[intColorIndex]; 8 GraphicsPath path = new GraphicsPath(); 9 path.AddLine(new Point(m_rectWorking.Left, m_rectWorking.Bottom), new Point(m_rectWorking.Left, m_rectWorking.Top   m_rectWorking.Width));10 path.AddArc(new Rectangle(m_rectWorking.Left, m_rectWorking.Top, m_rectWorking.Width, m_rectWorking.Width), 180f, 180f);11 path.AddLine(new Point(m_rectWorking.Right, m_rectWorking.Topm_rectWorking.Width), new Point(m_rectW
源文地址:https://www.guoxiongfei.cn/cntech/26704.html