Home

.NET Controls: The Timer

 

The Timer

 

Introduction

A timer is a non-spatial object that uses recurring lapses of time in a computer or in your application. To work, every lapse of period, the control sends a message to the operating system. As opposed to the time that controls your computer, a timer is partly but greatly under your control. Users do not see nor do they use a timer as a control. As a programmer, you decide if, why, when, and how to use this control.

To support timers, the .NET Framework provides the Timer control from the Toolbox and it is implemented through the Timer class. To add it to your application at design time, on the Toolbox, click Timer and click the form.

Practical LearningPractical Learning: Introducing Timer Controls

  1. Start a new Windows Forms Application named ScreenSaver1
  2. Change the following properties of the form:
    BackColor: Black
    FormBorderStyle: None
    WindowState: Maximized
  3. In the Properties window, click the Events button  and double-click MouseMove
  4. Implement the event as follows:
     
    #pragma once
    
    
    namespace ScreenSaver1
    {
    	
    	. . . No Change
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    		static int MoveCounter = 0;
    		Graphics *graphDrawingArea;
    		Bitmap *bmpDrawingArea;
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			. . . No Change
    		}	
    	private: System::Void Form1_MouseMove(System::Object *  sender, System::Windows::Forms::MouseEventArgs *  e)
    		{
    			if( MoveCounter == 20 )
    				Close();
    			MoveCounter++;
    		 }
    
    	};
    }
  5. Display the form
    Double-click the middle of the form to generate its Load event and implement it as follows:
     
    System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    {
    	 bmpDrawingArea   = new Bitmap(Width, Height);
    	 graphDrawingArea = Graphics::FromImage(bmpDrawingArea);
    
    	 Cursor::Hide();
    }
  6. Display the form again and, in the Events section of the Properties window, double-click KeyDown
  7. Implement its event as follows:
     
    System::Void Form1_KeyDown(System::Object *  sender, System::Windows::Forms::KeyEventArgs *  e)
    {
    	 // If the user presses Esc, stop the screen saver
    	 if( e->KeyCode == Keys::Escape )
    		Close();
    }
  8. Execute the application to test it

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 LearningPractical Learning: Using Timer Controls

  1. On the Toolbox, click the Timer control and click the form
  2. In the Properties window, set the Enabled property to True and set the Interval to 20
  3. Under the form, double-click the timer
  4. 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);
    }		
  5. Execute the application
 

Home Copyright © 2004-2010 FunctionX, Inc.