功能:限定宽度的情况下,测试文本所占区域
CPaintDC dc(this);
Graphics g(dc.GetSafeHdc());
Gdiplus::Font font(L"华文新魏", 30);
Gdiplus::StringFormat sf;
Gdiplus::RectF rectLayer;
rectLayer.X = 0;
rectLayer.Y = 0;
rectLayer.Width = 1000;
rectLayer.Height = -1;
Gdiplus::RectF boundingBox;
g.MeasureString(m_text, -1, &font, rectLayer, &boundingBox );
g.DrawString(m_text, -1, &font, boundingBox, &sf, &Gdiplus::SolidBrush(Gdiplus::Color(255, 0, 0)) );
附录:
The Graphics::MeasureString method measures the extent of the string in the specified font and layout rectangle.
Syntax
Status MeasureString(
[in] const WCHAR *string,
[in] INT length,
[in] const Font *font,
[in, ref] const RectF &layoutRect,
[out] RectF *boundingBox
) const;
Parameters
string [in]
Type: const WCHAR*
Pointer to a wide-character string to be measured.
Important For bidirectional languages, such as Arabic, the string length must not exceed 2046 characters.
length [in]
Type: INT
Integer that specifies the number of characters in the string array. The length parameter can be set to –1 if the string is null terminated.
font [in]
Type: const Font*
Pointer to a Font object that specifies the family name, size, and style of the font that is applied to the string.
layoutRect [in, ref]
Type: const RectF
Reference to a rectangle that bounds the string.
boundingBox [out]
Type: RectF*
Pointer to a RectF object that receives the rectangle that bounds the string.
Return value
Type:
Type: Status
If the method succeeds, it returns OK, which is an element of the Status enumeration.
If the method fails, it returns one of the other elements of the Status enumeration.