You can retrieve information about the selected font from a device context by using the GetTextMetrics and GetTextFace functions.
The GetTextMetrics function copies a TEXTMETRIC structure into a buffer that you supply. The structure contains a description of the font, including the average dimensions of the character cells within the font, the number of characters in the font, and the character set on which the font is based. You can use the text metrics to determine how much space you'll need between lines of text, or which character values have corresponding characters and which are represented by the font's default character.
The text metrics are most often used to determine how much space you need between lines of text to prevent one line from overwriting another. For example, to compute an appropriate value for single-line spacing, you add the values of the tmHeight and tmExternalLeading fields of the TEXTMETRIC structure. The tmHeight field specifies the height of each character cell and tmExternalLeading specifies the font designer's recommended spacing between the bottom of one character cell and the top of the next. The following example shows how to write several lines with single-spacing:
TEXTMETRIC TextMetric;
int nLineSpacing;
int i;
.
.
.
GetTextMetrics(hDC, &TextMetric);
nLineSpace = TextMetric.tmHeight + TextMetric.tmExternalLeading;
Y = 0;
for (i = 0; i < 4; i++) {
TextOut(hDC, 0, Y, “Single-line spacing”, 19);
Y += nLineSpace;
}
You can also use the text metrics to verify that the selected font has the characteristics you need, such as weight, character set, pitch, and family. This is useful if you did not prepare the device context; for example, if you received it as part of a window message from a child window or control. For more information about the fields of the TEXTMETRIC structure, see the online reference.
The GetTextFace function copies a name identifying the typeface of the selected font into a buffer that you supply. The name of the typeface together with the text metrics let you fully specify the font. The following example copies the name of the current font into the character array FaceName.
char FaceName[32];
.
.
.
GetTextFace(hDC, 32, FaceName);