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控件
相关推荐
CoderCodingNo7 分钟前
【GESP】C++ 二级真题解析,[2025年12月]第一题环保能量球
开发语言·c++·算法
独好紫罗兰11 分钟前
对python的再认识-基于数据结构进行-a005-元组-CRUD
开发语言·数据结构·python
chilavert31822 分钟前
技术演进中的开发沉思-356:重排序(中)
java·开发语言
devmoon26 分钟前
为 Pallet 搭建最小化 Mock Runtime 并编写单元测试环境
开发语言·单元测试·区块链·智能合约·polkadot
Coder_Boy_40 分钟前
Java开发者破局指南:跳出内卷,借AI赋能,搭建系统化知识体系
java·开发语言·人工智能·spring boot·后端·spring
Mr_Xuhhh1 小时前
介绍一下ref
开发语言·c++·算法
nbsaas-boot1 小时前
软件开发最核心的理念:接口化与组件化
开发语言
lsx2024061 小时前
Java 对象概述
开发语言
Mr_Xuhhh1 小时前
C++11实现线程池
开发语言·c++·算法
无水先生1 小时前
python函数的参数管理(01)*args和**kwargs
开发语言·python