Drawing to the Screen

No matter what kind of drawing you're doing, the underlying abstraction that you're dealing with is the Graphics class from the System.Drawing namespace. The Graphics class provides the abstract surface on which you're drawing, whether the results of your drawing operations are displayed on the screen, stored in a file, or spooled to the printer. The Graphics class is too large to show here, but we'll come back to it again and again throughout the chapter.

One way to obtain a graphics object is to use CreateGraphics to create a new one associated with a form:

bool drawEllipse = false;

void drawEllipseButton_Click(object sender, EventArgs e) {
  // Toggle whether or not to draw the ellipse
  drawEllipse = !drawEllipse;

  Graphics ...

Get Windows Forms Programming in C# now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.