c#绘制渐变色的Led

项目场景:

c#绘制渐变色的button


c 复制代码
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;

namespace WindowsFormsApp2
{
    public class GradientCircle : UserControl
    {
        public GradientCircle()
        {
            this.Size = new Size(circleSize, circleSize);
            this.Paint += new PaintEventHandler(GradientCircleForm_Paint);
            //this.Click += GradientCircleForm_Click;
        }

       
        private double _scale;
        int circleSize = 300, InitialSize = 300;
        [Category("自定义")]
        public double OverrideScale
        {
            set
            {
                circleSize = (int)(_scale * InitialSize);
                this.Size = new Size(circleSize, circleSize);
                _scale = value;    
                this.Invalidate();
            }
        }



        private Color _clrGradientCircle=Color.Green;
        [Category("自定义")]
        public Color ColorGradientCircle
        {
            get { 
                return _clrGradientCircle; 
            }
            set {
                _clrGradientCircle = value; 
            Invalidate();
            }
        }
        private void GradientCircleForm_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            Rectangle circleRect = new Rectangle(
                (this.ClientSize.Width - circleSize) / 2,
                (this.ClientSize.Height - circleSize) / 2,
                circleSize,
                circleSize);
            using (GraphicsPath path = new GraphicsPath())
            {
                path.AddEllipse(circleRect);
                using (PathGradientBrush brush = new PathGradientBrush(path))
                {
                    // 设置中心颜色
                    brush.CenterColor = Color.White;
                    // 边缘颜色可以是多种颜色
                    Color[] surroundColors = new Color[] { _clrGradientCircle };
                    brush.SurroundColors = surroundColors;
                    g.FillPath(brush, path);
                }
            }
        }
    }
}
相关推荐
加勒比之杰克5 分钟前
【C++11】Lambda 表达式、可变参数、emplace_back 系列
开发语言·c++·lambda·emplace_back·可变参数模版
思成不止于此21 分钟前
【C++ 数据结构】二叉搜索树:原理、实现与核心操作全解析
开发语言·数据结构·c++·笔记·学习·搜索二叉树·c++40周年
P***253936 分钟前
JavaScript部署
开发语言·前端·javascript
王家羽翼-王羽1 小时前
C# 连接 PLC 的S7西门子协议读写,样例分享
c#
斯内科1 小时前
C#进行CAN【控制器局域网】通讯
c#·can·pcanbasic.net
come112341 小时前
现代前端技术栈关系详解 (PHP 开发者特供版)
开发语言·前端·php
yong99901 小时前
基于互信息的Matlab多模态医学图像配准实现
开发语言·matlab
E***q5391 小时前
JavaScript数据挖掘开发
开发语言·javascript·数据挖掘
Lxinccode2 小时前
python(59) : 多线程调用大模型ocr提取图片文本
开发语言·python·图片提取文字·批量提取文件·多线程ocr
自由日记2 小时前
python简单线性回归
开发语言·python·线性回归