TextRenderer.MeasureText メソッド

定義

指定したフォントで描画するときの指定したテキストを測定します。

オーバーロード

名前 説明
MeasureText(String, Font, Size, TextFormatFlags)

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したフォントと書式設定の指示で描画するときの、指定したテキストのサイズをピクセル単位で提供します。

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

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したフォントと書式設定の指示で描画するときの、指定したテキストのサイズをピクセル単位で提供します。

MeasureText(IDeviceContext, String, Font, Size)

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したデバイス コンテキストで指定したフォントで描画するときの、指定したテキストのサイズ (ピクセル単位) を提供します。

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

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したデバイス コンテキストで指定したフォントで描画するときの、指定したテキストのサイズ (ピクセル単位) を提供します。

MeasureText(String, Font, Size)

指定したサイズを使用して最初の外接する四角形を作成し、指定したフォントで描画するときの指定したテキストのサイズをピクセル単位で提供します。

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

指定したデバイス コンテキストで指定したフォントで描画された指定したテキストのサイズをピクセル単位で提供します。

MeasureText(IDeviceContext, String, Font)

指定したデバイス コンテキストで指定したフォントで描画された指定したテキストのサイズをピクセル単位で提供します。

MeasureText(String, Font)

指定したフォントで描画する場合の、指定したテキストのサイズをピクセル単位で提供します。

MeasureText(ReadOnlySpan<Char>, Font)

指定したフォントで描画する場合の、指定したテキストのサイズをピクセル単位で提供します。

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

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したデバイス コンテキスト、フォント、および書式設定命令で描画するときの、指定したテキストのサイズ (ピクセル単位) を提供します。

MeasureText(ReadOnlySpan<Char>, Font, Size)

指定したサイズを使用して最初の外接する四角形を作成し、指定したフォントで描画するときの指定したテキストのサイズをピクセル単位で提供します。

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

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したデバイス コンテキスト、フォント、および書式設定命令で描画するときの、指定したテキストのサイズ (ピクセル単位) を提供します。

MeasureText(String, Font, Size, TextFormatFlags)

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したフォントと書式設定の指示で描画するときの、指定したテキストのサイズをピクセル単位で提供します。

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

パラメーター

text
String

測定するテキスト。

font
Font

測定されたテキストに適用する Font

proposedSize
Size

最初の外接する四角形の Size

flags
TextFormatFlags

測定されたテキストに適用する書式設定の指示。

返品

指定したSizeと形式で描画textfont (ピクセル単位)。

次のコード例は、 MeasureText メソッドの 1 つを使用する方法を示しています。 この例を実行するには、コードを Windows フォームに貼り付け、フォームの DrawALineOfText イベント ハンドラーから Paint を呼び出し、ePaintEventArgs として渡します。

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 では、 proposedSize パラメーターと flags パラメーターを使用して、テキスト サイズを決定するときの高さと幅の関係を示します。 1 行でテキストを測定するときに、proposedSize パラメーターが高さ寸法がSizeより大きいInt32.MaxValueを表す場合、返されるSizeはテキストの実際の高さを反映するように調整されます。

DrawText パラメーターを受け取るTextFormatFlagsオーバーロードのいずれかを使用して、テキストの描画方法を操作できます。 たとえば、 TextRenderer の既定の動作では、描画されたテキストの外接する四角形にパディングを追加して、オーバーハングするグリフに対応します。 これらの余分なスペースなしでテキストの行を描画する必要がある場合は、DrawTextMeasureTextパラメーターを受け取るSizeTextFormatFlagsのバージョンを使用する必要があります。 例については、MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)を参照してください。

Note

このMeasureText(String, Font, Size, TextFormatFlags)のオーバーロードは、TextFormatFlagsまたはNoPaddingLeftAndRightPadding値を無視します。 既定値以外の埋め込み値を指定する場合は、MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags) オブジェクトを受け取るIDeviceContextのオーバーロードを使用する必要があります。

適用対象

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

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したフォントと書式設定の指示で描画するときの、指定したテキストのサイズをピクセル単位で提供します。

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

パラメーター

text
ReadOnlySpan<Char>

測定するテキスト。

font
Font

測定されたテキストに適用する Font

proposedSize
Size

最初の外接する四角形の Size

flags
TextFormatFlags

測定されたテキストに適用する書式設定の指示。

返品

指定したSizeと形式で描画textfont (ピクセル単位)。

例外

ModifyString が設定されています。

適用対象

MeasureText(IDeviceContext, String, Font, Size)

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したデバイス コンテキストで指定したフォントで描画するときの、指定したテキストのサイズ (ピクセル単位) を提供します。

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

パラメーター

dc
IDeviceContext

テキストを測定するデバイス コンテキスト。

text
String

測定するテキスト。

font
Font

測定されたテキストに適用する Font

proposedSize
Size

最初の外接する四角形の Size

返品

指定したSizeで描画textfont (ピクセル単位)。

例外

dcnullです。

次のコード例は、 MeasureText メソッドの 1 つを使用する方法を示しています。 この例を実行するには、コードを Windows フォームに貼り付け、フォームの DrawALineOfText イベント ハンドラーから Paint を呼び出し、ePaintEventArgs として渡します。

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 メソッドは、proposedSize パラメーターを使用して、テキスト サイズを決定するときの高さと幅の関係を示します。 1 行でテキストを測定するときに、proposedSize パラメーターが高さ寸法がSizeより大きいInt32.MaxValueを表す場合、返されるSizeはテキストの実際の高さを反映するように調整されます。

適用対象

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

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したデバイス コンテキストで指定したフォントで描画するときの、指定したテキストのサイズ (ピクセル単位) を提供します。

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

パラメーター

dc
IDeviceContext

テキストを測定するデバイス コンテキスト。

text
ReadOnlySpan<Char>

測定するテキスト。

font
Font

測定されたテキストに適用する Font

proposedSize
Size

最初の外接する四角形の Size

返品

指定したSizeで描画textfont (ピクセル単位)。

例外

dcnullです。

適用対象

MeasureText(String, Font, Size)

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したサイズを使用して最初の外接する四角形を作成し、指定したフォントで描画するときの指定したテキストのサイズをピクセル単位で提供します。

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

パラメーター

text
String

測定するテキスト。

font
Font

測定されたテキストに適用する Font

proposedSize
Size

最初の外接する四角形の Size

返品

指定したSizeで描画textfont (ピクセル単位)。

次のコード例は、 MeasureText メソッドの 1 つを使用する方法を示しています。 この例を実行するには、コードを Windows フォームに貼り付け、フォームの DrawALineOfText イベント ハンドラーから Paint を呼び出し、ePaintEventArgs として渡します。

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 メソッドは、proposedSize パラメーターを使用して、テキスト サイズを決定するときの高さと幅の関係を示します。 1 行でテキストを測定するときに、proposedSize パラメーターが高さ寸法がSizeより大きいInt32.MaxValueを表す場合、返されるSizeはテキストの実際の高さを反映するように調整されます。

適用対象

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したデバイス コンテキストで指定したフォントで描画された指定したテキストのサイズをピクセル単位で提供します。

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

パラメーター

dc
IDeviceContext

テキストを測定するデバイス コンテキスト。

text
ReadOnlySpan<Char>

測定するテキスト。

font
Font

測定されたテキストに適用する Font

返品

指定したデバイス コンテキストで指定したfontで描画textSize (ピクセル単位)。

例外

dcnullです。

適用対象

MeasureText(IDeviceContext, String, Font)

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したデバイス コンテキストで指定したフォントで描画された指定したテキストのサイズをピクセル単位で提供します。

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

パラメーター

dc
IDeviceContext

テキストを測定するデバイス コンテキスト。

text
String

測定するテキスト。

font
Font

測定されたテキストに適用する Font

返品

指定したデバイス コンテキストで指定したSizeを持つ 1 行に描画textfont (ピクセル単位)。

次のコード例は、 MeasureText メソッドの 1 つを使用する方法を示しています。 この例を実行するには、コードを Windows フォームに貼り付け、フォームの DrawALineOfText イベント ハンドラーから Paint を呼び出し、ePaintEventArgs として渡します。

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メソッドでは、テキストを 1 行に描画する必要があります。

適用対象

MeasureText(String, Font)

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したフォントで描画する場合の、指定したテキストのサイズをピクセル単位で提供します。

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

パラメーター

text
String

測定するテキスト。

font
Font

測定されたテキストに適用する Font

返品

指定したSizeを持つ 1 本の線に描画textfont (ピクセル単位)。 DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) パラメーターを受け取るTextFormatFlagsオーバーロードのいずれかを使用して、テキストの描画方法を操作できます。 たとえば、 TextRenderer の既定の動作では、描画されたテキストの外接する四角形にパディングを追加して、オーバーハングするグリフに対応します。 これらの余分なスペースなしでテキストの行を描画する必要がある場合は、DrawText(IDeviceContext, String, Font, Point, Color)MeasureText(IDeviceContext, String, Font)パラメーターを受け取るSizeTextFormatFlagsのバージョンを使用する必要があります。 例については、MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)を参照してください。

次のコード例は、 MeasureText メソッドの使用方法を示しています。 この例を実行するには、コードを Windows フォームに貼り付け、フォームの MeasureText1 イベント ハンドラーから Paint を呼び出し、ePaintEventArgs として渡します。

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

注釈

MeasureTextメソッドでは、テキストを 1 行に描画する必要があります。

適用対象

MeasureText(ReadOnlySpan<Char>, Font)

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したフォントで描画する場合の、指定したテキストのサイズをピクセル単位で提供します。

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

パラメーター

text
ReadOnlySpan<Char>

測定するテキスト。

font
Font

測定されたテキストに適用する Font

返品

指定したフォントで 1 行に描画されたテキストの Size (ピクセル単位)。 DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags) パラメーターを受け取るTextFormatFlagsオーバーロードのいずれかを使用して、テキストの描画方法を操作できます。 たとえば、 TextRenderer の既定の動作では、描画されたテキストの外接する四角形にパディングを追加して、オーバーハングするグリフに対応します。 これらのスペースを追加せずにテキスト行を描画する必要がある場合は、Size パラメーターと TextFormatFlags パラメーターを受け取るDrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Point, Color)MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)のバージョンを使用する必要があります。 例については、MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)を参照してください。

適用対象

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

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したデバイス コンテキスト、フォント、および書式設定命令で描画するときの、指定したテキストのサイズ (ピクセル単位) を提供します。

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

パラメーター

dc
IDeviceContext

テキストを測定するデバイス コンテキスト。

text
ReadOnlySpan<Char>

測定するテキスト。

font
Font

測定されたテキストに適用する Font

proposedSize
Size

最初の外接する四角形の Size

flags
TextFormatFlags

測定されたテキストに適用する書式設定の指示。

返品

指定したSizeと形式で描画textfont (ピクセル単位)。

例外

dcnullです。

ModifyString が設定されています。

適用対象

MeasureText(ReadOnlySpan<Char>, Font, Size)

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したサイズを使用して最初の外接する四角形を作成し、指定したフォントで描画するときの指定したテキストのサイズをピクセル単位で提供します。

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

パラメーター

text
ReadOnlySpan<Char>

測定するテキスト。

font
Font

測定されたテキストに適用する Font

proposedSize
Size

最初の外接する四角形の Size

返品

指定したSizeで描画textfont (ピクセル単位)。

適用対象

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

ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs
ソース:
TextRenderer.cs

指定したサイズを使用してテキストの最初の外接する四角形を作成し、指定したデバイス コンテキスト、フォント、および書式設定命令で描画するときの、指定したテキストのサイズ (ピクセル単位) を提供します。

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

パラメーター

dc
IDeviceContext

テキストを測定するデバイス コンテキスト。

text
String

測定するテキスト。

font
Font

測定されたテキストに適用する Font

proposedSize
Size

最初の外接する四角形の Size

flags
TextFormatFlags

測定されたテキストに適用する書式設定の指示。

返品

指定したSizeと形式で描画textfont (ピクセル単位)。

例外

dcnullです。

次の例では、 MeasureText メソッドと DrawText メソッドを使用して、異なるフォント スタイルで 1 行のテキストを描画する方法を示します。 この例を実行するには、Windows フォームに次のコードを貼り付け、フォームの DrawALineOfText イベント ハンドラーから Paint を呼び出し、ePaintEventArgs として渡します。

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メソッドでは、proposedSizeパラメーターとflags パラメーターを使用して、テキスト サイズを決定するときの高さと幅の関係を示します。 1 行でテキストを測定するときに、proposedSize パラメーターが高さ寸法がSizeより大きいInt32.MaxValueを表す場合、返されるSizeはテキストの実際の高さを反映するように調整されます。

DrawText パラメーターを受け取るTextFormatFlagsオーバーロードのいずれかを使用して、テキストの描画方法を操作できます。 たとえば、 TextRenderer の既定の動作では、描画されたテキストの外接する四角形にパディングを追加して、オーバーハングするグリフに対応します。 これらの余分なスペースを含まないテキスト行を描画する必要がある場合は、例に示すように、DrawTextMeasureTextパラメーターを受け取るSizeTextFormatFlagsのバージョンを使用します。

適用対象