Home

Drawing Shapes: Regular Shapes

 

Rectangles and Squares

A rectangle is a geometric figure made of four sides that compose four right angles. To draw a rectangle, you can either specify the Rectangle value that encloses it, or you can define its location and its dimensions. To draw a rectangle that is enclosed in a Rectangle value, you can use the following version of the Graphics::DrawRectangle() method:

public:
    void DrawRectangle(Pen ^ pen,
		       Rectangle rect);

Remember that such a rectangle object can be illustrated as follows:

After defining a Rectangle variable, you can pass it to the method. Here is an example:

System::Void Form1_Paint(System::Object ^  sender, 
		System::Windows::Forms::PaintEventArgs ^  e)
{
    Pen ^ penCurrent = gcnew Pen(Color::Red);
    Rectangle Rect(20, 20, 248, 162);

    e->Graphics->DrawRectangle(penCurrent, Rect);
}

Remember that you can also define a Pen and/or a Rectangle objects in the parentheses of the method:

System::Void Form1_Paint(System::Object ^  sender, 
		System::Windows::Forms::PaintEventArgs ^  e)
{
    e->Graphics->DrawRectangle(gcnew Pen(Color::Red), 
			       Rectangle(20, 20, 248, 162));
}

This would produce:

A Rectangle Drawn From a Rectangle Value

It is (very) important to remember that the third argument of the Rectangle represents its width (and not its right) value and the fourth argument represents its height (and not its bottom) value. This is a confusing fact for those who have programmed in GDI: GDI+ defines a Rectangle differently than GDI. In fact, to determine the location and dimensions of a rectangle to draw, the Graphics class provides the following versions of the DrawRectangle() method:

public:
   void DrawRectangle(Pen ^ pen,
		      int x,
		      int y,
		      int width,
		      int height);
   void DrawRectangle(Pen ^ pen,
		      float x,
		      float y,
		      float width,
		      float height);

This time, the rectangle is represented by its location with a point at (x, y) and its dimensions with the width and height argument. This can be illustrated in a Windows coordinate system as follows:

Rectangle

Based on this, the earlier rectangle can also be drawn with the following:

System::Void Form1_Paint(System::Object ^  sender, 
		System::Windows::Forms::PaintEventArgs ^  e)
{
    e->Graphics->DrawRectangle(gcnew Pen(Color::Red), 20, 20, 248, 162);
}

A square is a rectangle whose four sides are equal.

Practical LearningPractical Learning: Drawing a Rectangle

  1. Start a new Windows Forms Application named WeeklySales1
  2. Design the form as follows:
     
    Control Name Text
    Label Label   Monday
    Label Label   Tuesday
    Label Label   Wednesday
    Label Label   Thursday
    Label Label   Friday
    TextBox TextBox txtMonday 12000
    TextBox TextBox txtTuesday 11000
    TextBox TextBox txtWednesday 8500
    TextBox TextBox txtThursday 16800
    TextBox TextBox txtFriday 17500
    Button Button Generate btnGenerate
  3. Right-click the form and click View Code
  4. Declare two variables as follows:
     
    private:
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container * components;
    		Graphics ^ graphDrawingArea;
    		Bitmap   ^ bmpDrawingArea;
  5. Return to the form and double-click an unoccupied area of its body
  6. Implement the event as follows:
     
    System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e)
    {
        bmpDrawingArea   = gcnew Bitmap(Width, Height);
        graphDrawingArea = Graphics::FromImage(bmpDrawingArea);
    }
  7. Return to the form and click an empty area on it. In the Properties window, click the Events button Events
  8. Double-click the Paint field and implement its event as follows:
     
    System::Void Form1_Paint(System::Object^  sender, 
    			 System::Windows::Forms::PaintEventArgs^  e)
    {
        e->Graphics->DrawImage(bmpDrawingArea, 0, 0);
    }
  9. Return to the form and double-click the Generate button
  10. Implement the event as follows:
     
    System::Void btnGenerate_Click(System::Object^  sender, 
    			       System::EventArgs^  e)
    {
        int monday, tuesday, wednesday, thursday, friday;
    
        try {
            monday = int::Parse(txtMonday->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid sales on Monday");
    	 txtMonday->Text = L"0";
        }
    			 
        try {
            tuesday = int::Parse(txtTuesday->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid sales on Tuesday");
    	 txtTuesday->Text = L"0";
        }
    			 
        try {
    	 wednesday = int::Parse(txtWednesday->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid sales on Wednesday");
    	 txtWednesday->Text = L"0";
        }
    			 
        try {
    	 thursday = int::Parse(txtThursday->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid sales on Thursday");
    	 txtThursday->Text = L"0";
        }
    			 
        try {
    	 friday = int::Parse(txtFriday->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid sales on Friday");
    	 txtFriday->Text = L"0";
        }
    				 
        graphDrawingArea->Clear(this->BackColor);
    
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Red),       
    				    this->txtMonday->Left+10,    
    				    300-monday,    
    				    40, monday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Blue),      
    				    this->txtTuesday->Left+10,   
    				    300-tuesday,   
    				    40, tuesday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Fuchsia),   
    				    this->txtWednesday->Left+5, 
    				    300-wednesday, 
    				    40, wednesday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Brown),
    				    this->txtThursday->Left+5,
    				    300-thursday,
    				    40, thursday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Turquoise),
    				    this->txtFriday->Left+5,
    				    300-friday,    40, friday);
    
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Black),
    				    10, 300, Width - 30, 1);
        Invalidate();
    }
  11. Execute the application and test the form
     
     
  12. After using it, close the form
 

Home Copyright © 2007-2013, FunctionX Next