TextRenderer.MeasureText Metoda

Definice

Měří zadaný text při vykreslení se zadaným písmem.

Přetížení

Name Description
MeasureText(String, Font, Size, TextFormatFlags)

Poskytuje velikost zadaného textu v pixelech při vykreslení zadaným písmem a pokyny k formátování pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku textu.

MeasureText(ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

Poskytuje velikost zadaného textu v pixelech při vykreslení zadaným písmem a pokyny k formátování pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku textu.

MeasureText(IDeviceContext, String, Font, Size)

Poskytuje velikost zadaného textu v pixelech při vykreslení se zadaným písmem v zadaném kontextu zařízení pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku pro text.

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size)

Poskytuje velikost zadaného textu v pixelech při vykreslení se zadaným písmem v zadaném kontextu zařízení pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku pro text.

MeasureText(String, Font, Size)

Poskytuje velikost zadaného textu v pixelech při kreslení se zadaným písmem pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku.

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

Poskytuje velikost zadaného textu nakresleného v pixelech se zadaným písmem v zadaném kontextu zařízení.

MeasureText(IDeviceContext, String, Font)

Poskytuje velikost zadaného textu nakresleného v pixelech se zadaným písmem v zadaném kontextu zařízení.

MeasureText(String, Font)

Poskytuje velikost zadaného textu v pixelech při vykreslení se zadaným písmem.

MeasureText(ReadOnlySpan<Char>, Font)

Poskytuje velikost zadaného textu v pixelech při vykreslení se zadaným písmem.

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

Poskytuje velikost zadaného textu v pixelech při kreslení se zadaným kontextem zařízení, písmem a pokyny k formátování pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku pro text.

MeasureText(ReadOnlySpan<Char>, Font, Size)

Poskytuje velikost zadaného textu v pixelech při kreslení se zadaným písmem pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku.

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

Poskytuje velikost zadaného textu v pixelech při kreslení se zadaným kontextem zařízení, písmem a pokyny k formátování pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku pro text.

MeasureText(String, Font, Size, TextFormatFlags)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při vykreslení zadaným písmem a pokyny k formátování pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku textu.

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);
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
Public Shared Function MeasureText (text As String, font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

Parametry

text
String

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

proposedSize
Size

Počáteční Size ohraničující obdélník.

flags
TextFormatFlags

Pokyny k formátování, které se mají použít pro měřený text.

Návraty

V Sizepixelech nakreslených text se zadaným font a formátem.

Příklady

Následující příklad kódu ukazuje, jak použít jednu z MeasureText metod. Pokud chcete tento příklad spustit, vložte kód do formuláře Windows a zavolejte DrawALineOfText z obslužné rutiny události Paint a předejte e jako 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

Poznámky

MeasureText používá parametry proposedSizeflags k označení vztahu výšky k šířce při určování velikosti textu. Pokud parametr při měření textu na jednom řádku představuje proposedSize rozměr výšky větší než Size, vrácený Int32.MaxValue text se upraví tak, Size aby odrážel skutečnou výšku textu.

Pomocí jednoho z DrawText přetížení, které přebírá TextFormatFlags parametr, můžete manipulovat s tím, jak je text nakreslen. Výchozí chování TextRenderer je například přidání odsazení do ohraničujícího obdélníku nakresleného textu tak, aby se přizpůsobilo přetečení glyfů. Pokud potřebujete nakreslit řádek textu bez těchto nadbytečných mezer, měli byste použít verze DrawText a MeasureText ty přebírají Size parametr.TextFormatFlags Pro příklad viz MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Note

Toto přetížení MeasureText(String, Font, Size, TextFormatFlags) bude ignorovat TextFormatFlags hodnotu NoPadding nebo LeftAndRightPadding. Pokud zadáváte jinou hodnotu odsazení než výchozí, měli byste použít přetížení MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags) , které přebírá IDeviceContext objekt.

Platí pro

MeasureText(ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při vykreslení zadaným písmem a pokyny k formátování pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku textu.

public:
 static System::Drawing::Size MeasureText(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText(ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

Parametry

text
ReadOnlySpan<Char>

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

proposedSize
Size

Počáteční Size ohraničující obdélník.

flags
TextFormatFlags

Pokyny k formátování, které se mají použít pro měřený text.

Návraty

V Sizepixelech nakreslených text se zadaným font a formátem.

Výjimky

Platí pro

MeasureText(IDeviceContext, String, Font, Size)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při vykreslení se zadaným písmem v zadaném kontextu zařízení pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku pro 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);
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
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font, proposedSize As Size) As Size

Parametry

dc
IDeviceContext

Kontext zařízení, ve kterém chcete měřit text.

text
String

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

proposedSize
Size

Počáteční Size ohraničující obdélník.

Návraty

V Sizepixelech nakresleného text se zadaným fontparametrem .

Výjimky

dc je null.

Příklady

Následující příklad kódu ukazuje, jak použít jednu z MeasureText metod. Pokud chcete tento příklad spustit, vložte kód do formuláře Windows a zavolejte DrawALineOfText z obslužné rutiny události Paint a předejte e jako 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

Poznámky

Metoda MeasureText používá proposedSize parametr k označení vztahu výšky k šířce při určování velikosti textu. Pokud parametr při měření textu na jednom řádku představuje proposedSize rozměr výšky větší než Size, vrácený Int32.MaxValue text se upraví tak, Size aby odrážel skutečnou výšku textu.

Platí pro

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při vykreslení se zadaným písmem v zadaném kontextu zařízení pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku pro text.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText(System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size) As Size

Parametry

dc
IDeviceContext

Kontext zařízení, ve kterém chcete měřit text.

text
ReadOnlySpan<Char>

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

proposedSize
Size

Počáteční Size ohraničující obdélník.

Návraty

V Sizepixelech nakresleného text se zadaným fontparametrem .

Výjimky

dc je null.

Platí pro

MeasureText(String, Font, Size)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při kreslení se zadaným písmem pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku.

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);
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
Public Shared Function MeasureText (text As String, font As Font, proposedSize As Size) As Size

Parametry

text
String

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

proposedSize
Size

Počáteční Size ohraničující obdélník.

Návraty

V Sizepixelech nakresleného text se zadaným fontparametrem .

Příklady

Následující příklad kódu ukazuje, jak použít jednu z MeasureText metod. Pokud chcete tento příklad spustit, vložte kód do formuláře Windows a zavolejte DrawALineOfText z obslužné rutiny události Paint a předejte e jako 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

Poznámky

Metoda MeasureText používá proposedSize parametr k označení vztahu výšky k šířce při určování velikosti textu. Pokud parametr při měření textu na jednom řádku představuje proposedSize rozměr výšky větší než Size, vrácený Int32.MaxValue text se upraví tak, Size aby odrážel skutečnou výšku textu.

Platí pro

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu nakresleného v pixelech se zadaným písmem v zadaném kontextu zařízení.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font);
public static System.Drawing.Size MeasureText(System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font) As Size

Parametry

dc
IDeviceContext

Kontext zařízení, ve kterém chcete měřit text.

text
ReadOnlySpan<Char>

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

Návraty

V Sizepixelech nakreslené text pomocí zadaného font kontextu zařízení.

Výjimky

dc je null.

Platí pro

MeasureText(IDeviceContext, String, Font)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu nakresleného v pixelech se zadaným písmem v zadaném kontextu zařízení.

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);
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
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font) As Size

Parametry

dc
IDeviceContext

Kontext zařízení, ve kterém chcete měřit text.

text
String

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

Návraty

Pixely Sizenakreslené text v jediné čáře se zadaným font v zadaném kontextu zařízení.

Příklady

Následující příklad kódu ukazuje, jak použít jednu z MeasureText metod. Pokud chcete tento příklad spustit, vložte kód do formuláře Windows a zavolejte DrawALineOfText z obslužné rutiny události Paint a předejte e jako 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

Poznámky

Metoda MeasureText vyžaduje, aby byl text nakreslen na jeden řádek.

Platí pro

MeasureText(String, Font)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při vykreslení se zadaným písmem.

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);
public static System.Drawing.Size MeasureText(string? text, System.Drawing.Font? font);
static member MeasureText : string * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (text As String, font As Font) As Size

Parametry

text
String

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

Návraty

V Sizepixelech nakreslených text na jednu čáru se zadaným font. Pomocí jednoho z DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) přetížení, které přebírá TextFormatFlags parametr, můžete manipulovat s tím, jak je text nakreslen. Výchozí chování TextRenderer je například přidání odsazení do ohraničujícího obdélníku nakresleného textu tak, aby se přizpůsobilo přetečení glyfů. Pokud potřebujete nakreslit řádek textu bez těchto nadbytečných mezer, měli byste použít verze DrawText(IDeviceContext, String, Font, Point, Color) a MeasureText(IDeviceContext, String, Font) ty přebírají Size parametr.TextFormatFlags Pro příklad viz MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Příklady

Následující příklad kódu ukazuje, jak použít metodu MeasureText . Pokud chcete tento příklad spustit, vložte kód do formuláře Windows a zavolejte MeasureText1 z obslužné rutiny události Paint a předejte e jako 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

Poznámky

Metoda MeasureText vyžaduje, aby byl text nakreslen na jeden řádek.

Platí pro

MeasureText(ReadOnlySpan<Char>, Font)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při vykreslení se zadaným písmem.

public:
 static System::Drawing::Size MeasureText(ReadOnlySpan<char> text, System::Drawing::Font ^ font);
public static System.Drawing.Size MeasureText(ReadOnlySpan<char> text, System.Drawing.Font? font);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font) As Size

Parametry

text
ReadOnlySpan<Char>

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

Návraty

Text Sizev pixelech nakreslený na jeden řádek se zadaným písmem. Pomocí jednoho z DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags) přetížení, které přebírá TextFormatFlags parametr, můžete manipulovat s tím, jak je text nakreslen. Výchozí chování TextRenderer je například přidání odsazení do ohraničujícího obdélníku nakresleného textu tak, aby se přizpůsobilo přetečení glyfů. Pokud potřebujete nakreslit čáru textu bez těchto nadbytečných mezer, měli byste použít verze DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color) a MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font) které přebírají velikost a TextFormatFlags parametr. Pro příklad viz MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags).

Platí pro

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font, Size, TextFormatFlags)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při kreslení se zadaným kontextem zařízení, písmem a pokyny k formátování pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku pro text.

public:
 static System::Drawing::Size MeasureText(System::Drawing::IDeviceContext ^ dc, ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize, System::Windows::Forms::TextFormatFlags flags);
public static System.Drawing.Size MeasureText(System.Drawing.IDeviceContext dc, ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize, System.Windows.Forms.TextFormatFlags flags);
static member MeasureText : System.Drawing.IDeviceContext * ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size * System.Windows.Forms.TextFormatFlags -> System.Drawing.Size
Public Shared Function MeasureText (dc As IDeviceContext, text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

Parametry

dc
IDeviceContext

Kontext zařízení, ve kterém chcete měřit text.

text
ReadOnlySpan<Char>

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

proposedSize
Size

Počáteční Size ohraničující obdélník.

flags
TextFormatFlags

Pokyny k formátování, které se mají použít pro měřený text.

Návraty

V Sizepixelech nakreslených text se zadaným font a formátem.

Výjimky

dc je null.

Platí pro

MeasureText(ReadOnlySpan<Char>, Font, Size)

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při kreslení se zadaným písmem pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku.

public:
 static System::Drawing::Size MeasureText(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::Size proposedSize);
public static System.Drawing.Size MeasureText(ReadOnlySpan<char> text, System.Drawing.Font? font, System.Drawing.Size proposedSize);
static member MeasureText : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Size -> System.Drawing.Size
Public Shared Function MeasureText (text As ReadOnlySpan(Of Char), font As Font, proposedSize As Size) As Size

Parametry

text
ReadOnlySpan<Char>

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

proposedSize
Size

Počáteční Size ohraničující obdélník.

Návraty

V Sizepixelech nakresleného text se zadaným fontparametrem .

Platí pro

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

Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs
Zdroj:
TextRenderer.cs

Poskytuje velikost zadaného textu v pixelech při kreslení se zadaným kontextem zařízení, písmem a pokyny k formátování pomocí zadané velikosti k vytvoření počátečního ohraničujícího obdélníku pro 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);
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
Public Shared Function MeasureText (dc As IDeviceContext, text As String, font As Font, proposedSize As Size, flags As TextFormatFlags) As Size

Parametry

dc
IDeviceContext

Kontext zařízení, ve kterém chcete měřit text.

text
String

Text, který chcete měřit.

font
Font

Hodnota Font , která se použije na měřený text.

proposedSize
Size

Počáteční Size ohraničující obdélník.

flags
TextFormatFlags

Pokyny k formátování, které se mají použít pro měřený text.

Návraty

V Sizepixelech nakreslených text se zadaným font a formátem.

Výjimky

dc je null.

Příklady

Následující příklad ukazuje, jak pomocí MeasureText a DrawText metod nakreslit jeden řádek textu v různých stylech písma. Tento příklad spustíte tak, že do formuláře Windows vložíte následující kód a zavoláte DrawALineOfText z obslužné rutiny události Paint a předáte e jako 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

Poznámky

Metoda MeasureText používá proposedSize a flags parametry k označení vztahu výšky k šířce při určování velikosti textu. Pokud parametr při měření textu na jednom řádku představuje proposedSize rozměr výšky větší než Size, vrácený Int32.MaxValue text se upraví tak, Size aby odrážel skutečnou výšku textu.

Pomocí jednoho z DrawText přetížení, které přebírá TextFormatFlags parametr, můžete manipulovat s tím, jak je text nakreslen. Výchozí chování TextRenderer je například přidání odsazení do ohraničujícího obdélníku nakresleného textu tak, aby se přizpůsobilo přetečení glyfů. Pokud potřebujete nakreslit řádek textu bez těchto nadbytečných mezer, použijte verze DrawText a MeasureText které přebírají Size a TextFormatFlags parametr, jak je znázorněno v příkladu.

Platí pro