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控件