C#自定义带有文本的指示灯控件

前言

在实际工程中,往往需要在指示灯上添加文本,故本文提供了一种带有文本的自定义指示灯控件的创建方法。


一、自定义控件创建

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就切换为NG
            else
            {
                myLED.LedValue = MyLED.LEDstate.NG;
            }
        }

效果演示

  • 2
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值