![复制代码](//assets.cnblogs.com/images/copycode.gif)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private string strColor;
public Form1()
{
InitializeComponent();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
//report dimensions of clipping rectangle to console
Console.WriteLine("e.ClipRectangle = " + e.ClipRectangle);
//draw outline
g.FillRectangle(Brushes.Black,10,10,50,150);
g.DrawEllipse(Pens.White,15,15,40,40);
g.DrawEllipse(Pens.White,15,60,40,40);
g.DrawEllipse(Pens.White,15,105,40,40);
//use application state to draw exactly one of three lights
switch(strColor)
{
case "red":
g.FillEllipse(Brushes.Red,15,15,40,40);
break;
case "yellow":
g.FillEllipse(Brushes.Yellow,15,60,40,40);
break;
case "green":
g.FillEllipse(Brushes.Green,15,105,40,40);
break;
default:
g.FillEllipse(Brushes.Red,15,15,40,40);
break;
}
}
private void Form1_Click(object sender, EventArgs e)
{
//change application state by altering value of strColor
switch(strColor)
{
case "red":
strColor = "yellow";
break;
case "yellow":
strColor = "green";
break;
default:
strColor = "red";
break;
}
//invaliate the region containing the traffics lights
this.Invalidate(new Rectangle(10, 10, 50, 150));
//request an update
this.Update();
}
}
}
![复制代码](//assets.cnblogs.com/images/copycode.gif)
![](http://hiphotos.baidu.com/ssrstt/pic/item/2249687802087bf465cc7658f2d3572c10dfcfba.jpg)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步