TextRenderer::MeasureText Method (String^, Font^, Size, TextFormatFlags)

 

Provides the size, in pixels, of the specified text when drawn with the specified font and formatting instructions, using the specified size to create the initial bounding rectangle for the text.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public:
static Size MeasureText(
	String^ text,
	Font^ font,
	Size proposedSize,
	TextFormatFlags flags
)

Parameters

text
Type: System::String^

The text to measure.

font
Type: System.Drawing::Font^

The Font to apply to the measured text.

proposedSize
Type: System.Drawing::Size

The Size of the initial bounding rectangle.

flags
Type: System.Windows.Forms::TextFormatFlags

The formatting instructions to apply to the measured text.

Return Value

Type: System.Drawing::Size

The Size, in pixels, of text drawn with the specified font and format.

MeasureText uses the proposedSize and flags parameters to indicate the relationship of height to width when determining the text size. When measuring text on a single line, if the proposedSize parameter represents a Size with a height dimension greater than Int32::MaxValue, the returned Size will be adjusted to reflect the actual height of the text.

You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. If you need to draw a line of text without these extra spaces you should use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter. For an example, see MeasureText(IDeviceContext^, String^, Font^, Size, TextFormatFlags).

System_CAPS_noteNote

This overload of MeasureText(String^, Font^, Size, TextFormatFlags) will ignore a TextFormatFlags value of NoPadding or LeftAndRightPadding. If you are specifying a padding value other than the default, you should use the overload of MeasureText(IDeviceContext^, String^, Font^, Size, TextFormatFlags) that takes a IDeviceContext object.

The following code example demonstrates how to use one of the MeasureText methods. To run this example, paste the code into a Windows Form and call DrawALineOfText from the form's Paint event handler, passing e as PaintEventArgs.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top
Show: