A pie is a fraction of an ellipse delimited by a starting
angle and an angle that constitutes the desired portion to make up a pie. It can be illustrated
as follows:
To draw a pie, you can use the Graphics::DrawPie()
method that comes in various versions as follows:
public:
void DrawPie(Pen ^pen,
Rectangle rect,
float startAngle,
float sweepAngle);
void DrawPie(Pen ^pen,
RectangleF rect,
float startAngle,
float sweepAngle);
void DrawPie(Pen ^pen,
int x,
int y,
int width,
int height,
int startAngle,
int sweepAngle);
void DrawPie(Pen ^pen,
float x,
float y,
float width,
float height,
float startAngle,
float sweepAngle);
A pie is based on an ellipse (like an arc). The ellipse
would fit in a rectangle passed as the rect argument. The rectangle can
also be specified by its location (x, y) and its dimensions (width and height).
Inside of the parent rectangle in which an ellipse would be
drawn, you set a starting angle. This angle is measured from 0 up counted clockwise
(like the numbers of an analog clock). This means that an angle of 90 represents
6 o'clock and not 12 o'clock. This starting angle is passed as the startAngle
argument.
After specifying the starting angle, you must specify the
amount of angle covered by the pie. This also is measured clockwise. This value
is passed as the sweepAngle argument.
Here is an example:
System::Void Form1_Paint(System::Object ^ sender,
System::Windows::Forms::PaintEventArgs ^ e)
{
Pen ^ penCurrent = gcnew Pen(Color::Red);
e->Graphics->DrawPie(penCurrent, 20, 20, 200, 100, 45, 255);
}
This would produce:
Practical
Learning: Drawing an Ellipse |
|
- Start a new Windows Forms Application named SchoolEnrolment1
- Design the form as follows:
|
Control |
Name |
Text |
Label |
|
|
Enrolment / Program ___________________________ |
Label |
|
|
Graduates |
Label |
|
|
Undergraduates |
Label |
|
|
Certificates |
TextBox |
|
txtGraduates |
0 |
TextBox |
|
txtUndergraduates |
0 |
TextBox |
|
txtCertificates |
0 |
Button |
|
btnCreateChart |
Create Chart |
PictureBox |
|
pbxChart |
|
Label |
|
|
____Legend____ |
Label |
|
lblGraduates |
Graduates |
Label |
|
lblUndergraduates |
Undergraduates |
Label |
|
lblCertificates |
Certificates |
Button |
|
btnClose |
Close |
|
- Right-click the form and click View Code
- Declare three variables as follows:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
float Graduates;
float Undergraduates;
float Certificates;
|
- Return to the form and click an unoccupied area of its body. In the Properties
window, click the Events button
- Double-click the Paint field and implement the event
as follows:
System::Void Form1_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e)
{
pbxChart->CreateGraphics()->DrawEllipse(gcnew Pen(Color::Red),
Rectangle(0,
0,
260,
200));
pbxChart->CreateGraphics()->DrawPie(gcnew Pen(Color::Blue),
0.0F,
0.0F,
260.0F,
200.0F, 0.0F, Graduates);
pbxChart->CreateGraphics()->DrawPie(gcnew Pen(Color::Green),
0.0F,
0.0F,
260.0F,
200.0F, Graduates, Undergraduates);
pbxChart->CreateGraphics()->DrawPie(gcnew Pen(Color::Fuchsia),
0.0F,
0.0F,
260.0F,
200.0F, Graduates + Undergraduates,
Certificates);
e->Graphics->DrawEllipse(gcnew Pen(Color::Blue),
Rectangle(lblGraduates->Left,
lblGraduates->Top + 20,
lblUndergraduates->Width,
20));
e->Graphics->DrawEllipse(gcnew Pen(Color::Green),
Rectangle(lblUndergraduates->Left,
lblUndergraduates->Top + 20,
lblUndergraduates->Width,
20));
e->Graphics->DrawEllipse(gcnew Pen(Color::Fuchsia),
Rectangle(lblCertificates->Left,
lblCertificates->Top + 20,
lblUndergraduates->Width,
20));
}
|
- Return to the form and click the picture box
- In the Events section of the Properties window, double-click Paint and implement its event as follows:
System::Void pbxChart_Paint(System::Object^ sender,
System::Windows::Forms::PaintEventArgs^ e)
{
Invalidate();
}
|
- Return to the form and double-click the Create Chart button
- Implement the event as follows:
System::Void btnCreateChart_Click(System::Object^ sender,
System::EventArgs^ e)
{
float grad, under, cert, total;
float percentGraduates,
percentUndergraduates,
percentCertificates;
grad = float::Parse(txtGraduates->Text);
under = float::Parse(txtUndergraduates->Text);
cert = float::Parse(txtCertificates->Text); // 1860;
total = grad + under + cert;
percentGraduates = (grad / total) * 100;
percentUndergraduates = (under / total) * 100;
percentCertificates = (cert / total) * 100;
Graduates = (360 * percentGraduates) / 100;
Undergraduates = (360 * percentUndergraduates) / 100;
Certificates = (360 * percentCertificates) / 100;
pbxChart->Invalidate();
}
|
- Execute the application and test the form
- After using it, close the form
|
|