就像路口的红灯绿灯那样,能变颜色,能闪烁,求教高手!

解决方案 »

  1.   

    方法1:几幅图片变换来实现闪烁.
    方法2:作一个gif文件,播放它.
      

  2.   

    还有就是用shape,动态改变颜色。
      

  3.   

    unit InvokeU;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, ExtCtrls,LightU;type
      TInvokF = class(TForm)
        Button1: TButton;
        TimerShow: TTimer;
        Timer1: TTimer;
        procedure Button1Click(Sender: TObject);
        procedure TimerShowTimer(Sender: TObject);
        procedure Timer1Timer(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      InvokF: TInvokF;
      FLight:array[1..4] of TLight;
      Duration:array[1..3] of byte;
      WhichColor:byte;
      State:byte;
    implementationuses Math;{$R *.dfm}procedure TInvokF.Button1Click(Sender: TObject);
    var
      i:integer;
    begin
      For i:=1 to 4 do
        begin
          FLight[i]:=TLight.Create(self);
          with FLight[i] do
            begin
            Left:=80*i;
            Top:=30;
            Width:=50;
            Height:=50;
            Parent:=InvokF;
            Shape:=stCircle;
            Brush.Color :=clBtnFace;
          end;
        end;
      //
      //FLight[1].Left :=160;
      //FLight[4].top:=154;
      //FLight[2].Visible:=false;
      //FLight[3].Visible:=false;
      //
      State:=0;
      Duration[1]:=0;
      FLight[1].Duration:=5;
      FLight[2].Duration:=2;
      FLight[3].Duration:=5;
      WhichColor:=Random(271) mod 4;
      if WhichColor=0 then WhichColor:=1;
      TimerShow.Enabled:=true;
      //Timer1.Enabled:=true;
      //
      Canvas.Pen.Width:=16;
      Canvas.MoveTo(0,round(Self.height/2));
      Canvas.LineTo(self.Width ,round(Self.height/2));
      Canvas.MoveTo(round(Self.Width /2),0);
      Canvas.LineTo(round(Self.Width /2),self.height);
    end;procedure TInvokF.TimerShowTimer(Sender: TObject);
    begin
      case WhichColor of
        1:  begin
              Caption:='红灯 '+inttostr(FLight[1].Duration)+' 秒 '+inttostr(Duration[1]);
              FLight[1].Brush.Color:=clRed;          if Duration[1] >=FLight[1].Duration then
                begin
                  Duration[1]:=0;
                  FLight[1].Brush.Color:=clBtnShadow;
                  FLight[2].Brush.Color:=clYellow;
                  WhichColor:=2;
                  State:=0;
                end
              else
                Duration[1]:=Duration[1]+1;
            end;
        2:  begin
              Caption:='黄灯 '+inttostr(FLight[2].Duration)+' 秒 '+inttostr(Duration[2]);
              if Duration[2] >=FLight[2].Duration then
                  begin
                  Duration[2] :=0;
                  FLight[2].Brush.Color:=clBtnShadow;
                  case State of
                  0:  begin
                        FLight[3].Brush.Color:=clGreen;
                        WhichColor:=3;
                      end;
                  1:  begin
                        FLight[1].Brush.Color:=clRed;
                        WhichColor:=1;
                      end;
                  end;
                  end
              else
                  Duration[2]:=Duration[2]+1;
            end;
        3:  begin
              Caption:='绿灯 '+inttostr(FLight[3].Duration)+' 秒 '+inttostr(Duration[3]);
              if Duration[3]>=FLight[3].Duration then
                begin
                  Duration[3]:=0;
                  FLight[3].Brush.Color:=clBtnShadow;
                  FLight[2].Brush.Color:=clYellow;
                  WhichColor:=2;
                  State:=1;
                end
              else
                Duration[3]:=Duration[3]+1;
            end;
      end;
    end;
    procedure TInvokF.Timer1Timer(Sender: TObject);
    begin
      case WhichColor of
        1:  begin
              Caption:='红灯 '+inttostr(FLight[1].Duration)+' 秒 '+inttostr(Duration[1]);
              FLight[1].Brush.Color:=clRed;
              FLight[4].Brush.Color:=clGreen;
              if Duration[1] >=FLight[1].Duration then
                begin
                  Duration[1]:=0;
                  FLight[1].Brush.Color:=clYellow;
                  FLight[4].Brush.Color:=clYellow;
                  WhichColor:=2;
                  State:=0;
                end
              else
                Duration[1]:=Duration[1]+1;
            end;
        2:  begin
              Caption:='黄灯 '+inttostr(FLight[2].Duration)+' 秒 '+inttostr(Duration[2]);
              if Duration[2] >=FLight[2].Duration then
                  begin
                  Duration[2] :=0;
                  FLight[1].Brush.Color:=clBtnShadow;
                  case State of
                  0:  begin
                        FLight[1].Brush.Color:=clGreen;
                        WhichColor:=3;
                        FLight[4].Brush.Color:=clRed;
                      end;
                  1:  begin
                        FLight[1].Brush.Color:=clRed;
                        WhichColor:=1;
                        FLight[4].Brush.Color:=clGreen;
                      end;
                  end;
                  end
              else
                  Duration[2]:=Duration[2]+1;
            end;
        3:  begin
              Caption:='绿灯 '+inttostr(FLight[3].Duration)+' 秒 '+inttostr(Duration[3]);
              if Duration[3]>=FLight[3].Duration then
                begin
                  Duration[3]:=0;
                  FLight[1].Brush.Color:=clYellow;
                  FLight[4].Brush.Color:=clYellow;
                  WhichColor:=2;
                  State:=1;
                end
              else
                Duration[3]:=Duration[3]+1;
            end;
      end;
      /////////////////
      
    end;end.
    ------------------
    unit LightU;interface
    uses ExtCtrls;
    Type
      TLight=class(TShape)
      private
        FDuration: integer;
        procedure SetDuration(const Value: integer);
      public
        property Duration:integer read FDuration write SetDuration default 20;  end;implementationuses Controls;{ TLight }procedure TLight.SetDuration(const Value: integer);
    begin
      FDuration := Value;
    end;
    end.
    -----------
    界面上放2个timer一叫timer1,另一个TimerShow,还有一个button1
      

  4.   

    最简单的办法是 在Timer1的ontime事件中 让充当两个灯体对象的颜色互相调换就是了。
      

  5.   

    有个Led 控件,在http://www.vclxx.org/ 上找找