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控件
相关推荐
不染尘.6 小时前
二叉树相关题目
开发语言·数据结构·c++·算法
女王大人万岁6 小时前
Go标准库 sync 详解
服务器·开发语言·后端·golang
qq_411262426 小时前
短时间串口发送网络端怎么接收不到
开发语言·php
静谧空间6 小时前
java登录验证码CaptchaConfig
java·开发语言
小高Baby@6 小时前
session、cookie、Jwt-token
开发语言·后端·golang
maplewen.6 小时前
C++11 std::mutex
开发语言·c++
jiaguangqingpanda6 小时前
Day37-20260205
java·开发语言
历程里程碑6 小时前
21:重谈重定义理解一切皆“文件“及缓存区
linux·c语言·开发语言·数据结构·c++·算法·缓存
weixin_433179336 小时前
Python - 软件对象
开发语言·python
Want5956 小时前
Python新春烟花代码
开发语言·python·pygame