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控件
相关推荐
星空椰10 小时前
Python 面向对象高级:继承与类定义详解
开发语言·python
白露与泡影11 小时前
2026大厂Java面试题大全!牛客网最新版
java·开发语言
凯瑟琳.奥古斯特11 小时前
高阶子查询题目精炼
开发语言·数据库·python·职场和发展·数据库开发
雪度娃娃11 小时前
转向现代C++——在意为改写的函数添加 override
开发语言·c++
喵星人工作室12 小时前
C++火影忍者1.1.2
开发语言·c++
basketball61612 小时前
C++ 中的 ptrdiff_t 详解
开发语言·c++
月亮邮递员61613 小时前
Markdown语法总结
开发语言·前端·javascript
printfLILEI13 小时前
php中的类与对象以及反序列化
linux·开发语言·php
曹牧13 小时前
C#:主线程能够捕获到子线程中的异常
开发语言·数据库·c#
代码中介商13 小时前
深入解析STL中的stack、queue与priority_queue
开发语言·c++