TextRenderer.MeasureText 方法

定义

使用指定字体进行绘制时测量指定的文本。Measures the specified text when drawn with the specified font.

重载

MeasureText(String, Font)

在使用指定字体绘制时,提供指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text when drawn with the specified font.

MeasureText(IDeviceContext, String, Font)

提供在指定设备上下文中使用指定字体绘制的指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text drawn with the specified font in the specified device context.

MeasureText(String, Font, Size)

提供在使用指定尺寸创建初始边框时,采用指定字体绘制的指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text when drawn with the specified font, using the specified size to create an initial bounding rectangle.

MeasureText(IDeviceContext, String, Font, Size)

提供在使用指定尺寸创建文本的初始边框时,在指定的设备上下文中采用指定字体绘制的指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text when drawn with the specified font in the specified device context, using the specified size to create an initial bounding rectangle for the text.

MeasureText(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.

MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)

提供使用指定尺寸创建文本初始边框时,使用指定的设备上下文、字体和格式说明所绘制的指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text when drawn with the specified device context, font, and formatting instructions, using the specified size to create the initial bounding rectangle for the text.

MeasureText(String, Font)

在使用指定字体绘制时,提供指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text when drawn with the specified font.

public:
 static System::Drawing::Size MeasureText(System::String ^ text, System::Drawing::Font ^ font);
public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font);
static member MeasureText : string * System.Drawing.Font -> System.Drawing.Size

参数

text
String

要测量的文本。The text to measure.

font
Font

要应用于已测量文本的 FontThe Font to apply to the measured text.

返回

使用指定的 text 在一行上绘制的 fontSize(以像素为单位)。The Size, in pixels, of text drawn on a single line with the specified font. 您可以通过使用带有 DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) overloads that takes a TextFormatFlags parameter. 例如,TextRenderer 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. 如果绘制一行文本时不需要这些额外空白,您应当使用带有 DrawText(IDeviceContext, String, Font, Point, Color)MeasureText(IDeviceContext, String, Font) 参数的 SizeTextFormatFlags 版本。If you need to draw a line of text without these extra spaces you should use the versions of DrawText(IDeviceContext, String, Font, Point, Color) and MeasureText(IDeviceContext, String, Font) that take a Size and TextFormatFlags parameter. 有关示例,请参见 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

示例

下面的代码示例演示如何使用 MeasureText 方法。The following code example demonstrates how to use the MeasureText method. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 @no__t 事件处理程序中调用 MeasureText1,并将 e 作为 @no__t。To run this example, paste the code into a Windows Form and call MeasureText1 from the form's Paint event handler, passing e as PaintEventArgs.

private void MeasureText1(PaintEventArgs e)
{
    String text1 = "Measure this text";
    Font arialBold = new Font("Arial", 12.0F);
    Size textSize = TextRenderer.MeasureText(text1, arialBold);
    TextRenderer.DrawText(e.Graphics, text1, arialBold, 
        new Rectangle(new Point(10, 10), textSize), Color.Red);  
}
Private Sub MeasureText1(ByVal e As PaintEventArgs)
    Dim text1 As String = "Measure this text"
    Dim arialBold As New Font("Arial", 12.0F)
    Dim textSize As Size = TextRenderer.MeasureText(text1, arialBold)
    TextRenderer.DrawText(e.Graphics, text1, arialBold, _
        New Rectangle(New Point(10, 10), textSize), Color.Red)

End Sub

注解

@No__t-0 方法要求在一行上绘制文本。The MeasureText method requires that the text is drawn on a single line.

MeasureText(IDeviceContext, String, Font)

提供在指定设备上下文中使用指定字体绘制的指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text drawn with the specified font in the specified device context.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font -> System.Drawing.Size

参数

dc
IDeviceContext

测量文本所使用的设备上下文。The device context in which to measure the text.

text
String

要测量的文本。The text to measure.

font
Font

要应用于已测量文本的 FontThe Font to apply to the measured text.

返回

在指定设备上下文中使用指定的 text 在一行上绘制的 fontSize(以像素为单位)。The Size, in pixels, of text drawn in a single line with the specified font in the specified device context.

示例

下面的代码示例演示如何使用 @no__t 0 方法之一。The following code example demonstrates how to use one of the MeasureText methods. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 @no__t 事件处理程序中调用 DrawALineOfText,并将 e 作为 @no__t。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.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

注解

@No__t-0 方法要求在一行上绘制文本。The MeasureText method requires that the text is drawn on a single line.

MeasureText(String, Font, Size)

提供在使用指定尺寸创建初始边框时,采用指定字体绘制的指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text when drawn with the specified font, using the specified size to create an initial bounding rectangle.

public:
 static System::Drawing::Size MeasureText(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font, System.Drawing.Size proposedSize);
static member MeasureText : string * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size

参数

text
String

要测量的文本。The text to measure.

font
Font

要应用于已测量文本的 FontThe Font to apply to the measured text.

proposedSize
Size

初始边框的 SizeThe Size of the initial bounding rectangle.

返回

使用指定的 text 绘制的 fontSize(以像素为单位)。The Size, in pixels, of text drawn with the specified font.

示例

下面的代码示例演示如何使用 @no__t 0 方法之一。The following code example demonstrates how to use one of the MeasureText methods. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 @no__t 事件处理程序中调用 DrawALineOfText,并将 e 作为 @no__t。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.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

注解

@No__t-0 方法使用 @no__t 参数指示在确定文本大小时,高度与宽度之间的关系。The MeasureText method uses the proposedSize parameter to indicate the relationship of height to width when determining the text size. 在单个行上测量文本时,如果 proposedSize 参数表示高度尺寸大于 Int32.MaxValueSize,则将调整返回的 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.

MeasureText(IDeviceContext, String, Font, Size)

提供在使用指定尺寸创建文本的初始边框时,在指定的设备上下文中采用指定字体绘制的指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text when drawn with the specified font in the specified device context, using the specified size to create an initial bounding rectangle for the text.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Size proposedSize);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size

参数

dc
IDeviceContext

测量文本所使用的设备上下文。The device context in which to measure the text.

text
String

要测量的文本。The text to measure.

font
Font

要应用于已测量文本的 FontThe Font to apply to the measured text.

proposedSize
Size

初始边框的 SizeThe Size of the initial bounding rectangle.

返回

使用指定的 text 绘制的 fontSize(以像素为单位)。The Size, in pixels, of text drawn with the specified font.

异常

dcnulldc is null.

示例

下面的代码示例演示如何使用 @no__t 0 方法之一。The following code example demonstrates how to use one of the MeasureText methods. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 @no__t 事件处理程序中调用 DrawALineOfText,并将 e 作为 @no__t。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.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

注解

@No__t-0 方法使用 @no__t 参数指示在确定文本大小时,高度与宽度之间的关系。The MeasureText method uses the proposedSize parameter to indicate the relationship of height to width when determining the text size. 在单个行上测量文本时,如果 proposedSize 参数表示高度尺寸大于 Int32.MaxValueSize,则将调整返回的 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.

MeasureText(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.

public:
 static System::Drawing::Size MeasureText(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText (string text, System.Drawing.Font font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : string * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size

参数

text
String

要测量的文本。The text to measure.

font
Font

要应用于已测量文本的 FontThe Font to apply to the measured text.

proposedSize
Size

初始边框的 SizeThe Size of the initial bounding rectangle.

flags
TextFormatFlags

要应用于已测量文本的格式说明。The formatting instructions to apply to the measured text.

返回

使用指定的 text 和格式绘制的 fontSize(以像素为单位)。The Size, in pixels, of text drawn with the specified font and format.

示例

下面的代码示例演示如何使用 @no__t 0 方法之一。The following code example demonstrates how to use one of the MeasureText methods. 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 @no__t 事件处理程序中调用 DrawALineOfText,并将 e 作为 @no__t。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.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

注解

MeasureText 使用 @no__t 和 flags 参数,以指示在确定文本大小时的高度与宽度之间的关系。MeasureText uses the proposedSize and flags parameters to indicate the relationship of height to width when determining the text size. 在单个行上测量文本时,如果 proposedSize 参数表示高度尺寸大于 Int32.MaxValueSize,则将调整返回的 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.

您可以通过使用带有 DrawText 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. 例如,TextRenderer 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. 如果绘制一行文本时不需要这些额外空白,您应当使用带有 DrawTextMeasureText 参数的 SizeTextFormatFlags 版本。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. 有关示例,请参见 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

备注

@No__t 的此重载将忽略 NoPaddingLeftAndRightPadding 的 @no__t 值。This overload of MeasureText(String, Font, Size, TextFormatFlags) will ignore a TextFormatFlags value of NoPadding or LeftAndRightPadding. 如果指定的填充值不是默认值,则应使用采用 @no__t 对象的 @no__t 的重载。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.

MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)

提供使用指定尺寸创建文本初始边框时,使用指定的设备上下文、字体和格式说明所绘制的指定文本的尺寸(以像素为单位)。Provides the size, in pixels, of the specified text when drawn with the specified device context, font, and formatting instructions, using the specified size to create the initial bounding rectangle for the text.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, System::String ^ text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText (System.Drawing.IDeviceContext dc, string text, System.Drawing.Font font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : System.Drawing.IDeviceContext * string * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size

参数

dc
IDeviceContext

测量文本所使用的设备上下文。The device context in which to measure the text.

text
String

要测量的文本。The text to measure.

font
Font

要应用于已测量文本的 FontThe Font to apply to the measured text.

proposedSize
Size

初始边框的 SizeThe Size of the initial bounding rectangle.

flags
TextFormatFlags

要应用于已测量文本的格式说明。The formatting instructions to apply to the measured text.

返回

使用指定的 text 和格式绘制的 fontSize(以像素为单位)。The Size, in pixels, of text drawn with the specified font and format.

异常

dcnulldc is null.

示例

下面的示例演示如何使用 MeasureTextDrawText 方法以不同的字体样式绘制一行文本。The following example demonstrates how to use the MeasureText and DrawText methods to draw a single line of text in different font styles. 若要运行此示例,请将以下代码粘贴到 Windows 窗体中,并从窗体的 @no__t 事件处理程序中调用 DrawALineOfText,并将 e 作为 @no__t。To run this example paste the following code in a Windows Form and call DrawALineOfText from the form's Paint event handler, passing e as PaintEventArgs.

private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }
}
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub

注解

@No__t-0 方法使用 @no__t 和 flags 参数,以指示在确定文本大小时的高度与宽度之间的关系。The MeasureText method uses the proposedSize and flags parameters to indicate the relationship of height to width when determining the text size. 在单个行上测量文本时,如果 proposedSize 参数表示高度尺寸大于 Int32.MaxValueSize,则将调整返回的 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.

您可以通过使用带有 DrawText 参数的 TextFormatFlags 重载之一,控制文本的绘制方式。You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. 例如,TextRenderer 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. 如果需要在不使用这些额外空间的情况下绘制一行文本,请使用 DrawTextMeasureText 的版本,该版本采用 @no__t 和 @no__t 参数,如示例中所示。If you need to draw a line of text without these extra spaces, use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter, as shown in the example.

适用于