读书人

怎么使用纯C++求出字符串的字体位图的

发布时间: 2013-10-16 11:29:46 作者: rapoo

如何使用纯C++求出字符串的字体位图的像素宽度?”
求大神指教,如何使用纯C++求出字符串的字体位图的像素宽度?不使用MFC中的什么GetTextExtent之类的函数,有没有纯C++的API?
我想在当前字体以及字号下,求出某一个字符串在屏幕显示时所占的像素宽度、、、

万分感谢!!
[解决办法]
研究TrueType绘制算法。
[解决办法]
不同字体与字号下,每个字符的尺寸不同
如果你不想使用MFC,就自己加载所有可能用到的字库并获取每个字符对应的字形尺寸

[解决办法]
看清楚了,这不是MFC:
GetTextExtentPoint32
The GetTextExtentPoint32 function computes the width and height of the specified string of text.

BOOL GetTextExtentPoint32(
HDC hdc, // handle to device context
LPCTSTR lpString, // pointer to text string
int cbString, // number of characters in string
LPSIZE lpSize // pointer to structure for string size
);

Parameters
hdc
Handle to the device context.
lpString
Pointer to the string of text. The string does not need to be zero-terminated, since cbString specifies the length of the string.
cbString
Specifies the number of characters in the string.
lpSize
Pointer to a SIZE structure in which the dimensions of the string are to be returned.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero.

Windows NT: To get extended error information, callGetLastError.

Remarks
The GetTextExtentPoint32 function uses the currently selected font to compute the dimensions of the string. The width and height, in logical units, are computed without considering any clipping.

Because some devices kern characters, the sum of the extents of the characters in a string may not be equal to the extent of the string.

The calculated string width takes into account the intercharacter spacing set by the SetTextCharacterExtra function.

Windows CE: The GetTextExtentPoint32 function is identical to the GetTextExtentPoint function.

QuickInfo
Windows NT: Requires version 3.5 or later.
Windows: Requires Windows 95 or later.
Windows CE: Requires version 2.0 or later.
Header: Declared in wingdi.h.
Import Library: Use gdi32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows and Windows NT.

See Also
Fonts and Text Overview, Font and Text Functions, SetTextCharacterExtra, SIZE


读书人网 >C++

热点推荐