定义和构建流逝的计时器

Define and build lapsed timer

在 Delphi 中,我了解了如何构建流逝计时器。但是我不确定如何为 C++Builder 编写代码。我找不到任何示例。

在 Delphi 我在下面写了这段代码,从某处的源代码中复制了一份:-

....
type
  TFrame2 = class(TFrame)
StatusBar1: TStatusBar;
Timer1: TTimer;

constructor TFrame2.Create(TheOwner: TComponent);
 begin
   inherited Create(TheOwner);
   StartTime := Now;
   Timer1.Enabled := True;
 end;

destructor TFrame2.Destroy;
 begin
   inherited Destroy
 end;

procedure TFrame2.Timer1Timer(Sender: TObject);//This event occurs every second.
Var
Hour, Min, Sec, MSec : Word;
Diff : TTime;
 begin
Timer1.Enabled := False;
Diff := Now - StartTime;
DecodeTime(Diff, Hour, Min, Sec, MSec);
StatusBar1.Panels.Items[1].Text := IntToStr(Min)+' Minutes, '+IntToStr(Sec)+' Seconds.';
Timer1.Enabled := True;
end;

...

请问如何在 C++ 中做同样的事情。 谢谢

试试这个:

....
class TFrame2 : public TFrame
{
__published:
    TStatusBar *StatusBar1;
    TTimer *Timer1;
    ...
    void __fastcall Timer1Timer(TObject *Sender);
    ...
private:
    TDateTime StartTime;
    ...
public:
    __fastcall TFrame2(TComponent *TheOwner);
};

__fastcall TFrame2::TFrame2(TComponent *TheOwner)
    : TFrame(TheOwner)
{
    StartTime = Now();
    Timer1->Enabled = true;
}

void __fastcall TFrame2::Timer1Timer(TObject *Sender) //This event occurs every second.
{
    Timer1->Enabled = false;

    TDateTime Diff = Now() - StartTime;
    Word Hour, Min, Sec, MSec;
    DecodeTime(Diff, Hour, Min, Sec, MSec);
    StatusBar1->Panels->Items[1]->Text = String(Min)+" Minutes, "+String(Sec)+" Seconds.";

    Timer1->Enabled = true;
}

...

或者,您可以将 Timer1Timer() 简化为:

void __fastcall TFrame2::Timer1Timer(TObject *Sender) //This event occurs every second.
{
    // this is not overhead-intense code, so
    // stopping and re-starting the timer
    // is wasting unnecessary processing time...

    //Timer1->Enabled = true;

    TDateTime Diff = Now() - StartTime;
    StatusBar1->Panels->Items[1]->Text = Diff.FormatString("n' Minutes, 's' Seconds.'");

    //Timer1->Enabled = true;
}

就我个人而言,我根本不会使用系统时钟,以防万一用户更改时钟,或者它自动滚动为 DST,而您的计时器是 运行。我会使用 CPU 刻度代替,手动:

....
class TFrame2 : public TFrame
{
__published:
    TStatusBar *StatusBar1;
    TTimer *Timer1;
    ...
    void __fastcall Timer1Timer(TObject *Sender);
    ...
private:
    DWORD StartTime;
    ...
public:
    __fastcall TFrame2(TComponent *TheOwner);
};

__fastcall TFrame2::TFrame2(TComponent *TheOwner)
    : TFrame(TheOwner)
{
    StartTime = GetTickCount();
    Timer1->Enabled = true;
}

void __fastcall TFrame2::Timer1Timer(TObject *Sender) //This event occurs every second.
{
    //Timer1->Enabled = false;

    DWORD Diff = GetTickCount() - StartTime;
    DWORD Mins = Diff / 60000; Diff %= 60000;
    DWORD Secs = Diff / 1000;

    StatusBar1->Panels->Items[1]->Text = String(Mins)+" Minutes, "+String(Secs)+" Seconds.";

    //Timer1->Enabled = true;
}

...

或通过TStopWatch:

#include <System.Diagnostics.hpp>

....
class TFrame2 : public TFrame
{
__published:
    TStatusBar *StatusBar1;
    TTimer *Timer1;
    ...
    void __fastcall Timer1Timer(TObject *Sender);
    ...
private:
    TStopwatch SW;
    ...
public:
    __fastcall TFrame2(TComponent *TheOwner);
};

__fastcall TFrame2::TFrame2(TComponent *TheOwner)
    : TFrame(TheOwner)
{
    SW = TStopwatch::StartNew();
    Timer1->Enabled = true;
}

void __fastcall TFrame2::Timer1Timer(TObject *Sender) //This event occurs every second.
{
    //Timer1->Enabled = false;
    SW.Stop();

    TTimeSpan TS = SW.Elapsed;
    StatusBar1->Panels->Items[1]->Text = String(TS.Minutes)+" Minutes, "+String(TS.Seconds)+" Seconds.";

    SW.Start();
    //Timer1->Enabled = true;
}