TextRenderer.MeasureText Método

Definición

Mide el texto especificado cuando se traza con la fuente que se concreta.Measures the specified text when drawn with the specified font.

Sobrecargas

MeasureText(String, Font)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente determinada.Provides the size, in pixels, of the specified text when drawn with the specified font.

MeasureText(IDeviceContext, String, Font)

Proporciona el tamaño, en píxeles, del texto especificado trazado con la fuente indicada en el contexto de dispositivo que se concrete.Provides the size, in pixels, of the specified text drawn with the specified font in the specified device context.

MeasureText(String, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.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)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada en el contexto de dispositivo dado y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.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)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente e instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.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)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con el dispositivo de contexto, la fuente y las instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.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)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente determinada.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

Parámetros

text
String

Texto que se va a medir.The text to measure.

font
Font

Font que se va a aplicar al texto medido.The Font to apply to the measured text.

Devoluciones

Size, en píxeles, del parámetro text trazado en una sola línea con el parámetro font especificado.The Size, in pixels, of text drawn on a single line with the specified font. Puede manipular cómo se traza el texto mediante una de las sobrecargas de DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) que toma un parámetro 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. Por ejemplo, el comportamiento predeterminado de TextRenderer es agregar relleno al rectángulo de delimitación del texto trazado para dar cabida a glifos que sobresalgan.For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. Si tiene que trazar una línea de texto sin estos espacios extra, debe usar las versiones de DrawText(IDeviceContext, String, Font, Point, Color) y MeasureText(IDeviceContext, String, Font) que toman un parámetro Size y TextFormatFlags.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. Para obtener un ejemplo, consulte MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Ejemplos

En el ejemplo de código siguiente se muestra cómo utilizar el método MeasureText.The following code example demonstrates how to use the MeasureText method. Para ejecutar este ejemplo, pegue el código en un Windows Form y llame a MeasureText1 desde el controlador de eventos Paint del formulario, pasando e como PaintEventArgs.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

Comentarios

El método MeasureText requiere que el texto se dibuje en una sola línea.The MeasureText method requires that the text is drawn on a single line.

MeasureText(IDeviceContext, String, Font)

Proporciona el tamaño, en píxeles, del texto especificado trazado con la fuente indicada en el contexto de dispositivo que se concrete.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

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.The device context in which to measure the text.

text
String

Texto que se va a medir.The text to measure.

font
Font

Font que se va a aplicar al texto medido.The Font to apply to the measured text.

Devoluciones

Size, en píxeles, del parámetro text trazado en una sola línea con el parámetro font concretado en el contexto de dispositivo especificado.The Size, in pixels, of text drawn in a single line with the specified font in the specified device context.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar uno de los métodos MeasureText.The following code example demonstrates how to use one of the MeasureText methods. Para ejecutar este ejemplo, pegue el código en un Windows Form y llame a DrawALineOfText desde el controlador de eventos Paint del formulario, pasando e como PaintEventArgs.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

Comentarios

El método MeasureText requiere que el texto se dibuje en una sola línea.The MeasureText method requires that the text is drawn on a single line.

MeasureText(String, Font, Size)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.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

Parámetros

text
String

Texto que se va a medir.The text to measure.

font
Font

Font que se va a aplicar al texto medido.The Font to apply to the measured text.

proposedSize
Size

Size del rectángulo delimitador inicial.The Size of the initial bounding rectangle.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font especificado.The Size, in pixels, of text drawn with the specified font.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar uno de los métodos MeasureText.The following code example demonstrates how to use one of the MeasureText methods. Para ejecutar este ejemplo, pegue el código en un Windows Form y llame a DrawALineOfText desde el controlador de eventos Paint del formulario, pasando e como PaintEventArgs.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

Comentarios

El método MeasureText usa el parámetro proposedSize para indicar la relación de alto y ancho al determinar el tamaño del texto.The MeasureText method uses the proposedSize parameter to indicate the relationship of height to width when determining the text size. Al medir el texto en una sola línea, si el parámetro proposedSize representa un Size con una dimensión de alto mayor que Int32.MaxValue, el valor devuelto Size se ajustará para reflejar el alto real del texto.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)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente indicada en el contexto de dispositivo dado y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.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

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.The device context in which to measure the text.

text
String

Texto que se va a medir.The text to measure.

font
Font

Font que se va a aplicar al texto medido.The Font to apply to the measured text.

proposedSize
Size

Size del rectángulo delimitador inicial.The Size of the initial bounding rectangle.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font especificado.The Size, in pixels, of text drawn with the specified font.

Excepciones

dc es null.dc is null.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar uno de los métodos MeasureText.The following code example demonstrates how to use one of the MeasureText methods. Para ejecutar este ejemplo, pegue el código en un Windows Form y llame a DrawALineOfText desde el controlador de eventos Paint del formulario, pasando e como PaintEventArgs.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

Comentarios

El método MeasureText usa el parámetro proposedSize para indicar la relación de alto y ancho al determinar el tamaño del texto.The MeasureText method uses the proposedSize parameter to indicate the relationship of height to width when determining the text size. Al medir el texto en una sola línea, si el parámetro proposedSize representa un Size con una dimensión de alto mayor que Int32.MaxValue, el valor devuelto Size se ajustará para reflejar el alto real del texto.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)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con la fuente e instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.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

Parámetros

text
String

Texto que se va a medir.The text to measure.

font
Font

Font que se va a aplicar al texto medido.The Font to apply to the measured text.

proposedSize
Size

Size del rectángulo delimitador inicial.The Size of the initial bounding rectangle.

flags
TextFormatFlags

Instrucciones de formato que se van a aplicar al texto medido.The formatting instructions to apply to the measured text.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font y el formato especificados.The Size, in pixels, of text drawn with the specified font and format.

Ejemplos

En el ejemplo de código siguiente se muestra cómo usar uno de los métodos MeasureText.The following code example demonstrates how to use one of the MeasureText methods. Para ejecutar este ejemplo, pegue el código en un Windows Form y llame a DrawALineOfText desde el controlador de eventos Paint del formulario, pasando e como PaintEventArgs.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

Comentarios

MeasureText usa los parámetros proposedSize y flags para indicar la relación de alto y ancho al determinar el tamaño del texto.MeasureText uses the proposedSize and flags parameters to indicate the relationship of height to width when determining the text size. Al medir el texto en una sola línea, si el parámetro proposedSize representa un Size con una dimensión de alto mayor que Int32.MaxValue, el valor devuelto Size se ajustará para reflejar el alto real del texto.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.

Puede manipular cómo se traza el texto mediante una de las sobrecargas de DrawText que toma un parámetro TextFormatFlags.You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. Por ejemplo, el comportamiento predeterminado de TextRenderer es agregar relleno al rectángulo de delimitación del texto trazado para dar cabida a glifos que sobresalgan.For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. Si tiene que trazar una línea de texto sin estos espacios extra, debe usar las versiones de DrawText y MeasureText que toman un parámetro Size y TextFormatFlags.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. Para obtener un ejemplo, consulte MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Nota

Esta sobrecarga de MeasureText(String, Font, Size, TextFormatFlags) omitirá un valor TextFormatFlags de NoPadding o LeftAndRightPadding.This overload of MeasureText(String, Font, Size, TextFormatFlags) will ignore a TextFormatFlags value of NoPadding or LeftAndRightPadding. Si especifica un valor de relleno distinto del predeterminado, debe usar la sobrecarga de MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags) que toma un objeto IDeviceContext.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)

Proporciona el tamaño, en píxeles, del texto especificado cuando se traza con el dispositivo de contexto, la fuente y las instrucciones de formato indicadas y utilizando el tamaño concretado para crear un rectángulo delimitador inicial.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

Parámetros

dc
IDeviceContext

Contexto de dispositivo en el que se mide el texto.The device context in which to measure the text.

text
String

Texto que se va a medir.The text to measure.

font
Font

Font que se va a aplicar al texto medido.The Font to apply to the measured text.

proposedSize
Size

Size del rectángulo delimitador inicial.The Size of the initial bounding rectangle.

flags
TextFormatFlags

Instrucciones de formato que se van a aplicar al texto medido.The formatting instructions to apply to the measured text.

Devoluciones

Size, en píxeles, del parámetro text trazado con el parámetro font y el formato especificados.The Size, in pixels, of text drawn with the specified font and format.

Excepciones

dc es null.dc is null.

Ejemplos

En el ejemplo siguiente se muestra cómo usar los métodos MeasureText y DrawText para dibujar una sola línea de texto en distintos estilos de fuente.The following example demonstrates how to use the MeasureText and DrawText methods to draw a single line of text in different font styles. Para ejecutar este ejemplo, pegue el siguiente código en un Windows Form y llame a DrawALineOfText desde el controlador de eventos Paint del formulario, pasando e como PaintEventArgs.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

Comentarios

El método MeasureText usa los parámetros proposedSize y flags para indicar la relación de alto y ancho al determinar el tamaño del texto.The MeasureText method uses the proposedSize and flags parameters to indicate the relationship of height to width when determining the text size. Al medir el texto en una sola línea, si el parámetro proposedSize representa un Size con una dimensión de alto mayor que Int32.MaxValue, el valor devuelto Size se ajustará para reflejar el alto real del texto.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.

Puede manipular cómo se traza el texto mediante una de las sobrecargas de DrawText que toma un parámetro TextFormatFlags.You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. Por ejemplo, el comportamiento predeterminado de TextRenderer es agregar relleno al rectángulo de delimitación del texto trazado para dar cabida a glifos que sobresalgan.For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. Si necesita dibujar una línea de texto sin estos espacios adicionales, use las versiones de DrawText y MeasureText que toman un parámetro Size y TextFormatFlags, tal como se muestra en el ejemplo.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.

Se aplica a