using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace _秒黄灯闪烁的实现
{
public partial class Form1 : Form
{
public int time = 30;
public bool isGreen = true;
public bool isRed = false; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
timer1_Tick(sender, e);
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen1 = new Pen(Color.Green);
SolidBrush brush1 = new SolidBrush(Color.Green);
if (time > 0 && time < 4 && isGreen)
{//在0-3秒之间实现黄灯的闪烁效果 //哪位仁兄完成之? 要的就是这一段代码,像实际的交通灯一样闪烁效果,闪烁三次黄颜色以示预警。 }
else
{
g.DrawEllipse(pen1, this.Width / 2, this.Height / 2, 20, 20);
g.FillEllipse(brush1, this.Width / 2, this.Height / 2, 20, 20);
}
} private void timer1_Tick(object sender, EventArgs e)
{
time--;
if (time < 1)
{
time = 30;
if (isGreen)
{
isGreen = false;
isRed = true;
}
else
{
isRed = false;
isGreen = true;
} }
this.Refresh();
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace _秒黄灯闪烁的实现
{
public partial class Form1 : Form
{
public int time = 30;
public bool isGreen = true;
public bool isRed = false; public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
timer1_Tick(sender, e);
} private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Pen pen1 = new Pen(Color.Green);
SolidBrush brush1 = new SolidBrush(Color.Green);
if (time > 0 && time < 4 && isGreen)
{//在0-3秒之间实现黄灯的闪烁效果 //哪位仁兄完成之? 要的就是这一段代码,像实际的交通灯一样闪烁效果,闪烁三次黄颜色以示预警。 }
else
{
g.DrawEllipse(pen1, this.Width / 2, this.Height / 2, 20, 20);
g.FillEllipse(brush1, this.Width / 2, this.Height / 2, 20, 20);
}
} private void timer1_Tick(object sender, EventArgs e)
{
time--;
if (time < 1)
{
time = 30;
if (isGreen)
{
isGreen = false;
isRed = true;
}
else
{
isRed = false;
isGreen = true;
} }
this.Refresh();
}
}
trafficLight.SignalState = TrafficLight.State.Blink;你也可以略微改变代码使得黄灯闪几下后自动进入红灯状态。
public class TrafficLight : UserControl
{
public TrafficLight()
{
this.Size = new System.Drawing.Size(60, 60);
timer.Interval = 500;
timer.Tick += delegate {
isOnWhenBlinking = !isOnWhenBlinking;
lightColor = isOnWhenBlinking ? Color.Yellow : SystemColors.Control;
Invalidate();
};
}
public State SignalState
{
get { return lightState; }
set
{
lightState = value;
if (lightState == State.Blink)
{
timer.Start();
isOnWhenBlinking = true;
lightColor = Color.Yellow;
}
else
{
timer.Stop();
if (lightState == State.Off)
{
lightColor = SystemColors.Control;
}
else
{
lightColor = lightState == State.Green ? Color.Green : Color.Red;
}
}
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
using (Graphics g = e.Graphics)
{
e.Graphics.FillEllipse(new SolidBrush(lightColor), 0, 0, Width, Height);
}
} public enum State
{
Off,
Green,
Blink,
Red
} bool isOnWhenBlinking;
State lightState = State.Green;
Color lightColor = Color.Green;
Timer timer = new Timer();
}