GDI+ Sample: Chase Circle

 

Introduction

This is another use of circular geometry for an attempt of a screen saver. In this case, we draw 3 circles around a central point. The circles move around that central point continuously using a timer.

 

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++ .Net and create a new Windows  named ChaseCircle
  2. Change the properties of the form as follows:
    BackColor: Black
    FormBorderStyle: None
    WindowState: Maximized
  3. Double-click the middle of the form to generate its Load event
  4. Return to the form and, in the Events section of the Properties window, generate a KeyPress event for it
  5. Return to the form and add a Timer control to it
  6. Set the timer's Enabled to True and its Interval to 50
  7. Double-click the timer and change the file as follows
     
    #pragma once
    
    
    namespace ScreenSaver5
    {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    
    	/// <summary> 
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the 
    	///          'Resource File Name' property for the managed resource compiler tool 
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public __gc class Form1 : public System::Windows::Forms::Form
    	{	
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    		}
      
    	protected:
    		void Dispose(Boolean disposing)
    		{
    			if (disposing && components)
    			{
    				components->Dispose();
    			}
    			__super::Dispose(disposing);
    		}
    	private: System::Windows::Forms::Timer *  timer1;
    
    	private: System::ComponentModel::IContainer *  components;
    			 int radius;
    
    
    	private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    
    
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    		{
    			this->components = new System::ComponentModel::Container();
    			this->timer1 = new System::Windows::Forms::Timer(this->components);
    			// 
    			// timer1
    			// 
    			this->timer1->Enabled = true;
    			this->timer1->Interval = 40;
    			this->timer1->Tick += new System::EventHandler(this, timer1_Tick);
    			// 
    			// Form1
    			// 
    			this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
    			this->BackColor = System::Drawing::Color::Black;
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
    			this->Name = S"Form1";
    			this->Text = S"Form1";
    			this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
    			this->KeyPress += new System::Windows::Forms::KeyPressEventHandler(this, Form1_KeyPress);
    
    		}	
    
    
    	private: System::Void Form1_KeyPress(System::Object *  sender, System::Windows::Forms::KeyPressEventArgs *  e)
    			 {
    				 if( e->KeyChar == Keys::Escape )
    					 Close();
    			 }
    
    	private: System::Void timer1_Tick(System::Object *  sender, System::EventArgs *  e)
    			 {
    				 Graphics *graph = Graphics::FromHwnd(this->Handle);
    
    				 graph->TranslateTransform(static_cast<float>(this->ClientSize.Width / 2), static_cast<float>(this->ClientSize.Height / 2));
    				 
    				 int radius = 250;
    				 static int angle1 =  0;
    				 static int angle2 = 15;
    				 static int angle3 = 30;
    				 static int angle4 = 45;
    				 static int angle5 = 60;
    				 
    				 double PI2 = 2 * Math::PI;
    
    				 int X1 = static_cast<int>(radius * Math::Cos(angle1/PI2));
    				 int Y1 = static_cast<int>(radius * Math::Sin(angle1/PI2));
    				 int X2 = static_cast<int>(radius * Math::Cos(angle2/PI2));
    				 int Y2 = static_cast<int>(radius * Math::Sin(angle2/PI2));
    				 int X3 = static_cast<int>(radius * Math::Cos(angle3/PI2));
    				 int Y3 = static_cast<int>(radius * Math::Sin(angle3/PI2));
    				 int X4 = static_cast<int>(radius * Math::Cos(angle4/PI2));
    				 int Y4 = static_cast<int>(radius * Math::Sin(angle4/PI2));
    				 int X5 = static_cast<int>(radius * Math::Cos(angle5/PI2));
    				 int Y5 = static_cast<int>(radius * Math::Sin(angle5/PI2));
    
    				 graph->Clear(Color::Black);
    
    				 graph->DrawEllipse(Pens::Fuchsia,    -50,    -50, 100, 100);
    				 graph->DrawEllipse(Pens::Aqua,    X1-200, Y1-200, 400, 400);
    				 graph->DrawEllipse(Pens::Yellow,  X2-200, Y2-200, 400, 400);
    				 graph->DrawEllipse(Pens::Blue,    X3-200, Y3-200, 400, 400);
    				 graph->DrawEllipse(Pens::White,   X4-200, Y4-200, 400, 400);
    				 graph->DrawEllipse(Pens::Red,     X5-200, Y5-200, 400, 400);
    
    				 angle1++;
    				 angle2++;
    				 angle3++;
    				 angle4++;
    				 angle5++;
    			 }
    };
    }
  8. Execute the application
 

Home Copyright © 2004-2014 FunctionX, Inc.