The Size of a Character

To display multiple lines of text using the TextOut function, you need to determine the dimensions of font characters. You can space successive lines of text based on the height of a character, and you can space columns of text across the client area based on the width of a character.

You can obtain character dimensions with the GetTextMetrics call. GetTextMetrics requires a handle to the device context because it returns information about the font currently selected in the device context. Windows copies the various values of text metrics into a structure of type TEXTMETRIC. The values are in units that depend on the mapping mode selected in the device context. In the default device context, this mapping mode is MM_TEXT, so the dimensions are in units of pixels.

To use the GetTextMetrics function, you first need to define a structure variable (commonly called tm):

TEXTMETRIC tm ;

Next, get a handle to the device context and call GetTextMetrics:

hdc = GetDC (hwnd) ;

GetTextMetrics (hdc, &tm) ;

After you examine the values in the text metric structure (and probably save a few of them for future use), you release the device context:

ReleaseDC (hwnd, hdc) ;