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 窗体中,并从窗体的 事件处理程序调用 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 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。 如果绘制一行文本时不需要这些额外空白,您应当使用带有 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 窗体中,并从窗体的 事件处理程序调用 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 窗体中,并从窗体的 事件处理程序调用 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 窗体中,并从窗体的 事件处理程序调用 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 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。 如果绘制一行文本时不需要这些额外空白,您应当使用带有 DrawText(IDeviceContext, String, Font, Point, Color) 和 MeasureText(IDeviceContext, String, Font) 参数的 Size 和 TextFormatFlags 版本。 有关示例,请参见 MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)。
示例
下面的代码示例演示如何使用 MeasureText 方法。 若要运行此示例,请将代码粘贴到 Windows 窗体中,并从窗体的 事件处理程序调用 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 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。 如果需要绘制没有这些额外空格的一行文本,应使用需要 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 窗体中,并从窗体的 事件处理程序调用 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 的默认行为是向所绘制文本的边框添加空白以符合延伸标志符号。 如果需要在不使用这些额外空格的情况下绘制文本行,请使用 采用 Size 和 参数的 DrawTextMeasureText 和 TextFormatFlags 版本,如示例中所示。