杰克 发表于 2012-2-25 01:42:57

“红绿灯”的例子,在delphi里怎么弄

      终于学会了vb的“红绿灯”,觉得好玩,就是每按一次按钮,就亮一次灯,是依次的,但不懂在delphi里怎么弄,哪位朋友可以帮一下忙吗?(请写出代码)
这是vb的代码
    Private Sub Command1_Click()
If Shape2.Visible = False And Shape3.Visible = False Then
    Shape2.Visible = True
   Shape1.Visible = False
   Shape3.Visible = False
ElseIf Shape2.Visible = True Then
      Shape1.Visible = False
      Shape2.Visible = False
      Shape3.Visible = True
    Else: Shape3.Visible = False
          Shape1.Visible = True
          Shape2.Visible = False
         
End If
End Sub

comeheres 发表于 2012-2-25 17:06:38



最近写的,但我的这个红绿灯是通过修改shape控件的颜色来实现的,与楼主的原理不一样

杰克 发表于 2012-2-26 14:52:58

comeheres 发表于 2012-2-25 17:06 static/image/common/back.gif
最近写的,但我的这个红绿灯是通过修改shape控件的颜色来实现的,与楼主的原理不一样

能把代码写出来吗,不尽感激

comeheres 发表于 2012-2-26 15:13:12

杰克 发表于 2012-2-26 14:52 static/image/common/back.gif
能把代码写出来吗,不尽感激

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对准图片的三个灯。

comeheres 发表于 2012-2-26 15:41:40

杰克 发表于 2012-2-26 14:52 static/image/common/back.gif
能把代码写出来吗,不尽感激

如果需要源码,去我的网盘下载,我传上去了 http://comeheres.ys168.com/

G0398 发表于 2012-2-26 16:46:11

这个程序有什么用处么

杰克 发表于 2012-2-26 23:02:42

comeheres 发表于 2012-2-26 15:13 static/image/common/back.gif
窗体上放置一个Button,五个Label,三个圆形Shape,五个Timer,两个TrackBar。其中前四个Timer的Interval ...

太谢谢了,这对一个菜鸟来说是个很好的教材,我希望教程应该从很好玩的例子开始,可能不要问为什么,有兴趣了,自己多做几个,多创新几个,自然会产生疑问,那学起来就快多了。我要这段代码就是想知道在delphi里的if语句是怎么写,叫我看教程简单要跳楼,看好玩的例子反而学得很快。谢谢了。
   希望小甲鱼看到。

comeheres 发表于 2012-2-26 23:17:25

G0398 发表于 2012-2-26 16:46 static/image/common/back.gif
这个程序有什么用处么

{:5_109:}你说写“Hello World”程序有什么用呢?
页: [1]
查看完整版本: “红绿灯”的例子,在delphi里怎么弄