C++builder 定义和构建延时计时器

C++builder 定义和构建延时计时器,c++builder,C++builder,在Delphi中,我了解如何构建lapsing计时器。但我不确定如何为C++Builder编写代码。我找不到任何例子 在Delphi中,我在下面编写了这段代码,它是某个地方的源代码副本:- .... type TFrame2 = class(TFrame) StatusBar1: TStatusBar; Timer1: TTimer; constructor TFrame2.Create(TheOwner: TComponent); begin inherited Create(T

在Delphi中,我了解如何构建lapsing计时器。但我不确定如何为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;
}
#包括
....
类TFrame2:公共TFrame
{
__出版:
TStatusBar*StatusBar1;
t定时器*定时器1;
...
void uu fastcall Timer1Timer(TObject*发送方);
...
私人:
TStopwatch SW;
...
公众:
__fastcall TFrame2(t组件*所有者);
};
__fastcall TFrame2::TFrame2(t组件*所有者)
:t框架(所有者)
{
SW=TStopwatch::StartNew();
Timer1->Enabled=true;
}
void _fastcall TFrame2::Timer1Timer(TObject*Sender)//此事件每秒发生一次。
{
//Timer1->Enabled=false;
SW.Stop();
TTimeSpan TS=经过的西南方向;
StatusBar1->Panels->Items[1]->Text=String(TS.Minutes)+“Minutes”,“String(TS.Seconds)+“Seconds.”;
SW.Start();
//Timer1->Enabled=true;
}

Delphi和C++ Builder共享VCL,C++ Builder有很多具体的更改,使之与Delphi兼容,因此从Delphi代码中进行C++翻译通常非常简单,至少与你所提供的例子一样简单。你试过自己做这个吗?如果你给我们展示C++中的代码,并要求你在翻译中发现的具体问题,而不是要求翻译整个事情,那就更好了。可能的解决方案是额外的。再次感谢