Characteristics of a Timer |
|
A timer is an object used to count lapses of time and send a message when it has finished counting.
Each count is called a tick. When a tick occurs, the control fires a Tick
event. This Tick event is of type EventArgs, meaning that it
doesn't provide more information than to let you know that a lapse has occurred.
The amount of time allocated for counting is called an
interval and it is represented by the Interval property. The Interval is
a very important characteristic of the Timer control because it measures and controls the total time needed to perform a complete count. The
Interval is measured in milliseconds. Like any counter, the lower the value, the faster the count will finish, and the higher the value, the longer the count (if you ask one kid to count from 1 to 10 and you ask another to count from 1 to 20
at the same time, if you ask them to shout when they finish, the first kid would finish first and would shout first). The amount of interval you specify will depend on what you are trying to do.
In order for a timer to count, you must tell it when it should start counting. In some applications, you may want the control to work full-time while in some other applications, you may want the control to work only in response to an intermediate event. The ability to stop and start a Timer control
can be set using the Enabled Boolean property. When, or as soon as, this property is set to true, the control starts counting.
You can also make it start by calling the Timer::Start() method. Its
syntax is:
public: void Start();
If, when, or as soon as, the Enabled property is set to false, the control stops and resets its counter to 0.
You can also stop the timer by calling the Timer::Stop() method. Its
syntax is:
public: void Stop();
Practical
Learning: Using Timer Controls |
|
- On the Toolbox, click the Timer control and click the form
- In the Properties window, set the Enabled property to True
and set the Interval to 20
- Under the form, double-click the timer
- Create the following two methods and implement the timer's event as follows:
void DrawCentralCircle(int CenterX, int CenterY, int Radius)
{
int start = CenterX - Radius;
int end = CenterY - Radius;
int diam = Radius * 2;
graphDrawingArea->DrawEllipse(new Pen(Color::Blue), start, end, diam, diam);
}
void DrawCornerCircle(int CenterX, int CenterY, int Radius)
{
int start = CenterX - Radius;
int end = CenterY - Radius;
int diam = Radius * 2;
graphDrawingArea->DrawEllipse(new Pen(Color::Red), start, end, diam, diam);
}
System::Void timer1_Tick(System::Object * sender, System::EventArgs * e)
{
Graphics *graph = Graphics::FromHwnd(this->Handle);
int centerX = ClientRectangle.Width / 2;
int centerY = ClientRectangle.Height / 2;
static int mainRadius = 10;
static int smallRadius = 5;
static bool isMax;
static bool smallRadiusMax;
if( isMax == true )
mainRadius--;
else
mainRadius++;
if( mainRadius > (ClientRectangle.Height / 2) )
isMax = true;
if( mainRadius < 10 )
isMax = false;
if( smallRadiusMax == true )
smallRadius--;
else
smallRadius++;
if( smallRadius > 240 )
smallRadiusMax = true;
if( smallRadius < 5 )
smallRadiusMax = false;
graphDrawingArea->FillRectangle(new SolidBrush(Color::Black), 0, 0, Width, Height);
// Central
DrawCentralCircle(centerX, centerY, mainRadius);
// Top-Left
DrawCornerCircle(centerX/2, centerY/2, smallRadius);
// Top-Right
DrawCornerCircle(centerX + (centerX/2), centerY/2, smallRadius);
// Bottom-Left
DrawCornerCircle(centerX/2, centerY + (centerY/2), smallRadius);
// BottomRight
DrawCornerCircle(centerX + (centerX/2), centerY + (centerY/2), smallRadius);
graph->DrawImage(bmpDrawingArea, 0, 0);
}
|
- Execute the application
|
|