FunctionX Practical Learning Logo

Tick Counter

 

Introduction

The Win32 API provides a special function used to count a specific number of lapses that have occurred since you started your computer. This information or counter is available through the GetTickCount() function. Its syntax is:

DWORD GetTickCount(VOID);

This function takes no argument. If it succeeds in performing its operation, which it usually does, it provides the number of milliseconds that have elapsed since you started your computer. Just like the VCL Timer control, what you do with the result of this function is up to you and it can be used in various circumstances. For example, computer games and simulations make great use of this function.

After retrieving the value that this function provides, you can display it in a text-based control. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::btnElapsedClick(TObject *Sender)
{
	unsigned long Elapsed = GetTickCount();
	edtElapsed->Text = IntToStr(Elapsed);
}
//---------------------------------------------------------------------------

Practical Learning: Counting the Computer's Ticks

  1. Start Borland C++ Builder and create a new application with its default form
  2. Save it in a new folder named CompTicks1
  3. Save the unit as Exercise and save the project CompTicks
     
  4. Design the form as follows: set its BorderStyle to bsDialog. Change its name to frmMain and set its Caption to Counting Computer Ticks
  5. Add a GroupBox control and set its Caption to Elapsed Time
  6. Add a Timer control from the System tab of the Component Palette. Set its Interval to 20
  7. Add an Edit control and change its Name to edtComputerTime
  8. Add another Edit control and change its Name to edtApplicationTime
  9. Press F12 to access the Code Editor. In the private section of the form, declare an unsigned integer as follows:
     
    private:
        unsigned int TimeTheComputerStarted; // User declarations
    public: // User declarations
        __fastcall TfrmMain(TComponent* Owner);
    };
    //---------------------------------------------------------------------------
  10. 10. On the form, double-click the Timer1 icon to access its OnTimer event and implement the source file as follows:
     
    //---------------------------------------------------------------------------
    
    #include <vcl.h>
    #pragma hdrstop
    
    #include "Unit1.h"
    //---------------------------------------------------------------------------
    #pragma package(smart_init)
    #pragma resource "*.dfm"
    TfrmMain *frmMain;
    //---------------------------------------------------------------------------
    __fastcall TfrmMain::TfrmMain(TComponent* Owner)
    : TForm(Owner)
    {
    	TimeTheComputerStarted = GetTickCount();
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::Timer1Timer(TObject *Sender)
    {
    	unsigned long CurrentTickValue = GetTickCount();
    	unsigned int Difference = CurrentTickValue - TimeTheComputerStarted;
    
    	edtComputerTime->Text = IntToStr(CurrentTickValue);
    	edtApplicationTime->Text = IntToStr(Difference);
    }
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::BitBtn1Click(TObject *Sender)
    {
    	Close();
    }
    //---------------------------------------------------------------------------
  11. Press F9 to test the application
     
  12. After testing the application, close it and return to Bcb
  13. To make the values easier to read, change the form as follows. Delete both Edit boxes and replace them with Label controls named lblComputerTime and lblApplicationTime respectively:
     
  14. Change the code of the OnTimer event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TfrmMain::Timer1Timer(TObject *Sender)
    {
    	unsigned long CurrentTickValue = GetTickCount();
    	unsigned int Difference = CurrentTickValue - TimeTheComputerStarted;
    
    	unsigned int ComputerHours, ComputerMinutes, ComputerSeconds;
    	unsigned int ApplicationHours,
    			ApplicationMinutes, ApplicationSeconds;
    
    	ComputerHours = (CurrentTickValue / (3600 * 999)) % 24;
    	ComputerMinutes = (CurrentTickValue / (60 * 999)) % 60;
    	ComputerSeconds = (CurrentTickValue / 999) % 60;
    	ApplicationHours = (Difference / (3600 * 999)) % 24;
    	ApplicationMinutes = (Difference / (60 * 999)) % 60;
    	ApplicationSeconds = (Difference / 999) % 60;
    
    	AnsiString ComputerTime, ApplicationTime;
    
    	ComputerTime = IntToStr(ComputerHours) + " hours, " +
    	IntToStr(ComputerMinutes) + " minutes " +
    	IntToStr(ComputerSeconds) + " seconds";
    	ApplicationTime = IntToStr(ApplicationHours) + " hours " +
    	IntToStr(ApplicationMinutes) + " minutes " +
    	IntToStr(ApplicationSeconds) + " seconds";
    
    	lblComputerTime->Caption = ComputerTime;
    	lblApplicationTime->Caption = ApplicationTime;
    }
    //---------------------------------------------------------------------------
  15. Test the application
     
  16. After testing the application, close it
  17. Save All
 

Home Copyright 漏 2004-2007 FunctionX, Inc. VCL

两个鬼故事白岩松演讲稿宝宝起名网是真的吗综敛财人生亲爱的热爱的网盘小谎大事宝宝起名字哪个网站丝印公司起名凤舞天骄私服毫克与克饺子店起名字好听巨魔与精灵移动宽带测速起个韩国服饰店名字河南省教师教育网木命怎么起名大全鼠年起名字叫什么好呢龚姓起名德字三个字起名公司侯璎珏羊棒骨火锅店起名玛拉顿地图狼和小羊的故事名称酒店起名起名鬼才什么牌子起名商标起名大全起公司名称大全免费网站iris演员表带宝起名字禁区沉沦注册公司如何起名少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化