杰克 发表于 2012-2-26 14:52
能把代码写出来吗,不尽感激 implementation
var
redtime:Integer ;
greentime:Integer ;
n:Integer;
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
redtime:=5; //默认红灯和绿灯时间5秒
greentime :=5;
Label5.Caption:='【当前时间】:'+DateTimeToStr(Now()); //获取系统时间
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
Label3.Caption :=IntToStr(TrackBar1.Position+5)+'秒';
redtime :=TrackBar1.Position+5; // 通过移动TrackBar滑块来设置红灯时间
end;
procedure TForm1.TrackBar2Change(Sender: TObject);
begin
Label4.Caption :=IntToStr(TrackBar2.Position+5)+'秒'; // 通过移动TrackBar滑块来设置绿灯时间
greentime :=TrackBar2.Position+5;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if Button1.Caption='演示' then
begin
Timer1.Interval:=1000;
Button1.Caption:='停止';
end
else if Button1.Caption='停止' then
begin
Button1.Caption:='演示'; //恢复默认的状态
Timer1.Interval :=0;
Timer2.Interval :=0;
Timer3.Interval :=0;
Timer4.Interval :=0;
Shape1.Brush.Color:=clRed;
Shape2.Brush.Color:=clGray;
Shape3.Brush.Color:=clGray;
redtime :=TrackBar1.Position+5;
end;
end;
procedure TForm1.Timer1Timer(Sender: TObject); //红灯事件
begin
if redtime <>1 then
redtime:=redtime-1
else
begin
redtime :=TrackBar1.Position+5;
Shape1.Brush.Color:=clGray ;
Timer1.Interval:=0;
Shape2.Brush.Color:=clYellow;
Timer2.Interval:=2000;
end;
end;
procedure TForm1.Timer2Timer(Sender: TObject); //黄灯事件
begin
if Shape2.Brush.Color=clYellow then
Timer2.Interval:=0;
Shape2.Brush.Color:=clGray ;
Shape3.Brush.Color:=clGreen ;
greentime :=TrackBar2.Position+5;
Timer3.Interval :=1000;
end;
procedure TForm1.Timer3Timer(Sender: TObject); //绿灯事件
begin
if greentime <>3 then
greentime:=greentime-1
else
begin //剩余2秒时,启动绿灯闪烁
Timer3.Interval :=0;
Timer4.Interval :=250;
n:=8; //闪烁8次 ,8*250=2000,刚好是2秒
end;
end;
procedure TForm1.Timer4Timer(Sender: TObject); //绿灯闪烁事件
begin
if Shape3.Brush.Color=clGreen then
Shape3.Brush.Color:=clGray
else
Shape3.Brush.Color:=clGreen;
n:=n-1;
if n=0 then
begin
Timer4.Interval:=0; //关闭绿灯,开启红灯,如此循环
Shape3.Brush.Color:=clGray;
Shape1.Brush.Color:=clRed;
redtime :=TrackBar1.Position+5;
Timer1.Interval:=1000;
end;
end;
procedure TForm1.Timer5Timer(Sender: TObject);
begin
Label5.Caption:='【当前时间】:'+DateTimeToStr(Now()); //每秒刷新显示系统时间
end;
end.
窗体上放置一个Button,五个Label,三个圆形Shape,五个Timer,两个TrackBar。其中前四个Timer的Interval属性为0,Timer5的Interval属性为1000。红绿灯界面是网上找的一个图片,然后把三个圆形Shape对准图片的三个灯。
|