C#自定義控件指示燈效果
C#用戶控件之指示燈
在體現(xiàn)通訊狀態(tài)、運(yùn)行狀態(tài)等用一個(gè)靚眼的指示燈如何做?

思路(GDI)
- 外環(huán)用筆繪制(Pen),內(nèi)圓用畫(huà)刷(SolidBrush);
兩個(gè)方法(用筆畫(huà)圓,用畫(huà)刷填充圓的內(nèi)部):
- 繪制邊界RectangleF定義的橢圓/圓
DrawEllipse(Pen pen,RectangleF rect)
- 填充RectangleF定義邊框的橢圓的內(nèi)部
FillEllipse(Brush brush,RectangleF rect)
定義屬性
- 指示燈顏色、外環(huán)與邊界的間隙、內(nèi)圓與邊界的間隙、外環(huán)寬度、當(dāng)前顏色
//外環(huán)寬度
private float outWidth = 4.0f;
[Browsable(true)]
[Category("布局_G")]
[Description("外環(huán)的寬度")]
public float OutWidth
{
get { return outWidth; }
set
{
if (value <=0||value<0.1*this.Width ) return;
outWidth = value; this.Invalidate();
}
}//顏色(Color)——備注:寫(xiě)5種顏色屬性(灰色=Gray、棕色=DarkGoldenrod、紅色=Red、藍(lán)色=Blue、綠色=limeGreen<比Green好看些>)
private Color zcolor1 = Color.Gray; //灰色.......寫(xiě)5種
[Browsable(true)]
[Category("布局_G")]
[Description("顏色1")]
public Color ZColor1
{
get { return zcolor1; }
set { zcolor1 = value; this.Invalidate(); }
}
//當(dāng)前顏色獲取(定義一個(gè)私有方法)(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),屬性都是一個(gè)樣往下敲就是
注意:間隙設(shè)置值的范圍(外環(huán)間隙要小于內(nèi)圓間隙)GDI繪制圖形:(外環(huán)、內(nèi)圓)
Color getCurColor = GetCurColor(); //獲取當(dāng)前顏色 //繪制外環(huán)(DrawEllipse-用筆畫(huà)橢圓) 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); //繪制內(nèi)圓(FillEllipse-填充橢圓內(nèi)部) sb = new SolidBrush(getCurColor); rec = new RectangleF(gapIn, gapIn, this.width - 2 * this.gapIn, this.height - 2 * gapIn); g.FillEllipse(sb, rec);
最后生成(閃爍的話,是不是對(duì)用戶更友好呢)


兩種閃爍方法
關(guān)鍵在于timer定時(shí)器的使用,在定時(shí)器的Tick方法中定義變量更替
【1】只內(nèi)圓閃爍(定義內(nèi)圓畫(huà)刷顏色Transparent<透明色>、GetCurColor<當(dāng)前色>兩種畫(huà)刷)
if (this.flickerAct == true)
{
if (this.blink == true) //將blink標(biāo)志位在定時(shí)器的Tick方法中取反 (blink=!blink)
{
sb = new SolidBrush(zcolor6); //zcolor6為透明色
}
else
{
sb = new SolidBrush(getCurColor); //getCurColor為當(dāng)前色
}
}
else
{
sb = new SolidBrush(getCurColor); //不閃爍就定義當(dāng)前色畫(huà)刷
}
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) //定時(shí)器Tick事件方法
{
if (this.flickerVis == true)
{
//顯隱控件
this.Visible=!this.Visible; //整體閃爍只定義Visible即可
this.blink=false;
}
else
{
//內(nèi)圓閃爍標(biāo)志
this.blink = !this.blink;
}
this.Invalidate();
}【3】頻率可調(diào)(定時(shí)器的Interval)
private bool flickerAct = false;
[Browsable(true)]
[Category("布局_G")]
[Description("是否閃爍")]
public bool FlickerAct
{
get { return flickerAct; }
set
{
if (value == true)
{
myTimer.Interval = this.flickerFre; //傳遞給定時(shí)器Interval 一個(gè)int(毫秒刷新率)值即可
this.myTimer.Start(); //閃爍定時(shí)器開(kāi)始
}
else
{
this.myTimer.Stop(); //不閃爍定時(shí)器停止;同時(shí)將標(biāo)志位、顯示置為默認(rèn)值
this.blink = false;
this.Visible = true;
}
flickerAct = value; this.Invalidate();
}
}閃瞎雙眼,捂臉

想要二進(jìn)制使用示例
private void led1_Load(object sender, EventArgs e)
{
bool b = false;
//三元運(yùn)算定義兩種顏色即可
this.led1.CurValue = b ? 2 : 3;
}到此這篇關(guān)于C#自定義控件—指示燈的文章就介紹到這了,更多相關(guān)C#指示燈內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C#使用SQLDMO操作數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了C#使用SQLDMO操作數(shù)據(jù)庫(kù)的方法,實(shí)例分析了基于SQLDMO.dll動(dòng)態(tài)鏈接庫(kù)操作數(shù)據(jù)庫(kù)的相關(guān)技巧,需要的朋友可以參考下2015-06-06
C#利用Refit實(shí)現(xiàn)JWT自動(dòng)續(xù)期詳解
Refit?是一個(gè)受到Square的Retrofit庫(kù)(Java)啟發(fā)的自動(dòng)類型安全REST庫(kù),這篇文章主要為大家介紹了C#如何利用Refit實(shí)現(xiàn)JWT自動(dòng)續(xù)期,感興趣的可以了解下2023-08-08
C#怎樣才能實(shí)現(xiàn)窗體最小化到托盤(pán)呢?
C#怎樣才能實(shí)現(xiàn)窗體最小化到托盤(pán)呢?...2007-03-03
C#實(shí)現(xiàn)字符串模糊匹配的方法小結(jié)
在C#中實(shí)現(xiàn)字符串的模糊匹配可以借助正則表達(dá)式或者一些模糊匹配算法來(lái)實(shí)現(xiàn),文章通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-07-07
c#將list類型轉(zhuǎn)換成DataTable方法示例
將List類型轉(zhuǎn)換成DataTable的通用方法,大家參考使用吧2013-12-12
C++聯(lián)合體轉(zhuǎn)換成C#結(jié)構(gòu)的實(shí)現(xiàn)方法
這篇文章主要介紹了C++聯(lián)合體轉(zhuǎn)換成C#結(jié)構(gòu)的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-08-08
Unity的AssetPostprocessor之Model函數(shù)使用實(shí)戰(zhàn)
這篇文章主要為大家介紹了Unity的AssetPostprocessor之Model函數(shù)使用實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08

