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控件
相关推荐
小成202303202656 小时前
Linux高级02
linux·开发语言
知行合一。。。6 小时前
Python--04--数据容器(总结)
开发语言·python
咸鱼2.06 小时前
【java入门到放弃】需要背诵
java·开发语言
ZK_H6 小时前
嵌入式c语言——关键字其6
c语言·开发语言·计算机网络·面试·职场和发展
A.A呐6 小时前
【C++第二十九章】IO流
开发语言·c++
椰猫子6 小时前
Java:异常(exception)
java·开发语言
lifewange6 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
cmpxr_7 小时前
【C】原码和补码以及环形坐标取模算法
c语言·开发语言·算法
2401_827499997 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉7 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构