Winform-自定义按钮_播放图标

发布时间 2023-04-11 21:29:39作者: ꧁执笔小白꧂

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 PlayButton : ToolStripButton
    {

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

            Graphics g = pevent.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;  // 抗锯齿,使边缘平滑

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

            RectangleF rect = new RectangleF(5, 4, (this.Height / 2) + 2, (this.Height / 2) + 2);  // 圆或方形

            List<PointF> pointFs = new List<PointF>();
            pointFs.Add(new PointF(6,5));
            pointFs.Add(new PointF(6, this.Height-5));
            pointFs.Add(new PointF((this.Height / 2)+4, this.Height / 2));

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