首页 > 技术文章 > C#红绿状态灯

ike_li 2018-09-05 10:56 原文

1.在Label里 画圆,存在窗体刷新会丢失画。

  public void SetShowConnectStatus(Label lbl, bool isOk)
        {
            lbl.Text = "";
            Graphics gra = lbl.CreateGraphics();
            gra.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            Color c = isOk ? Color.Green : Color.Red;
            Brush bush = new SolidBrush(c);//填充的颜色
            gra.FillEllipse(bush, 10, 10, lbl.Width / 2, lbl.Width / 2);

        }

 2.在控件Paint事件里画,Invalidate 刷新。

   

  private bool isRotaryConnectOK = false;
        private void button1_Click(object sender, EventArgs e)
        {

            isRotaryConnectOK =!isRotaryConnectOK;
            lblRotaryConnectStatus.Invalidate();
        }
       
        private void lblRotaryConnectStatus_Paint(object sender, PaintEventArgs e)
        {
            Graphics gp = e.Graphics;
       gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; Color c
= isRotaryConnectOK ? Color.Lime : Color.Red; SolidBrush s = new SolidBrush(c); gp.FillEllipse(s, 5, 5, lblRotaryConnectStatus.Width/2, lblRotaryConnectStatus.Width / 2); }

void FillEllipse(Brush brush,int x,int y,int width,int height);
其中brush为指定画刷,(x1,y1)为指定矩形的左上角坐标,width为指定矩形的宽,height为指定矩形的高。 

添加文字:

Font font_ = new Font("微软雅黑", 14, FontStyle.Regular);
SolidBrush brush_ = new SolidBrush(Color.White);
gp.DrawString(adcLsbNumber_, font_, brush_,8, 6);

 

推荐阅读