c++builder - Define and build lapsed timer -


in delphi, understand how build lapsing timer. not sure how write code c++builder. not find example.

in delphi wrote code below, copy 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 := - 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 same in c++. thanks

try this:

.... 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; }  ... 

alternatively, can simplify timer1timer() this:

void __fastcall tframe2::timer1timer(tobject *sender) //this event occurs every second. {     // not overhead-intense code,     // stopping , re-starting timer     // 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; } 

personally, not use system clock @ all, in case user changes clock, or auto-rolls dst, while timer running. use cpu ticks instead, either manually:

.... 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; }  ... 

or via 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; } 

Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -