In Delphi, I understand how to build lapsing timer. But I am not sure about how to write code for C++Builder. I could not find any example.
In Delphi I wrote this code below, a copy from the source somewhere:-
....
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;
...
Please kindly how to do same in C++. Thanks
Try this:
Alternatively, you can simplify
Timer1Timer()
to this:Personally, I would not use the system clock at all, in case the user changes the clock, or it auto-rolls for DST, while your timer is running. I would use CPU ticks instead, either manually:
Or via
TStopWatch
: