Original link: http://my.oschina.net/ypimgt/blog/62354
First, get the string size by: GetTextExtentPoint32.
Size *se=new size;
CDC MEMDC; First define a display device object
Memdc.selectobject (font);
...... Other operations related to MEMDC
GetTextExtentPoint32 (Memdc.getsafehdc (), Strtext,wcslen (StrText),<b>se</b>);//Gets the width of the string
SE->CX and Se->cy are the length and width of the string, respectively.
Second, to obtain the size of a single character by: GetTextMetrics.
Textmetric TM;
Memdc.gettextmetrics (&TM);
Textmetric diagram:
The members of the TEXTMETRIC structure are introduced:
Textmetric is the structure of the form defined in the WINGDI.H.
The TEXTMETRIC structure has 20 fields:
typedef struct TAGTEXTMETRIC {
LONG Tmheight; Character height see illustration of textmetric structure field
LONG tmascent; Character Upper height (above baseline)
LONG tmdescent; Character lower height (below baseline)
LONG tminternalleading,//number of top spaces with character heights defined by Tmheight
LONG tmexternalleading,//The number of spaces sandwiched between two rows
LONG tmavecharwidth,//Average character width
LONG tmmaxcharwidth,//width of the widest character
LONG Tmweight; The weight and severity of the font
LONG TmOverhang,//Add additional height on some stitching fonts
LONG Tmdigitizedaspectx,//device horizontal orientation for font design
LONG tmdigitizedaspecty,//device vertical orientation for font design
Bchar Tmfirstchar; The first character defined for a font
Bchar Tmlastchar; The last character defined for the font
Bchar Tmdefaultchar; Substitution characters for characters that are not in the font
Bchar Tmbreakchar; The characters used for Chaizi
BYTE Tmitalic,//non-zero when font is italic
BYTE tmunderlined,//non-zero when the font is underlined
BYTE tmstruckout,//font is deleted when nonzero
BYTE tmpitchandfamily,//font spacing (low 4 bits) and family (high 4 bit)
BYTE Tmcharset; Character set of the font
} textmetric;
Variable:
BYTE tmpitchandfamily:
Tmpf_fixed_pitch for true expression variable-length font, FALSE is fixed-length font
Tmpf_vector for true expression vector font
Tmpf_truetype is true for all true fonts
Tmpf_device to TRUE indicates device font
Variables of type textmetric are used to store device font structure information (the above variables), which is required when outputting text in a window.
Example One
To get the value of the Textmetric type variable, you can use the GetTextMetrics (hdc,lptextmetric) function in the Windows SDK; GetTextMetrics (&TM) is available in MFC.
Examples of use are as follows:
Using the Windows SDK:
Textmetric TM;
GetTextMetrics (HDC,&TM);
Using MFC's Usage:
Textmetric TM;
Cdc::gettextmetrics (&TM);
Example Two
The following is the WM_CREATE program code that obtains the character width and height of the system font:
Case WM_CREATE:
HDC = GetDC (hwnd);
GetTextMetrics (hdc, &TM);
Cxchar = Tm.tmavecharwidth;
Cychar = Tm.tmheight + tm.tmexternalleading;
ReleaseDC (hwnd, HDC);
return 0;
Where Cxchar, Cychar, and cxcaps are variables of type static int defined in the callback function. For fixed-width fonts, cxcaps equals Cxchar. For variable-width fonts, cxcaps is set to Cxchar multiplied by 150%. For variable-width fonts, the tmpitchandfamily field in the TEXTMETRIC structure has a low level of 1, and for fixed-width fonts, the value is 0. Example two uses this bit from Cxchar to calculate cxcaps:cxcaps = (tm.tmpitchandfamily & 1? 3:2) * CXCHAR/2;
Get a single character size and string size