GDI+ Examples: Circular

 

Introduction

In this example that simulates a screen saver, we draw one circle in the center of the screen. Then we draw four circles around the central one. We use a timer to monitor what is going on. As the timer ticks, the radius of the central circle changes. Also, the radii of the other four circles change. When the radius a circle reaches a maximum, the radius decreases. Then it reaches a minimum, it starts increasing again.

Practical Learning: Starting the Exercise

  1. Start Microsoft Visual C++ .Net and create a new Windows  named Circular
  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 KeyDown 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 20
  7. Double-click the timer and change the file as follows
     
    #pragma once
    
    
    namespace Circular
    {
    	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;
    			 Graphics *graphDrawingArea;
    			 Bitmap   *bmpDrawingArea;
    
    	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 = 20;
    			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->MaximizeBox = false;
    			this->Name = S"Form1";
    			this->Text = S"Form1";
    this->KeyDown += new System::Windows::Forms::KeyEventHandler(this, Form1_KeyDown);
    			this->Load += new System::EventHandler(this, Form1_Load);
    
    		}	
    	private: System::Void Form1_Load(System::Object *  sender, System::EventArgs *  e)
    		 {
    			 bmpDrawingArea   = new Bitmap(Width, Height);
    			graphDrawingArea = Graphics::FromImage(bmpDrawingArea);
    
    			 Cursor::Hide();
    		 }
    
    private: System::Void Form1_KeyDown(System::Object *  sender, 
    		System::Windows::Forms::KeyEventArgs *  e)
    			 {
    				 if( e->KeyCode == Keys::Escape )
    					 Close();
    			 }
    
    			 
    
    	 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);
    	 }
    
    	private: 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);
    	 }
    
    	};
    }
  8. Execute the application
 
 

Home Copyright 2004-2006 FunctionX, Inc.