Winform-自定义按钮_三色灯图标

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Management.Instrumentation;

namespace MyContrl
{
    /// <summary>
    /// 指示灯
    /// </summary>
    public partial class RoundButton : ToolStripButton
    {

        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);

            Graphics g = pevent.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;  // 抗锯齿,使边缘平滑
            RectangleF rect = new RectangleF(5, 4, (this.Height / 2) + 2, (this.Height / 2) + 2);

            SolidBrush sb = new SolidBrush(Color.Red);  // 默认红色

            // 填充控件内部
            if (this.Tag != null && this.Tag.ToString().Contains("1"))  // 1为绿
            {
                sb = new SolidBrush(Color.Green);
            }
            else if (this.Tag != null && this.Tag.ToString().Contains("2"))  // 2为红
            {
                sb = new SolidBrush(Color.Red);
            }
            else{
                sb = new SolidBrush(Color.Yellow);  // 黄
            }

            g.FillEllipse(sb, rect);
        }
    }
}

 

posted @ 2023-04-11 21:28  ꧁执笔小白꧂  阅读(316)  评论(0)    收藏  举报