C# 绘制GDI红绿灯控件

C# 绘制GDI红绿灯控件

python 复制代码
using System;
using System.Windows.Forms;
using System.Drawing;
 
public class TrafficLightControl : Control
{
    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        Graphics g = e.Graphics;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
 
        // 红色背景
        g.FillEllipse(Brushes.Red, 0, 0, Width, Height);
 
        // 黄色灯(小圆圈)
        g.FillEllipse(Brushes.Yellow, Width / 4, Height / 4, Width / 2, Height / 2);
 
        // 红色灯(小圆圈)
        g.FillEllipse(Brushes.Black, Width / 2, Height / 4, Width / 2, Height / 2);
 
        // 绿色灯(小圆圈)
        g.FillEllipse(Brushes.Green, 3 * (Width / 4), Height / 4, Width / 2, Height / 2);
    }
 
    protected override void OnResize(EventArgs e)
    {
        base.OnResize(e);
        this.Invalidate();
    }
}
 
// 使用方法:
// 1. 添加控件到工具箱
// 2. 在Form上拖拽TrafficLightControl控件
相关推荐
淼澄研学几秒前
Python实战:基于LangChain与Chroma构建企业级RAG知识库问答系统
开发语言·python·langchain
4311媒体网17 分钟前
帝国CMS网站自定义搭建技巧
服务器·开发语言·前端
布莱克60522 分钟前
C++拷贝构造与拷贝赋值运算符的区别详解
开发语言·c++
lhldsg41 分钟前
全民健身解决方案:从共享球场到智能运营的技术实践
java·大数据·开发语言·需求分析
归去来?1 小时前
Python 常用优雅写法
开发语言·python
devilnumber1 小时前
Java 30 组高频技术 / 知识点多角度对比
java·开发语言
何以解忧,唯有..2 小时前
Python 异常处理完全指南:从基础到高级实践
开发语言·前端·python
gugucoding2 小时前
57. 【Java】日志框架:SLF4J与Logback
java·开发语言·logback
此生决int2 小时前
深入理解C++系列(15)——AVL树
开发语言·c++
筱璦2 小时前
PHP+ VUE3机构级股票分仓管理交易系统源码可出
开发语言·php·vue3·量化·股票分仓