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:
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:
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
Learning: Drawing a Rectangle |
|
- Start a new Windows Forms Application named WeeklySales1
- Design the form as follows:
|
Control |
Name |
Text |
Label |
|
|
Monday |
Label |
|
|
Tuesday |
Label |
|
|
Wednesday |
Label |
|
|
Thursday |
Label |
|
|
Friday |
TextBox |
|
txtMonday |
12000 |
TextBox |
|
txtTuesday |
11000 |
TextBox |
|
txtWednesday |
8500 |
TextBox |
|
txtThursday |
16800 |
TextBox |
|
txtFriday |
17500 |
Button |
|
Generate |
btnGenerate |
|
- Right-click the form and click View Code
- Declare two variables as follows:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;
Graphics ^ graphDrawingArea;
Bitmap ^ bmpDrawingArea;
|
- Return to the form and double-click an unoccupied area of its body
- Implement the event
as follows:
System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
bmpDrawingArea = gcnew Bitmap(Width, Height);
graphDrawingArea = Graphics::FromImage(bmpDrawingArea);
}
|
- Return to the form and click an empty area on it. In the Properties
window, click the Events button
- 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);
}
|
- Return to the form and double-click the Generate button
- 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();
}
|
- Execute the application and test the form
- After using it, close the form
|
|