3.3 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, TextOut, 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.

Drawing a Line

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

MoveTo(hDC, 10, 90);
LineTo(hDC, 360, 90);

Drawing a Rectangle

You 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 coordinates (10,30) and (60,80), respectively:

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

Drawing an Ellipse or Circle

You draw an ellipse or a circle by using the Ellipse function. This function uses the selected pen to draw the border, and the selected brush to fill the interior. The following example draws an ellipse within the rectangle defined by the coordinates (160,30) and (210,80):

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

Drawing an Arc

You draw an arc by using the Arc function. With this function, you define a bounding rectangle for the circle containing the arc, and then specify the points at which the arc starts and ends. The following example draws an arc within the rectangle defined by the coordinates (10,90) and (360,120); it draws the arc from the coordinates (10,90) to the coordinates (360,90):

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

Drawing a Pie Wedge

You 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 within the rectangle defined by the coordinates (310,30) and (360,80) and that starts and ends at the coordinates (360,30) and (360,80), respectively:

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

Displaying Text

You display text by using the TextOut function. This function displays a string starting at the specified point. The following example displays the string “A Sample String” at the coordinates (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, lstrlen(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 defined by the coordinates (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 more information, see Chapter 11, “Bitmaps.”