Zebra0.com

csharp drawing

Drawing Rectangles

Drawing is done with either a Brush or Pen on a graphics object. The pen draws line, the brush draws filled areas.
You can not draw in form load because the Load event is run before the Form is drawn.
So anything you draw is overwritten by the Form.

Add this code to the MouseClick event to draw a rectangle with the top left corner at the point clicked with a width of 10 and height of 5.

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
   // Draw a small circle at the point clicked.
   // Get the graphics of the form.
   Graphics g = this.CreateGraphics();
   // Create a Red Brush.
   SolidBrush myBrush = new SolidBrush(Color.Red);
   // Draw a rectangle from point clicked with a width of 10 and height of 5.
   g.FillRectangle(myBrush, e.X, e.Y, 10,5);
}


To Do: Experiment! Try drawing squares with the center at the point clicked.

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