指示灯控件"/>
C#自定义带有文本的指示灯控件
文章目录
前言
一、自定义控件创建
1.创建项目
2.项目代码
二、自定义控件调用
1.将自定义控件添加到工具栏
2.控件使用
3.状态切换
效果演示
前言
在实际工程中,往往需要在指示灯上添加文本,故本文提供了一种带有文本的自定义指示灯控件的创建方法。
一、自定义控件创建
1.创建项目
2.项目代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace MyLED
{//指示灯状态public enum LEDstate{OK,NG}public partial class UserControl1: UserControl{public UserControl1(){InitializeComponent();this.MinimumSize=new Size(20,20);//设置控件最小宽和高,防止缩放到最小时超出文本大小报错//双缓冲this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);this.SetStyle(ControlStyles.DoubleBuffer, true);this.SetStyle(ControlStyles.ResizeRedraw, true);this.SetStyle(ControlStyles.Selectable, true);this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);this.SetStyle(ControlStyles.UserPaint, true);}private LEDstate ledValue;//指示灯状态[Description("指示灯状态")]//显示在属性设计窗口public LEDstate LedValue{get { return ledValue; }set{ledValue = value;this.Invalidate();//重绘}}private Color okColor = Color.Lime;//OK状态时指示灯颜色,可根据需要设置[Description("OK状态指示灯颜色")]public Color OKColor{get { return okColor; }set{okColor = value;this.Invalidate();}}private Color ngColor = Color.Red;//NG状态时指示灯颜色,可根据需要设置[Description("NG状态指示灯颜色")]public Color NGColor{get { return ngColor; }set{ngColor = value;this.Invalidate();}}private Graphics graphics;//创建GDI绘图protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);graphics = e.Graphics;//创建画布SetGraphics(graphics);//设置画布int LedWidth = Math.Min(this.Width, this.Height);//获取控件宽和高之中的最小值//设置不同状态时画笔颜色Color color;switch (LedValue){case LEDstate.OK:color = OKColor;break;default:color = NGColor;break;}SolidBrush sb = new SolidBrush(color);//创建填充画笔RectangleF rec = new RectangleF(2, 2, LedWidth - 2, LedWidth - 2);//创建正方形graphics.FillEllipse(sb, rec);//填充正方形内切圆//设置不同状态时指示灯上的文本switch (LedValue){case LEDstate.OK:LedText("OK", LedWidth);break;default:LedText("NG", LedWidth);break;}}/// <summary>/// 设置绘图质量/// </summary>/// <param name="graphics"></param>private void SetGraphics(Graphics graphics){graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;//抗锯齿graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//高质量graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;//文本呈现质量graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;//文本抗锯齿graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;//高质量缩放}/// <summary>/// 设置文字/// </summary>/// <param name="state"></param>/// <param name="edge"></param>private void LedText(string state, int edge){StringFormat format = new StringFormat();//文本布局format.Alignment = StringAlignment.Center;//文本水平居中format.LineAlignment = StringAlignment.Center;//文本垂直居中//绘制文本graphics.DrawString(state, new Font("宋体", (edge - 2) / 6, FontStyle.Bold), Brushes.Black,new RectangleF(2, 2, edge - 2, edge - 2), format);}}
}
二、自定义控件调用
1.将自定义控件添加到工具栏
工具—选择工具箱项—.NET Frameork组件—浏览—按照路径找到自定义控件文件中的MyLED.dll文件—选择。添加完成后就可在左侧工具箱中找到指示灯控件。
*注意:自定义指示灯的文件不可放在中文路径下,否则在添加工具时会报错!
2.控件使用
选中指示灯控件放在窗体中,即可实现想要的效果。
3.状态切换
/// <summary>/// 点击按钮切换指示灯状态/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void Button1_Click(object sender, EventArgs e){//如果指示灯状态为NG就切换为OK,(*注:MyLED.LEDstate.NG中的MyLED为引用自定义控件的名称)if (myLED.LedValue == MyLED.LEDstate.NG){myLED.LedValue = MyLED.LEDstate.OK;}//如果指示灯状态为OK就切换为NGelse{myLED.LedValue = MyLED.LEDstate.NG;}}
效果演示
更多推荐
C#自定义带有文本的指示灯控件
发布评论