GDI+计算文本矩形区域

6 篇文章 0 订阅

功能:限定宽度的情况下,测试文本所占区域

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 to1 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值