C#自定义控件—指示灯
<h1 id="c用户控件之指示灯">C#用户控件之指示灯</h1><p><strong>在体现通讯状态、运行状态等用一个靓眼的指示灯如何做?</strong></p>
<p><img src="https://img2024.cnblogs.com/blog/3497053/202409/3497053-20240904235423542-455565344.png" alt="" loading="lazy"></p>
<p><strong>思路(GDI)</strong></p>
<ul>
<li>外环用笔绘制(Pen),内圆用画刷(SolidBrush);</li>
</ul>
<p><strong>两个方法(用笔画圆,用画刷填充圆的内部):</strong></p>
<ol>
<li>绘制边界RectangleF定义的椭圆/圆</li>
</ol>
<p><code>DrawEllipse(Pen pen,RectangleF rect)</code></p>
<ol start="2">
<li>填充RectangleF定义边框的椭圆的内部</li>
</ol>
<p><code>FillEllipse(Brush brush,RectangleF rect)</code></p>
<hr>
<p><strong>定义属性</strong></p>
<ul>
<li>指示灯颜色、外环与边界的间隙、内圆与边界的间隙、外环宽度、当前颜色</li>
</ul>
<pre><code>//外环宽度
private float outWidth = 4.0f;
public float OutWidth
{
get { return outWidth; }
set
{
if (value <=0||value<0.1*this.Width ) return;
outWidth = value; this.Invalidate();
}
}
</code></pre>
<hr>
<pre><code>//颜色(Color)——备注:写5种颜色属性(灰色=Gray、棕色=DarkGoldenrod、红色=Red、蓝色=Blue、绿色=limeGreen<比Green好看些>)
private Color zcolor1 = Color.Gray; //灰色.......写5种
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;
}
//间隙(float),属性都是一个样往下敲就是
注意:间隙设置值的范围(外环间隙要小于内圆间隙)
</code></pre>
<hr>
<p><strong>GDI绘制图形:(外环、内圆)</strong></p>
<pre><code>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);
</code></pre>
<hr>
<p><strong>最后生成</strong>(闪烁的话,是不是对用户更友好呢)</p>
<p><img src="https://img2024.cnblogs.com/blog/3497053/202409/3497053-20240904235423542-455565344.png" alt="" loading="lazy"></p>
<p><img src="https://img2024.cnblogs.com/blog/3497053/202409/3497053-20240905232707799-944782908.gif" alt="" loading="lazy"></p>
<hr>
<p><strong>两种闪烁方法</strong></p>
<p>关键在于timer定时器的使用,在定时器的Tick方法中定义变量更替</p>
<hr>
<p>【1】只内圆闪烁(定义内圆画刷颜色Transparent<透明色>、GetCurColor<当前色>两种画刷)</p>
<pre><code>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);
</code></pre>
<hr>
<p>【2】整体都闪烁(定义控件的Visible)</p>
<pre><code> 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();
}
</code></pre>
<p>【3】频率可调(定时器的Interval)</p>
<hr>
<pre><code>private bool flickerAct = false;
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();
}
}
</code></pre>
<hr>
<h3 id="闪瞎双眼捂脸">闪瞎双眼,捂脸</h3>
<p><img src="https://img2024.cnblogs.com/blog/3497053/202409/3497053-20240905235152727-1160622097.gif" alt="" loading="lazy"></p>
<hr>
<p><strong>想要二进制使用示例</strong></p>
<pre><code>private void led1_Load(object sender, EventArgs e)
{
bool b = false;
//三元运算定义两种颜色即可
this.led1.CurValue = b ? 2 : 3;
}
</code></pre>
<hr>
<h1 id="end">End</h1><br><br>
来源:https://www.cnblogs.com/guoenshuo/p/18397557
頁:
[1]