Home

GDI+ Tutorials: Polygon

 

Introduction

A polygon is a series of connected lines with the whole shape being closed. In other words, a polygon is defined a group of lines so that, except for the first line of the group, the starting point of each line is the same as the end point of the previous line and the end point of the last line is connected to the start point of the first line.

To draw a polygon, you can use the Graphics::Polygon() method. It is overloaded with two versions as follows:

public:
    void DrawPolygon(Pen^ pen, array<Point>^ points);
    void DrawPolygon(Pen^ pen, array<PointF>^ points);

To use this method, you can first declare a Point or PointF array and pass it as the second argument to the method. Here is an example:

System::Void Form1_Paint(System::Object ^  sender, 
			 System::Windows::Forms::PaintEventArgs ^  e)
{
    array<Point> ^ Pt = { Point(20, 50), Point(180, 50), Point(180, 20),
                          Point(230, 70), Point(180, 120), Point(180, 90),
       	                  Point(20,  90) };

    Pen ^ penCurrent = gcnew Pen(Color::Red);
    e->Graphics->DrawPolygon(penCurrent, Pt);
}

This would produce:

 

 

Home Copyright © 2007-2012 FunctionX