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)

テキストの初期の外接する四角形を作成するために指定されたサイズを使用し、指定したフォントと書式指定を使用して描画される場合の、指定したテキストのサイズ (ピクセル単位) を提供します。

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

指定した text と書式で描画される fontSize (ピクセル単位)。

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

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高さ寸法よりInt32.MaxValue大きい a Size を表す場合、返されるSizeテキストは実際の高さを反映するように調整されます。

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

注意

このオーバーロードのMeasureText(String, Font, Size, TextFormatFlags)NoPaddingは無視TextFormatFlagsされます。LeftAndRightPadding 既定値以外のパディング値を指定する場合は、オブジェクトを受け取るIDeviceContextオーバーロードをMeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)使用する必要があります。

適用対象

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

テキストの初期の外接する四角形を作成するために指定されたサイズを使用し、指定したフォントと書式指定を使用して描画される場合の、指定したテキストのサイズ (ピクセル単位) を提供します。

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

指定した text と書式で描画される fontSize (ピクセル単位)。

例外

ModifyString が設定されます。

適用対象

MeasureText(IDeviceContext, String, Font, Size)

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

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

指定した text で描画される fontSize (ピクセル単位)。

例外

dcnullです。

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

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高さ寸法よりInt32.MaxValue大きい a Size を表す場合、返されるSizeテキストは実際の高さを反映するように調整されます。

適用対象

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

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

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

指定した text で描画される fontSize (ピクセル単位)。

例外

dcnull です。

適用対象

MeasureText(String, Font, Size)

初期の外接する四角形を作成するために指定されたサイズを使用し、指定したフォントを使用して描画される場合の、指定したテキストのサイズ (ピクセル単位) を提供します。

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

指定した text で描画される fontSize (ピクセル単位)。

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

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高さ寸法よりInt32.MaxValue大きい a Size を表す場合、返されるSizeテキストは実際の高さを反映するように調整されます。

適用対象

MeasureText(IDeviceContext, ReadOnlySpan<Char>, Font)

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

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

戻り値

Size

指定したデバイス コンテキストで指定された font を使用して描画される textSize (ピクセル単位)。

例外

dcnull です。

適用対象

MeasureText(IDeviceContext, String, Font)

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

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

指定したデバイス コンテキストで指定された text を使用して 1 行に描画される fontSize (ピクセル単位)。

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

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)

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

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

指定した text で 1 行に描画される fontSize (ピクセル単位)。 DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) パラメーターを受け取る TextFormatFlags オーバーロードの 1 つを使用して、テキストの描画方法を操作できます。 たとえば、TextRenderer の既定の動作では、グリフの突出部が収まるように、描画されるテキストの外接する四角形にパディングが追加されます。 この余分な領域なしでテキストの行を描画する必要がある場合は、DrawText(IDeviceContext, String, Font, Point, Color) パラメーターと MeasureText(IDeviceContext, String, Font) パラメーターを受け取るバージョンの Size および TextFormatFlags を使用する必要があります。 例については、「MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)」を参照してください。

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

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)

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

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

戻り値

Size

指定したフォントで 1 行に描画されるテキストの Size (ピクセル単位)。 DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags) パラメーターを受け取る TextFormatFlags オーバーロードの 1 つを使用して、テキストの描画方法を操作できます。 たとえば、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)

テキストの初期の外接する四角形を作成するために指定されたサイズを使用し、指定したデバイス コンテキスト、フォント、および書式指定を使用して描画される場合の、指定したテキストのサイズ (ピクセル単位) を提供します。

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

指定した text と書式で描画される fontSize (ピクセル単位)。

例外

dcnull です。

ModifyString が設定されます。

適用対象

MeasureText(ReadOnlySpan<Char>, Font, Size)

初期の外接する四角形を作成するために指定されたサイズを使用し、指定したフォントを使用して描画される場合の、指定したテキストのサイズ (ピクセル単位) を提供します。

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

指定した text で描画される fontSize (ピクセル単位)。

適用対象

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

テキストの初期の外接する四角形を作成するために指定されたサイズを使用し、指定したデバイス コンテキスト、フォント、および書式指定を使用して描画される場合の、指定したテキストのサイズ (ピクセル単位) を提供します。

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

指定した text と書式で描画される fontSize (ピクセル単位)。

例外

dcnullです。

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

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高さ寸法よりInt32.MaxValue大きい a Size を表す場合、返されるSizeテキストは実際の高さを反映するように調整されます。

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

適用対象