Home

GDI+ Example: Weekly Sales

 

Introduction

This is an example of drawing a rectangular vertical chart.

Practical LearningPractical Learning: Creating the Chart

  1. Start a new Windows Forms Application named WeeklySales2
  2. Design the form as follows:
     
    Company Weekly Sales
    Control Name Text Other Properties
    Label Label   Monday  
    Label Label   Tuesday  
    Label Label   Wednesday  
    Label Label   Thursday  
    Label Label   Friday  
    TextBox TextBox txtMonday 12000 TextAlign: Right
    TextBox TextBox txtTuesday 11000 TextAlign: Right
    TextBox TextBox txtWednesday 8500 TextAlign: Right
    TextBox TextBox txtThursday 16800 TextAlign: Right
    TextBox TextBox txtFriday 17500 TextAlign: Right
    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. Double-click the Generate button and implement its Click event as follows:
     
    System::Void btnGenerate_Click(System::Object ^  sender, 
    			       System::EventArgs ^  e)
    {
        int monday = int::Parse(txtMonday->Text) / 100;
        int tuesday = int::Parse(txtTuesday->Text) / 100;
        int wednesday = int::Parse(txtWednesday->Text) / 100;
        int thursday = int::Parse(txtThursday->Text) / 100;
        int friday = int::Parse(txtFriday->Text) / 100;
    
        graphDrawingArea->Clear(this->BackColor);
    
        graphDrawingArea->FillRectangle(gcnew SolidBrush(Color::Red),
    			this->txtMonday->Left+5,
       			280-monday,    40, monday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Black),
    			this->txtMonday->Left+5,
    		        280-monday,    40, monday);
        graphDrawingArea->FillRectangle(gcnew SolidBrush(Color::Blue),
    			this->txtTuesday->Left+5,
    			280-tuesday,   40, tuesday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Black),
    			this->txtTuesday->Left+5,
    		        280-tuesday,   40, tuesday);
        graphDrawingArea->FillRectangle(gcnew SolidBrush(Color::Fuchsia),
    			this->txtWednesday->Left+5,
    		   	280-wednesday, 40, wednesday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Black),
    			this->txtWednesday->Left+5,
    			280-wednesday, 40, wednesday);
        graphDrawingArea->FillRectangle(new SolidBrush(Color::Brown),
    			this->txtThursday->Left+5,
    			280-thursday,  40, thursday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Black),
    			this->txtThursday->Left+5,
    			280-thursday,  40, thursday);
        graphDrawingArea->FillRectangle(gcnew SolidBrush(Color::Turquoise),
    			this->txtFriday->Left+5,
    			280-friday,    40, friday);
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Black),
    			this->txtFriday->Left+5,
    		        280-friday,    40, friday);
    
        graphDrawingArea->DrawRectangle(gcnew Pen(Color::Black),
    			10, 280, Width - 30, 1);
        Invalidate();
    }
  11. Execute the application and test the form
     
     
  12. After using it, close the form
 

Home Copyright © 2007-2012 FunctionX