Now that you know how to determine the fonts available on a particular device, how to create a font and select it into the device context, and how to determine the sizes and characteristics of the fonts, it's time to try your hand at text formatting. The process involves placing each line of text within margins in one of four ways: aligned on the left margin, aligned on the right margin, centered between the margins, or justified—that is, running from one margin to the other, with equal spaces between the words. For the first three jobs, you can use the DrawText function with the DT_WORDBREAK parameter, but this approach has limitations. For instance, you can't determine what part of the text DrawText was able to fit within the rectangle. DrawText is convenient for some simple jobs, but for more complex formatting tasks, you'll probably want to employ TextOut.
One of the most useful functions for working with text is GetTextExtent. This function tells you the width and height of a character string based on the current font selected in the device context:
dwExtent = GetTextExtent (hdc, lpString, nCount) ;
The width of the text in logical units is in the low word of dwExtent, and the height of the text in logical units is in the high word. Although you can also obtain the text height from the tmHeight field of the TEXTMETRIC structure, the TEXTMETRIC width is inadequate when you're working with variable-pitch fonts or with italic or boldface text.
Breaking text into lines involves searching for break characters. In theory, you should determine the font's break character from the tmBreakChar field of the TEXTMETRIC structure, but you can also simply assume that it's the space character (ASCII number 32). In theory, you should also use the AnsiNext and AnsiPrev functions to step through the string, but you'll get better performance if you use normal C pointer arithmetic. (Of course, if you hope eventually to convert your programs to languages that use other character sets, which might have 2 or more bytes per character, then you had best follow these ”in theory“ rules right from the start.)