GetTextExtent

2.x

  DWORD GetTextExtent(hdc, lpszString, cbString)    
  HDC hdc; /* handle of device context */
  LPCSTR lpszString; /* address of string */
  int cbString; /* number of bytes in string */

The GetTextExtent function computes the width and height of a line of text, using the current font to compute the dimensions.

Parameters

hdc

Identifies the device context.

lpszString

Points to a character string.

cbString

Specifies the number of bytes in the string.

Return Value

The low-order word of the return value contains the string width, in logical units, if the function is successful; the high-order word contains the string height.

Comments

The current clipping region does not affect the width and height returned by the GetTextExtent function.

Since some devices do not place characters in regular cell arrays (that is, they kern characters), the sum of the extents of the characters in a string may not be equal to the extent of the string.

Example

The following example retrieves the number of characters in a string by using the lstrlen function, calls the GetTextExtent function to retrieve the dimensions of the string, and then uses the LOWORD macro to determine the string width, in logical units:

DWORD dwExtent;
WORD wTextWidth;
LPSTR lpszJustified = "Text to be justified in this test.";

dwExtent = GetTextExtent(hdc, lpszJustified, lstrlen(lpszJustified));
wTextWidth = LOWORD(dwExtent);

See Also

GetTabbedTextExtent, SetTextJustification