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);
