Click to Rate and Give Feedback
MSDN
MSDN Library
GDI+
GDI+ Reference
Classes
Graphics
Graphics Methods
 MeasureString Method
Graphics::MeasureString Method

The MeasureString method measures the extent of the string in the specified font and layout rectangle.

Syntax

Status MeasureString(      
    const WCHAR *string,     INT length,     const Font *font,     const RectF &layoutRect,     RectF *boundingBox ) const;

Parameters

string
[in] Pointer to a wide-character string to be measured.
length
[in] 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] Pointer to a Font object that specifies the family name, size, and style of the font that is applied to the string.
layoutRect
[in] Reference to a rectangle that bounds the string.
boundingBox
[out] Pointer to a RectF object that receives the rectangle that bounds the string.

Return Value

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.

Example

The following example measures the size of a string and then draws a rectangle that represents that size.

VOID Example_MeasureString(HDC hdc)
{
   Graphics graphics(hdc);
   // Set up the string.
   WCHAR string[] = L"Measure Text";
   Font font(L"Arial", 16);
   RectF layoutRect(0, 0, 100, 50);
   RectF boundRect;
   // Measure the string.
   graphics.MeasureString(string, 12, &font, layoutRect, &boundRect);
   // Draw a rectangle that represents the size of the string.
   graphics.DrawRectangle(&Pen(Color(255, 0, 0, 0)), boundRect);
}

Method Information

Stock Implementationgdiplus.dll
HeaderDeclared in Gdiplusgraphics.h, include gdiplus.h
Import librarygdiplus.lib
Minimum availabilityGDI+ 1.0
Minimum operating systems Windows 98/Me, Windows XP, Windows 2000, Windows NT 4.0 SP6

See Also

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2010 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker