|
Windows Topics: The Tick Counter |
|
|
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's 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
|
|
- To start a new application, on the main menu, click File -> New
VCL Forms Application - C++Builder
- Design the form as follows:
|
Control |
Alignment |
Caption |
Kind |
Name |
TGroupBox |
|
|
Elapsed Time |
|
|
TLabel |
|
|
Time this computer... |
|
lblComputerTime |
TEdit |
|
taRightJustify |
|
|
edtComputerTime |
TLabel |
|
|
millisconds |
|
|
TLabel |
|
|
Time since this application... |
|
lblApplicationTime |
TEdit |
|
taRightJustify |
|
|
edtApplicationTime |
TLabel |
|
|
millisconds |
|
|
TTimer |
|
|
|
|
|
TBitBtn |
|
|
|
bkClose |
|
|
- Under the Code Editor, click the header tab to access the file
- In the private section of the form, declare an unsigned integer as
follows:
private:
unsigned int TimeTheComputerStarted; // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
- Press F12 to display the form
- Double-click an unoccupied area of the form
- Implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TimeTheComputerStarted = GetTickCount();
}
//---------------------------------------------------------------------------
- Press F12 to return to the form
- On the form, double-click the Timer1 icon to access its OnTimer
event
- Implement the source file as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
unsigned long CurrentTickValue = GetTickCount();
unsigned int Difference = CurrentTickValue - TimeTheComputerStarted;
edtComputerTime->Text = UnicodeString(CurrentTickValue);
edtApplicationTime->Text = UnicodeString(Difference);
}
//---------------------------------------------------------------------------
- Press F9 to test the application
- After testing the application, close the form and return to your
programming environment
- To make the values easier to read, change the form as follows:
Delete both Edit boxes and both milliseconds labele:
- Press F12 to access the code
- Change the code of the OnTimer event as follows:
//---------------------------------------------------------------------------
void __fastcall TForm1::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;
UnicodeString ComputerTime, ApplicationTime;
ComputerTime = UnicodeString(ComputerHours) + L" hours, " +
UnicodeString(ComputerMinutes) + L" minutes, and " +
UnicodeString(ComputerSeconds) + L" seconds";
ApplicationTime = UnicodeString(ApplicationHours) + L" hours, " +
UnicodeString(ApplicationMinutes) + L" minutes, and " +
UnicodeString(ApplicationSeconds) + L" seconds";
lblComputerTime->Caption = L"This computer has been ON for "
+ ComputerTime;
lblApplicationTime->Caption = L"This application has been active for "
+ ApplicationTime;
}
//---------------------------------------------------------------------------
- Press F9 to test the application
- After testing the application, close the form and return to your
programming environment
|
|