Drawing and Writing

GDI provides a wide variety of output operations, from drawing lines to writing text. Specifically, you can use the LineTo, Rectangle, Ellipse, Arc, Pie, Text-Out, and DrawText functions to draw lines, rectangles, circles, arcs, pie wedges, and text, respectively. All these functions use the selected pen and brush to draw borders and fill interiors, and the selected font to write text.

You can draw lines by using the LineTo function. You usually combine the MoveTo and LineTo functions to draw lines. The following example draws a line from the point (10, 90) to the point (360, 90):

MoveTo(hDC, 10, 90);

LineTo(hDC, 360, 90);

You can draw a rectangle by using the Rectangle function. This function uses the selected pen to draw the border, and the selected brush to fill the interior. The following example draws a rectangle that has its upper-left and lower-right corners at the points (10, 30) and (60, 80), respectively:

Rectangle (hDC, 10, 30, 60, 80);

You can draw an ellipse or circle by using the Ellipse function. The function uses the selected pen to draw the border, and the selected brush to fill the interior. The following example draws an ellipse that is bounded by the rectangle specified by the points (160, 30) and (210, 80):

Ellipse (hDC, 160, 30, 210, 80);

You can draw arcs by using the Arc function. You draw an arc by defining a bounding rectangle for the circle containing the arc, then specifying on which points the arc starts and ends. The following example draws an arc within the rectangle defined by the points (10, 90) and (360, 120); it draws the arc from the point (10, 90) to the point (360, 90):

Arc(hDC, 10, 90, 360, 120, 10, 90, 360, 90);

You can draw a pie wedge by using the Pie function. A pie wedge consists of an arc and two radii extending from the focus of the arc to its endpoints. The Pie function uses the selected pen to draw the border, and the selected brush to fill the interior. The following example draws a pie wedge that is bounded by the rectangle specified by the points (310, 30) and (360, 80) and that starts and ends at the points (360, 30) and (360, 80), respectively:

Pie (hDC, 310, 30, 360, 80, 360, 30, 360, 80);

You can display text by using the TextOut function. The function displays a string starting at the specified point. The following example displays the string A Sample String at the point (1, 1):

TextOut(hDC, 1, 1, “A Sample String”, 15);

You can also use the DrawText function to display text. This function is similar to TextOut, except that it lets you write text on multiple lines. The following example displays the string This long string illustrates the DrawText function on multiple lines in the specified rectangle:

RECT rcTextBox;

LPSTR lpText = “This long string illustrates the DrawText function”;

.

.

.

SetRect(&rcTextBox, 1, 10, 160, 40);

DrawText(hDC, lpText, strlen(lpText), &rcTextBox, DT_LEFT);

This example displays the string pointed to by the lpText variable as one or more left-aligned lines in the rectangle specified by the points (1, 10) and (160, 40).

Although you can also create and display bitmaps in a window, the process is not described in this chapter. For details, see Chapter 23, “Bitmaps.”