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控件
相关推荐
西阳未落1 小时前
C语言中的内存函数(memcpy, memmove, memcmp, memset)
c语言·开发语言
axban3 小时前
QT M/V架构开发实战:QFileSystemModel介绍
开发语言·qt·架构
钢门狂鸭5 小时前
关于rust的crates.io
开发语言·后端·rust
Lionel_SSL5 小时前
《深入理解Java虚拟机》第三章读书笔记:垃圾回收机制与内存管理
java·开发语言·jvm
技术猿188702783516 小时前
PHP 与 WebAssembly 的 “天然隔阂”
开发语言·php·wasm
薄荷撞~可乐6 小时前
C#Task(Api)应用
开发语言·c#
another heaven8 小时前
【Qt VS2022调试时无法查看QString等Qt变量信息】解决方法
开发语言·qt
A黄俊辉A8 小时前
axios+ts封装
开发语言·前端·javascript
杨福瑞9 小时前
C语⾔内存函数
c语言·开发语言
eqwaak09 小时前
科技信息差(9.12)
开发语言·python·科技·量子计算