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控件
相关推荐
韩立学长30 分钟前
【开题答辩实录分享】以《基于python的奶茶店分布数据分析与可视化》为例进行答辩实录分享
开发语言·python·数据分析
天若有情67336 分钟前
C++空值初始化利器:empty.h使用指南
开发语言·c++
远远远远子42 分钟前
类与对象 --1
开发语言·c++·算法
无敌最俊朗@1 小时前
C/C++ 关键关键字面试指南 (const, static, volatile, explicit)
c语言·开发语言·c++·面试
2401_831501731 小时前
Python学习之day03学习(文件和异常)
开发语言·python·学习
酷~1 小时前
C语言模拟面向对象编程方法之多态
c语言·开发语言
hui函数2 小时前
python全栈(基础篇)——day03:后端内容(字符串格式化+简单数据类型转换+进制的转换+运算符+实战演示+每日一题)
开发语言·后端·python·全栈
寻星探路2 小时前
Java EE初阶启程记09---多线程案例(2)
java·开发语言·java-ee
froginwe113 小时前
Python 3 输入和输出
开发语言
小何好运暴富开心幸福3 小时前
C++之再谈类与对象
开发语言·c++·vscode