C#自定义控件—指示灯

C#用户控件之指示灯

在体现通讯状态、运行状态等用一个靓眼的指示灯如何做?

思路(GDI)

  • 外环用笔绘制(Pen),内圆用画刷(SolidBrush);

两个方法(用笔画圆,用画刷填充圆的内部):

  1. 绘制边界RectangleF定义的椭圆/圆

DrawEllipse(Pen pen,RectangleF rect)

  1. 填充RectangleF定义边框的椭圆的内部

FillEllipse(Brush brush,RectangleF rect)


定义属性

  • 指示灯颜色、外环与边界的间隙、内圆与边界的间隙、外环宽度、当前颜色
//外环宽度
private float outWidth = 4.0f;
[Browsable(true)]
[Category("布局_G")]
[Description("外环的宽度")]
public float OutWidth
{
    get { return outWidth; }
    set
    {
         if (value <=0||value<0.1*this.Width ) return;
        outWidth = value; this.Invalidate();
    }
}


//颜色(Color)——备注:写5种颜色属性(灰色=Gray、棕色=DarkGoldenrod、红色=Red、蓝色=Blue、绿色=limeGreen<比Green好看些>)
private Color zcolor1 = Color.Gray;   //灰色.......写5种
 [Browsable(true)]
 [Category("布局_G")]
 [Description("颜色1")]
 public Color ZColor1
 {
     get { return zcolor1; }
     set { zcolor1 = value; this.Invalidate(); }
 }
//当前颜色获取(定义一个私有方法)(Int)
  private Color GetCurColor()
  {
      List<Color> colors = new List<Color>();
      colors.Add(zcolor1);
      colors.Add(zcolor2);
      colors.Add(zcolor3);
      colors.Add(zcolor4);
      colors.Add(zcolor5);
      return colors[curValue];
  }
//间隙(float),属性都是一个样往下敲就是
注意:间隙设置值的范围(外环间隙要小于内圆间隙)

GDI绘制图形:(外环、内圆)

Color getCurColor = GetCurColor();  //获取当前颜色

//绘制外环(DrawEllipse-用笔画椭圆)
p = new Pen(getCurColor, outWidth);
RectangleF rec = new RectangleF(this.gapOut, this.gapOut, this.width - 2 * this.gapOut, this.height - 2 * gapOut);
g.DrawEllipse(p, rec);

//绘制内圆(FillEllipse-填充椭圆内部)
sb = new SolidBrush(getCurColor);
rec = new RectangleF(gapIn, gapIn, this.width - 2 * this.gapIn, this.height - 2 * gapIn);
g.FillEllipse(sb, rec);


最后生成(闪烁的话,是不是对用户更友好呢)


两种闪烁方法

关键在于timer定时器的使用,在定时器的Tick方法中定义变量更替


【1】只内圆闪烁(定义内圆画刷颜色Transparent<透明色>、GetCurColor<当前色>两种画刷)

if (this.flickerAct == true)
{
    if (this.blink == true)  //将blink标志位在定时器的Tick方法中取反 (blink=!blink)
    {
        sb = new SolidBrush(zcolor6);  //zcolor6为透明色
    }
    else
    {
        sb = new SolidBrush(getCurColor);  //getCurColor为当前色
    }
}
else
{
    sb = new SolidBrush(getCurColor);  //不闪烁就定义当前色画刷
}
rec = new RectangleF(gapIn, gapIn, this.width - 2 * this.gapIn, this.height - 2 * gapIn);
g.FillEllipse(sb, rec);


【2】整体都闪烁(定义控件的Visible)

 private void MyTimer_Tick(object sender, EventArgs e)  //定时器Tick事件方法
 {
     if (this.flickerVis == true)
     {
         //显隐控件
         this.Visible=!this.Visible;  //整体闪烁只定义Visible即可
         this.blink=false;
     }
     else
     {
         //内圆闪烁标志
         this.blink = !this.blink;
     }
     this.Invalidate();
 }

【3】频率可调(定时器的Interval)


private bool flickerAct = false;
[Browsable(true)]
[Category("布局_G")]
[Description("是否闪烁")]
public bool FlickerAct
{
    get { return flickerAct; }
    set
    {
        if (value == true)
        {
            myTimer.Interval = this.flickerFre;  //传递给定时器Interval 一个int(毫秒刷新率)值即可
            this.myTimer.Start();  //闪烁定时器开始
        }
        else
        {
            this.myTimer.Stop();  //不闪烁定时器停止;同时将标志位、显示置为默认值
            this.blink = false;
            this.Visible = true;
        }
        flickerAct = value; this.Invalidate();
    }
}

闪瞎双眼,捂脸


想要二进制使用示例

  private void led1_Load(object sender, EventArgs e)
  {
      bool b = false;
      //三元运算定义两种颜色即可
      this.led1.CurValue = b ? 2 : 3; 
  }

End

posted @ 2024-09-06 00:00  郭恩硕  阅读(618)  评论(5编辑  收藏  举报