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控件
相关推荐
阿蒙Amon17 分钟前
C#扩展方法全解析:给现有类型插上翅膀的魔法
开发语言·c#
尘浮72842 分钟前
60天python训练计划----day59
开发语言·python
Chef_Chen1 小时前
从0开始学习R语言--Day39--Spearman 秩相关
开发语言·学习·r语言
不学会Ⅳ1 小时前
Mac M芯片搭建jdk源码环境(jdk24)
java·开发语言·macos
好开心啊没烦恼2 小时前
Python 数据分析:计算,分组统计1,df.groupby()。听故事学知识点怎么这么容易?
开发语言·python·数据挖掘·数据分析·pandas
lljss20203 小时前
Python11中创建虚拟环境、安装 TensorFlow
开发语言·python·tensorflow
Python×CATIA工业智造6 小时前
Frida RPC高级应用:动态模拟执行Android so文件实战指南
开发语言·python·pycharm
我叫小白菜7 小时前
【Java_EE】单例模式、阻塞队列、线程池、定时器
java·开发语言
狐凄7 小时前
Python实例题:基于 Python 的简单聊天机器人
开发语言·python
weixin_446122468 小时前
JAVA内存区域划分
java·开发语言·redis