鱼C论坛

 找回密码
 立即注册
查看: 4148|回复: 7

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

[复制链接]
发表于 2012-2-25 01:42:57 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
      终于学会了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
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-25 17:06:38 | 显示全部楼层
red.gif

最近写的,但我的这个红绿灯是通过修改shape控件的颜色来实现的,与楼主的原理不一样
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-2-26 14:52:58 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-26 15:13:12 | 显示全部楼层
杰克 发表于 2012-2-26 14:52
能把代码写出来吗,不尽感激
  1. implementation
  2. var
  3.   redtime:Integer ;
  4.   greentime:Integer ;
  5.   n:Integer;
  6. {$R *.dfm}
  7. procedure TForm1.FormCreate(Sender: TObject);
  8. begin
  9. redtime:=5;                                     //默认红灯和绿灯时间5秒
  10. greentime :=5;
  11. Label5.Caption:='【当前时间】:'+DateTimeToStr(Now());  //获取系统时间
  12. end;

  13. procedure TForm1.TrackBar1Change(Sender: TObject);
  14. begin
  15. Label3.Caption :=IntToStr(TrackBar1.Position+5)+'秒';
  16. redtime :=TrackBar1.Position+5;                     // 通过移动TrackBar滑块来设置红灯时间
  17. end;

  18. procedure TForm1.TrackBar2Change(Sender: TObject);
  19. begin
  20. Label4.Caption :=IntToStr(TrackBar2.Position+5)+'秒'; // 通过移动TrackBar滑块来设置绿灯时间
  21. greentime :=TrackBar2.Position+5;
  22. end;

  23. procedure TForm1.Button1Click(Sender: TObject);
  24. begin
  25.    if Button1.Caption='演示' then
  26.      begin
  27.        Timer1.Interval:=1000;
  28.        Button1.Caption:='停止';
  29.      end
  30.     else if Button1.Caption='停止' then
  31.     begin
  32.        Button1.Caption:='演示';         //恢复默认的状态
  33.        Timer1.Interval :=0;
  34.        Timer2.Interval :=0;
  35.        Timer3.Interval :=0;
  36.        Timer4.Interval :=0;
  37.        Shape1.Brush.Color:=clRed;
  38.        Shape2.Brush.Color:=clGray;
  39.        Shape3.Brush.Color:=clGray;
  40.        redtime :=TrackBar1.Position+5;
  41.       end;



  42. end;

  43. procedure TForm1.Timer1Timer(Sender: TObject);    //红灯事件
  44. begin
  45.   if redtime <>1 then
  46.    redtime:=redtime-1
  47.    else
  48.    begin
  49.      redtime :=TrackBar1.Position+5;
  50.      Shape1.Brush.Color:=clGray ;
  51.      Timer1.Interval:=0;
  52.      Shape2.Brush.Color:=clYellow;
  53.      Timer2.Interval:=2000;
  54.    end;
  55. end;


  56. procedure TForm1.Timer2Timer(Sender: TObject);    //黄灯事件
  57. begin
  58. if Shape2.Brush.Color=clYellow then
  59.    Timer2.Interval:=0;
  60.    Shape2.Brush.Color:=clGray ;
  61.    Shape3.Brush.Color:=clGreen ;
  62.    greentime :=TrackBar2.Position+5;
  63.    Timer3.Interval :=1000;
  64. end;

  65. procedure TForm1.Timer3Timer(Sender: TObject);      //绿灯事件
  66. begin
  67.    if greentime <>3 then
  68.    greentime:=greentime-1
  69.    else
  70.    begin                        //剩余2秒时,启动绿灯闪烁
  71.      Timer3.Interval :=0;
  72.      Timer4.Interval :=250;
  73.      n:=8;                     //闪烁8次 ,8*250=2000,刚好是2秒
  74.    end;
  75. end;

  76. procedure TForm1.Timer4Timer(Sender: TObject);  //绿灯闪烁事件

  77. begin
  78. if Shape3.Brush.Color=clGreen then
  79.          Shape3.Brush.Color:=clGray
  80.          else
  81.         Shape3.Brush.Color:=clGreen;
  82. n:=n-1;
  83. if n=0 then
  84. begin
  85. Timer4.Interval:=0;                    //关闭绿灯,开启红灯,如此循环
  86. Shape3.Brush.Color:=clGray;
  87. Shape1.Brush.Color:=clRed;
  88. redtime :=TrackBar1.Position+5;
  89. Timer1.Interval:=1000;
  90. end;
  91. end;




  92. procedure TForm1.Timer5Timer(Sender: TObject);
  93. begin
  94. Label5.Caption:='【当前时间】:'+DateTimeToStr(Now());    //每秒刷新显示系统时间
  95. end;

  96. end.
复制代码
窗体上放置一个Button,五个Label,三个圆形Shape,五个Timer,两个TrackBar。其中前四个Timer的Interval属性为0,Timer5的Interval属性为1000。红绿灯界面是网上找的一个图片,然后把三个圆形Shape对准图片的三个灯。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-26 15:41:40 | 显示全部楼层
杰克 发表于 2012-2-26 14:52
能把代码写出来吗,不尽感激

如果需要源码,去我的网盘下载,我传上去了 http://comeheres.ys168.com/
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-26 16:46:11 | 显示全部楼层
这个程序有什么用处么
小甲鱼最新课程 -> https://ilovefishc.com
 楼主| 发表于 2012-2-26 23:02:42 | 显示全部楼层
comeheres 发表于 2012-2-26 15:13
窗体上放置一个Button,五个Label,三个圆形Shape,五个Timer,两个TrackBar。其中前四个Timer的Interval ...

太谢谢了,这对一个菜鸟来说是个很好的教材,我希望教程应该从很好玩的例子开始,可能不要问为什么,有兴趣了,自己多做几个,多创新几个,自然会产生疑问,那学起来就快多了。我要这段代码就是想知道在delphi里的if语句是怎么写,叫我看教程简单要跳楼,看好玩的例子反而学得很快。谢谢了。
     希望小甲鱼看到。
小甲鱼最新课程 -> https://ilovefishc.com
发表于 2012-2-26 23:17:25 | 显示全部楼层
G0398 发表于 2012-2-26 16:46
这个程序有什么用处么

你说写“Hello World”程序有什么用呢?
小甲鱼最新课程 -> https://ilovefishc.com
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-5 17:25

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表