C#自带控件实现圆形led指示灯

C#自带控件实现圆形led指示灯

前段时间做项目需要用到圆形指示灯,在网上找了很久都没找到需要的,有说用用户自定义控件自己画一个,有说直接用按钮做指示灯,只是按钮的话,四方形的,不好看。下载了一个圆形按钮自定义的控件,但是并不能在代码中改变颜色,后面还是放弃了。
前天偶然发现一个C#自带的可以用来做圆形指示灯的控件:ovalShape。写了一个测试代码,通过按钮控制圆形指示灯在红色、绿色、灰色之间变换。
VS2010版本:testOvalShape.rar(注意:VS2010以上版本没默认安装Microsoft.VisualBasic.PowerPacks.Vs.dll,需要引用这个dll)
VS2010以上版本:testOvalShape1.rar
ovalShape控件可以通过两种方式改变颜色,一种是BackColor,一种是FillColor,默认情况下,这两种情况都不会显示颜色,因为默认情况都是透明的(Transparent),需要改变BackStyle为Opaque,才会显示BackColor。测试代码中使用的是FillColor实现的,颜色漂亮点。可以按照自己喜欢的填充方式来填充颜色。
FillColor 比FillGradientColor颜色深,看起来中间暗点,FillColor 比FillGradientColor颜色浅,看起来中间在发光。
这里写图片描述 这里写图片描述 这里写图片描述

  • 7
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
以下是使用C#自定义指示灯控件的步骤: 1. 首先,你需要在Visual Studio中创建一个新的WinForm项目。 2. 在解决方案资源管理器中,右键单击项目名称,然后选择“添加”>“用户控件”。 3. 在“添加新项”对话框中,选择“用户控件”模板,将名称设置为“IndicatorLightControl”,然后单击“添加”。 4. 在“IndicatorLightControl.cs”文件中,添加以下代码: ```csharp public partial class IndicatorLightControl : UserControl { private bool _isBlinking = false; private bool _isOn = false; private Color _onColor = Color.Green; private Color _offColor = Color.Red; private int _blinkInterval = 500; public IndicatorLightControl() { InitializeComponent(); } public bool IsBlinking { get { return _isBlinking; } set { _isBlinking = value; if (_isBlinking) { blinkTimer.Start(); } else { blinkTimer.Stop(); _isOn = false; this.BackColor = _offColor; } } } public bool IsOn { get { return _isOn; } set { _isOn = value; if (_isOn) { this.BackColor = _onColor; } else { this.BackColor = _offColor; } } } public Color OnColor { get { return _onColor; } set { _onColor = value; if (_isOn) { this.BackColor = _onColor; } } } public Color OffColor { get { return _offColor; } set { _offColor = value; if (!_isOn) { this.BackColor = _offColor; } } } public int BlinkInterval { get { return _blinkInterval; } set { _blinkInterval = value; blinkTimer.Interval = _blinkInterval; } } private void blinkTimer_Tick(object sender, EventArgs e) { _isOn = !_isOn; if (_isOn) { this.BackColor = _onColor; } else { this.BackColor = _offColor; } } } ``` 5. 在“IndicatorLightControl.Designer.cs”文件中,添加以下代码: ```csharp partial class IndicatorLightControl { private System.ComponentModel.IContainer components = null; private System.Windows.Forms.Timer blinkTimer; private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.blinkTimer = new System.Windows.Forms.Timer(this.components); this.SuspendLayout(); // // blinkTimer // this.blinkTimer.Tick += new System.EventHandler(this.blinkTimer_Tick); // // IndicatorLightControl // this.BackColor = System.Drawing.Color.Red; this.Name = "IndicatorLightControl"; this.Size = new System.Drawing.Size(20, 20); this.ResumeLayout(false); } } ``` 6. 在你的WinForm中,将“IndicatorLightControl”控件拖放到你的窗体上。 7. 在属性窗口中,你可以设置控件的颜色、是否闪烁以及闪烁速度等属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值