Setting the Text Alignment

The TextOut function uses a device context's current text alignment to determine how to position text relative to a given location. For example, the default text alignment is top-left, so TextOut places the upper-left corner of the character cell of the first character in the string at the specified location. That is, a function call such as the following places the upper-left corner of the letter A at the coordinates (10, 10):

TextOut(hDC, 10, 10, “ABCDEF”, 6);

You can change the text alignment for a device context by using the SetTextAlign function. If you think of TextOut as filling a rectangle with a text string, then you can think of the text alignment as specifying what part of the rectangle to place the specified point of the string in. SetTextAlign recognizes the left end, the center, and the right end of the rectangle, as well as the rectangle's top and bottom and the baseline within it. You can combine any one horizontal position with one vertical position to specify several combinations of alignment. For example, the following function sets the text alignment to right-bottom:

SetTextAlign(hDC, TA_RIGHT | TA_BOTTOM);

TextOut(hDC, 10, 10, “ABCDEF”, 6);

This example places the lower-right corner of the letter F at the coordinates (10, 10).

You can always determine the current text alignment by using the GetTextAlign function.