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控件
相关推荐
@。12433 分钟前
对于灰度发布(金丝雀发布)的了解
开发语言·前端
hsjkdhs1 小时前
C++之多层继承、多源继承、菱形继承
开发语言·c++·算法
Full Stack Developme2 小时前
Python Redis 教程
开发语言·redis·python
qq_4924484462 小时前
Jmeter设置负载阶梯式压测场景(详解教程)
开发语言·python·jmeter
cimeo3 小时前
【C 学习】12.2-函数补充
学习·c#
ID_180079054733 小时前
京东获取整站实时商品详情数据|商品标题|数据分析提取教程
java·开发语言
qiuiuiu4134 小时前
正点原子RK3568学习日志-编译第一个驱动程序helloworld
linux·c语言·开发语言·单片机
molong9314 小时前
Kotlin 内联函数、高阶函数、扩展函数
android·开发语言·kotlin
盼哥PyAI实验室4 小时前
踏上编程征程,与 Python 共舞
开发语言·python
阿无,5 小时前
Java设计模式之工厂模式
java·开发语言·设计模式