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 |
|
- Start Borland C++ Builder and create a new application with its
default form
- Save it in a new folder named CompTicks1
- Save the unit as Exercise and save the project CompTicks
- Design the form as follows: set its BorderStyle to bsDialog. Change
its name to frmMain and set its Caption to Counting Computer Ticks
- Add a GroupBox control and set its Caption to Elapsed Time
- Add a Timer control from the System tab of the Component Palette.
Set its Interval to 20
- Add an Edit control and change its Name to edtComputerTime
- Add another Edit control and change its Name to edtApplicationTime
- 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. 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();
}
//---------------------------------------------------------------------------
|
- Press F9 to test the application
- After testing the application, close it and return to Bcb
- 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:
- 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;
}
//---------------------------------------------------------------------------
|
- Test the application
- After testing the application, close it
- Save All
|
|
|