How to make a Digital clock in delphi7?

前端 未结 3 626
终归单人心
终归单人心 2021-02-14 23:56

I am pretty new to delphi , and would like to start with something easy . Could someone please show me an example how to make a Digital clock that will transfer the \"time\" ( h

3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-02-15 00:11

    Download my open source NLDDigiLabel component from here, place three of them on the form, place two common labels in between as time separators, and set the background color of the form. In this example, I did all this on a frame for convenience:

    unit Unit2;
    
    interface
    
    uses
      Windows, SysUtils, Classes, Graphics, Controls, Forms, StdCtrls, ExtCtrls,
      NLDDigiLabel;
    
    type
      TDigitalClock = class(TFrame)
        HoursLabel: TNLDDigiLabel;
        MinsLabel: TNLDDigiLabel;
        SecsLabel: TNLDDigiLabel;
        TimeSeparator1: TLabel;
        TimeSeparator2: TLabel;
        Timer: TTimer;
        procedure TimerTimer(Sender: TObject);
      private
        FTime: TTime;
        procedure SetTime(Value: TTime);
      public
        property Time: TTime read FTime write SetTime;
      end;
    
    implementation
    
    {$R *.dfm}
    
    { TDigitalClock }
    
    procedure TDigitalClock.SetTime(Value: TTime);
    var
      Hours: Word;
      Mins: Word;
      Secs: Word;
      MSecs: Word;
    begin
      if FTime <> Value then
      begin
        FTime := Value;
        DecodeTime(FTime, Hours, Mins, Secs, MSecs);
        HoursLabel.Value := Hours;
        MinsLabel.Value := Mins;
        SecsLabel.Value := Secs;
      end;
    end;
    
    procedure TDigitalClock.TimerTimer(Sender: TObject);
    begin
      SetTime(FTime + 1/SecsPerDay);
    end;
    
    end.
    

    Now, drop such a frame on your form, et voilá:

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      DigitalClock1.Time := Time;
    end;
    

    DigitalClock.png

提交回复
热议问题