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控件
相关推荐
本郡主是喵13 分钟前
并发编程 - go版
java·服务器·开发语言
..活宝..30 分钟前
【Emgu CV教程】11.2、Scharr边缘检测
图像处理·计算机视觉·c#·emgu cv·图像分析
yngsqq31 分钟前
事件监听 ——CAD C#二次开发
c#
努力学习的小廉42 分钟前
我爱学算法之—— 前缀和(中)
开发语言·redis·算法
The Kite1 小时前
MPLAB X IDE 软件安装与卸载
ide·c#·嵌入式
想带你从多云到转晴1 小时前
02. java: 类与对象
java·开发语言
abments1 小时前
基于ReAction范式的问答系统实现demo
开发语言·python
修电脑的猫2 小时前
带有输入的CDS和程序调用
开发语言·lua
圈圈编码2 小时前
悲观锁和乐观锁
java·开发语言·sql·mysql
多多*2 小时前
基于rpc框架Dubbo实现的微服务转发实战
java·开发语言·前端·redis·职场和发展·蓝桥杯·safari