Home

Drawing Shapes: Drawing Rectangles

 

A Series of Rectangles

The DrawRectangle() method is used to draw one rectangle. If you plan to draw many rectangles, you can proceed in one step by using the Graphics::DrawRectangles() method. It comes in two versions whose syntaxes are:

public:
    void DrawRectangles(Pen^ pen,
			array<Rectangle>^ rects);
    void DrawRectangles(Pen^ pen,
			array<RectangleF>^ rects);

This method requires an array of Rectangle or RectangleF values. When executed, it draws individual rectangles using each member of the array as its own rectangle. Here is an example:

System::Void Form1_Paint(System::Object^  sender,
			 System::Windows::Forms::PaintEventArgs^  e)
{
    Pen ^ penCurrent = gcnew Pen(Color::Red);
    array<Rectangle> ^ Rect = { Rectangle(20,  20, 120, 20),
	                        Rectangle(20,  50, 120, 30),
		                Rectangle(20,  90, 120, 40),
		                Rectangle(20, 140, 120, 60) };

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

This would produce:

Rectangles

Practical LearningPractical Learning: Drawing a Series of Rectangles

  1. Start a new Windows Forms Application named YearlySales1
  2. Design the form as follows:
     
     
    Control Name Text
    GroupBox GroupBox   Current Year's Sales
    Label Label   1st Qtr
    Label Label   2nd Qtr
    Label Label   3rd Qtr
    Label Label   4th Qtr
    TextBox TextBox txtCurrentQtr1 1200
    TextBox TextBox txtCurrentQtr2 14500
    TextBox TextBox txtCurrentQtr3 8500
    TextBox TextBox txtCurrentQtr4 16800
    Button Button Close btnClose
    GroupBox GroupBox   Previous Year's Sales
    Label Label   1st Qtr
    Label Label   2nd Qtr
    Label Label   3rd Qtr
    Label Label   4th Qtr
    TextBox TextBox txtPreviousQtr1 10000
    TextBox TextBox txtPreviousQtr2 11000
    TextBox TextBox txtPreviousQtr3 12500
    TextBox TextBox txtPreviousQtr4 15800
    Button Button Generate btnGenerate
    Label Label   _________ Legend _________
    Label Label lblCurYear This Year's Sales
    Label Label lblLastYear Last Year's Sales
  3. Right-click the table 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 curQtr1, curQtr2, curQtr3, curQtr4;
        int prvQtr1, prvQtr2, prvQtr3, prvQtr4;
    
        // Retrieve the values of the current year's sales
        try {
    	 curQtr1 = int::Parse(txtCurrentQtr1->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the first quater");
    	 curQtr1 = 0;
        }
    
        try {
    	curQtr2 = int::Parse(txtCurrentQtr2->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the second quater");
    	 curQtr2 = 0;
        }
    
        try {
    	curQtr3 = int::Parse(txtCurrentQtr3->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the third quater");
    	 curQtr3 = 0;
        }
    
        try {
    	curQtr4 = int::Parse(txtCurrentQtr4->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the fourth quater");
    	 curQtr4 = 0;
        }
    
        // Create an array of Rectangle objects for the current year
        array<Rectangle> ^ rectCurrentYear = { 
    	Rectangle(this->txtCurrentQtr1->Left+20,
    	          380-curQtr1, 40, curQtr1),
    	Rectangle(this->txtCurrentQtr2->Left+20,
    	          380-curQtr2, 40, curQtr2),
    	Rectangle(this->txtCurrentQtr3->Left+20,
    	          380-curQtr3, 40, curQtr3),
    	Rectangle(this->txtCurrentQtr4->Left+20,
    	          380-curQtr4, 40, curQtr4) };
    
        // Retrieve the values of last year's sales
        try {
    	prvQtr1 = int::Parse(txtPreviousQtr1->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the third quater");
    	 prvQtr1 = 0;
        }
    
        try {
            prvQtr2 = int::Parse(txtPreviousQtr2->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the third quater");
    	 prvQtr2 = 0;
        }
    
        try {
            prvQtr3 = int::Parse(txtPreviousQtr3->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the third quater");
    	 prvQtr3 = 0;
        }
    
        try {
            prvQtr4 = int::Parse(txtPreviousQtr4->Text) / 100;
        }
        catch(FormatException ^)
        {
    	 MessageBox::Show(L"Invalid value for the third quater");
    	 prvQtr4 = 0;
        }
    
        // Create an array of Rectangle objects for the previous year
        array<Rectangle> ^ rectPreviousYear = { 
    	Rectangle(this->txtPreviousQtr1->Left+30,
    	          380-prvQtr1, 40, prvQtr1),
    	Rectangle(this->txtPreviousQtr2->Left+30,
    	          380-prvQtr2, 40, prvQtr2),
    	Rectangle(this->txtPreviousQtr3->Left+30,
    	          380-prvQtr3, 40, prvQtr3),
    	Rectangle(this->txtPreviousQtr4->Left+30,
    	          380-prvQtr4, 40, prvQtr4) };
    			 
        // In case the user has changed the values,
        // erase the previous chart
        graphDrawingArea->Clear(this->BackColor);
    
        // Draw the chart for the previous year first to send it back
        graphDrawingArea->DrawRectangles(gcnew Pen(Color::Blue),
    				     rectPreviousYear);
        // Draw the chart for the current year in front
        graphDrawingArea->DrawRectangles(gcnew Pen(Color::Red),
    				     rectCurrentYear);
    		
        // Draw the small rectangles of the legend
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Blue),
    				    this->lblCurYear->Left-30,
    				    this->lblCurYear->Top,  14, 10);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Red),
    				    this->lblLastYear->Left-30,
    				    this->lblLastYear->Top, 14, 10);
    
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Black),
    					 25, 380, Width - 220, 1);
        Invalidate();
    }
  11. Return to the form. Double-click the Close button and implement its Click event as follows:
     
    System::Void btnClose_Click(System::Object ^  sender, 
    			System::EventArgs ^  e)
    {
        Close();
    }
  12. Execute the application and test the form
     
     
  13. After using it, close the form
 

Home Copyright © 2007-2012 FunctionX