Zebra0.com

csharp drawing

Draw Filled Triangle with Border

We will create an array of points, then use a brush to draw a filled polygon at the point clicked. Then we will use a pen to draw a border around the triangle.

Add this code to the MouseClick event:

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
   // Get the graphics of the form.
   Graphics g = this.CreateGraphics();
   // Create a green pen.
   Pen myPen = new Pen(Color.DarkGreen,3);
   // Create a blue brush.
   SolidBrush myBrush= new SolidBrush(Color.CornflowerBlue);
   // Create 3 points, and make array.
   Point pt1 = new Point(e.X-15, e.Y);
   Point pt2 = new Point(e.X+15, e.Y);
   Point pt3 = new Point(e.X, e.Y + 15);
   Point[] points = { pt1, pt2, pt3};
   // Fill a triangle using the array of points.
   g.FillPolygon(myBrush, points);
   // Draw a triangle using the array of points.
   g.DrawPolygon(myPen, points);
}
To Do: Experiment! Try drawing other polygons with the center at the point clicked.

End of lesson, Next lesson: ListBox Manager in C#