TextRenderer.MeasureText 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在使用指定的字型繪製時,測量指定的文字。
多載
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
要測量的文字。
- flags
- TextFormatFlags
格式化指示,要套用至已測量的文字。
傳回
以指定之 text
和格式所繪製 font
的 Size (以像素為單位)。
範例
下列程式碼範例示範如何使用其中 MeasureText 一種方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的事件處理常式呼叫 DrawALineOfText
,並 e
傳遞為 PaintEventArgs 。 Paint
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
參數來指出決定文字大小時高度與寬度的關聯性。 在單行上測量文字時,如果 proposedSize
參數代表 Size 高度維度大於 Int32.MaxValue 的 ,則會調整傳 Size 回的 ,以反映文字的實際高度。
您可以使用其中一個採用 DrawText 參數的 TextFormatFlags 多載,管理文字的繪製方式。 例如,TextRenderer 的預設行為是將邊框距離加入至已繪製之文字的週框 (Bounding Rectangle),以容納突出 (Overhanging) 的圖像 (Glyph)。 如果您需要繪製一行文字而不含這些額外空格,則應該使用會採用 DrawText 和 MeasureText 參數的 Size 和 TextFormatFlags 版本。 如需範例,請參閱 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)。
注意
這個 的多 MeasureText(String, Font, Size, TextFormatFlags) 載將會忽略 TextFormatFlags 或 LeftAndRightPadding 的值 NoPadding 。 如果您要指定預設值以外的填補值,您應該使用 接受 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>
要測量的文字。
- flags
- TextFormatFlags
格式化指示,要套用至已測量的文字。
傳回
以指定之 text
和格式所繪製 font
的 Size (以像素為單位)。
例外狀況
適用於
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
參數
要在其中測量文字的裝置內容。
- text
- String
要測量的文字。
傳回
以指定之 text
所繪製 font
的 Size (以像素為單位)。
例外狀況
dc
為 null
。
範例
下列程式碼範例示範如何使用其中 MeasureText 一種方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的事件處理常式呼叫 DrawALineOfText
,並 e
傳遞為 PaintEventArgs 。 Paint
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
使用 參數來指出決定文字大小時高度與寬度的關聯性。 在單行上測量文字時,如果 proposedSize
參數代表 Size 高度維度大於 Int32.MaxValue 的 ,則會調整傳 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
參數
要在其中測量文字的裝置內容。
- text
- ReadOnlySpan<Char>
要測量的文字。
傳回
以指定之 text
所繪製 font
的 Size (以像素為單位)。
例外狀況
dc
上所宣告的預設值是 null
。
適用於
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
要測量的文字。
傳回
以指定之 text
所繪製 font
的 Size (以像素為單位)。
範例
下列程式碼範例示範如何使用其中 MeasureText 一種方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的事件處理常式呼叫 DrawALineOfText
,並 e
傳遞為 PaintEventArgs 。 Paint
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
使用 參數來指出決定文字大小時高度與寬度的關聯性。 在單行上測量文字時,如果 proposedSize
參數代表 Size 高度維度大於 Int32.MaxValue 的 ,則會調整傳 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
參數
要在其中測量文字的裝置內容。
- text
- ReadOnlySpan<Char>
要測量的文字。
傳回
以指定之裝置內容中所指定的 font
所繪製之 text
的 Size (以像素為單位)。
例外狀況
dc
上所宣告的預設值是 null
。
適用於
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
參數
要在其中測量文字的裝置內容。
- text
- String
要測量的文字。
傳回
以指定之裝置內容中所指定的 text
,在單行上所繪製 font
的 Size (以像素為單位)。
範例
下列程式碼範例示範如何使用其中 MeasureText 一種方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的事件處理常式呼叫 DrawALineOfText
,並 e
傳遞為 PaintEventArgs 。 Paint
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 需要以單行繪製文字。
適用於
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
要測量的文字。
傳回
以指定之 text
,在單行上已繪製之 font
的 Size (以像素為單位)。 您可以使用其中一個採用 DrawText(IDeviceContext, String, Font, Rectangle, Color, TextFormatFlags) 參數的 TextFormatFlags 多載,管理文字的繪製方式。 例如,TextRenderer 的預設行為是將邊框距離加入至已繪製之文字的週框 (Bounding Rectangle),以容納突出 (Overhanging) 的圖像 (Glyph)。 如果您需要繪製一行文字而不含這些額外空格,則應該使用會採用 DrawText(IDeviceContext, String, Font, Point, Color) 和 MeasureText(IDeviceContext, String, Font) 參數的 Size 和 TextFormatFlags 版本。 如需範例,請參閱 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)。
範例
下列程式碼範例會示範如何使用 MeasureText 方法。 若要執行此範例,請將程式碼貼到 Windows Form 中,並從表單的事件處理常式呼叫 MeasureText1
,並 e
傳遞為 PaintEventArgs 。 Paint
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 需要以單行繪製文字。
適用於
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>
要測量的文字。
傳回
使用指定的字型在單行上繪製之文字的 Size (以像素為單位)。 您可以使用其中一個採用 DrawText(IDeviceContext, ReadOnlySpan<Char>, Font, Rectangle, Color, TextFormatFlags) 參數的 TextFormatFlags 多載,管理文字的繪製方式。 例如,TextRenderer 的預設行為是將邊框距離加入至已繪製之文字的週框 (Bounding Rectangle),以容納突出 (Overhanging) 的圖像 (Glyph)。 如果您需要繪製一行文字而不含這些額外空格,則應該使用會接受 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
參數
要在其中測量文字的裝置內容。
- text
- ReadOnlySpan<Char>
要測量的文字。
- flags
- TextFormatFlags
格式化指示,要套用至已測量的文字。
傳回
以指定之 text
和格式所繪製 font
的 Size (以像素為單位)。
例外狀況
dc
為 null
。
適用於
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>
要測量的文字。
傳回
以指定之 text
所繪製 font
的 Size (以像素為單位)。
適用於
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
參數
要在其中測量文字的裝置內容。
- text
- String
要測量的文字。
- flags
- TextFormatFlags
格式化指示,要套用至已測量的文字。
傳回
以指定之 text
和格式所繪製 font
的 Size (以像素為單位)。
例外狀況
dc
為 null
。
範例
下列範例示範如何使用 MeasureText 和 DrawText 方法,在不同的字型樣式中繪製單行文字。 若要執行此範例,請將下列程式碼貼到 Windows Form 中,並從表單的事件處理常式呼叫 DrawALineOfText
,並 e
傳遞為 PaintEventArgs 。 Paint
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
參數來指出決定文字大小時高度與寬度的關聯性。 在單行上測量文字時,如果 proposedSize
參數代表 Size 高度維度大於 Int32.MaxValue 的 ,則會調整傳 Size 回的 ,以反映文字的實際高度。
您可以使用其中一個採用 DrawText 參數的 TextFormatFlags 多載,管理文字的繪製方式。 例如,TextRenderer 的預設行為是將邊框距離加入至已繪製之文字的週框 (Bounding Rectangle),以容納突出 (Overhanging) 的圖像 (Glyph)。 如果您需要繪製不含這些額外空格的文字行,請使用 採用 和 TextFormatFlags 參數的 DrawText 和 MeasureTextSize 版本,如範例所示。