Zebra0.com

csharp drawing

Draw Filled Circle with Border

We will use a brush to draw a filled circle with the center of the circle at the point clicked. Then we will use a pen to draw a border around the circle.

Add this code to the MouseClick event:

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
   // Draw a yellow circle with a border with the center at the point clicked.
   // Get the graphics of the form.
   Graphics g = this.CreateGraphics();
   // Create a Yellow Brush.
   SolidBrush myBrush = new SolidBrush(Color.Yellow);
   // Create a black pen with a width of 1.
   Pen myPen = new Pen(Color.Black, 1);
   // Create a filled yellow circle with the center at point clicked.
   // The diameter of the circle is 40, so the center is x-20.
   g.FillEllipse(myBrush, e.X-20, e.Y-20, 40, 40);
   // Add a black border around it.
   g.DrawEllipse(myPen, e.X - 20, e.Y - 20, 40, 40);
}
To Do: Experiment! Try drawing other shapes with the center at the point clicked.

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