Partager via


How to: Draw Shapes with the .NET Framework

The following code example uses the Graphics class to modify the OnPaint event handler to retrieve a pointer to the Graphics object for the main form. This pointer is then used to set the background color of the form and draw a line and an arc using the Graphics.DrawLine and DrawArc methods.

Note

GDI+ is included with Windows XP and is available as a redistributable for Windows NT 4.0 SP 6, Windows 2000, Windows 98, and Windows Me. To download the latest redistributable, see https://go.microsoft.com/fwlink/?linkid=11232. For more information, see GDI+.

Example

#using <system.drawing.dll>
using namespace System;
using namespace System::Drawing;
// ...
protected: 
virtual Void Form1::OnPaint(PaintEventArgs^ pe ) override
{
   Graphics^ g = pe->Graphics;
   g->Clear(Color::AntiqueWhite);

   Rectangle rect = Form::ClientRectangle;
   Rectangle smallRect;
   smallRect.X = rect.X + rect.Width / 4;
   smallRect.Y = rect.Y + rect.Height / 4;
   smallRect.Width = rect.Width / 2;
   smallRect.Height = rect.Height / 2;

   Pen^ redPen = gcnew Pen(Color::Red);
   redPen->Width = 4;
   g->DrawLine(redPen, 0, 0, rect.Width, rect.Height);

   Pen^ bluePen = gcnew Pen(Color::Blue);
   bluePen->Width = 10;
   g->DrawArc( bluePen, smallRect, 90, 270 );
}

See Also

Reference

System::Drawing namespace

Other Resources

.NET Programming in Visual C++