Graphics.MeasureString 方法

定义

测量用指定的 Font 绘制的指定字符串。Measures the specified string when drawn with the specified Font.

重载

MeasureString(String, Font)

测量用指定的 Font 绘制的指定字符串。Measures the specified string when drawn with the specified Font.

MeasureString(String, Font, SizeF)

当在指定的布局区域内以指定的 Font 绘制时,测量指定的字符串。Measures the specified string when drawn with the specified Font within the specified layout area.

MeasureString(String, Font, Int32)

测量用指定的 Font 绘制的指定字符串。Measures the specified string when drawn with the specified Font.

MeasureString(String, Font, PointF, StringFormat)

测量用指定的 Font 绘制并用指定的 StringFormat 格式化的指定字符串。Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

MeasureString(String, Font, SizeF, StringFormat)

测量用指定的 Font 绘制并用指定的 StringFormat 格式化的指定字符串。Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

MeasureString(String, Font, Int32, StringFormat)

测量用指定的 Font 绘制并用指定的 StringFormat 格式化的指定字符串。Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

MeasureString(String, Font, SizeF, StringFormat, Int32, Int32)

测量用指定的 Font 绘制并用指定的 StringFormat 格式化的指定字符串。Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

MeasureString(String, Font)

测量用指定的 Font 绘制的指定字符串。Measures the specified string when drawn with the specified Font.

public:
 System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font);
member this.MeasureString : string * System.Drawing.Font -> System.Drawing.SizeF

参数

text
String

要测量的字符串。String to measure.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

返回

此方法返回 SizeF 结构,该结构表示 text 参数指定的、使用 font 参数绘制的字符串的大小,单位由 PageUnit 属性指定。This method returns a SizeF structure that represents the size, in the units specified by the PageUnit property, of the string specified by the text parameter as drawn with the font parameter.

异常

fontnullfont is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 创建要测量的字符串。Creates a string to measure.

  • 创建字体对象并将其设置为 Arial (16磅)。Creates a font object and sets it to Arial (16 point).

  • 创建一个大小对象,并使用它和 font 对象来度量字符串的大小。Creates a size object and uses it and the font object to measure the size of the string.

  • 使用度量的字符串大小绘制红色矩形。Draws a red rectangle using the measured size of the string.

  • 在绘制的矩形内绘制字符串。Draws the string within the drawn rectangle.

public:
   void MeasureStringMin( PaintEventArgs^ e )
   {
      // Set up string.
      String^ measureString = "Measure String";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );

      // Measure string.
      SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont );

      // Draw rectangle representing size of string.
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
   }
private void MeasureStringMin(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
Private Sub MeasureStringMin(ByVal e As PaintEventArgs)

    ' Set up string.
    Dim measureString As String = "Measure String"
    Dim stringFont As New Font("Arial", 16)

    ' Measure string.
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, stringFont)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    New PointF(0, 0))
End Sub

注解

MeasureString方法旨在用于单个字符串,并包括在字符串前后的少量额外空间,以允许延伸的字形。The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. 此外,此DrawString方法会调整标志符号点以优化显示质量,并显示比报告的MeasureString字符串窄。Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. 若要获取适用于布局中相邻字符串的度量值(例如,实现格式化文本时), MeasureCharacterRanges请使用方法或采用MeasureString StringFormat的方法之一,并通过GenericTypographicTo obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. 此外,请确保TextRenderingHintGraphicsAntiAliasAlso, ensure the TextRenderingHint for the Graphics is AntiAlias.

另请参阅

MeasureString(String, Font, SizeF)

当在指定的布局区域内以指定的 Font 绘制时,测量指定的字符串。Measures the specified string when drawn with the specified Font within the specified layout area.

public:
 System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF -> System.Drawing.SizeF

参数

text
String

要测量的字符串。String to measure.

font
Font

Font 定义字符串的文本格式。Font defines the text format of the string.

layoutArea
SizeF

SizeF 结构,它指定文本的最大布局区域。SizeF structure that specifies the maximum layout area for the text.

返回

此方法返回 SizeF 结构,该结构表示 text 参数指定的、使用 font 参数绘制的字符串的大小,单位由 PageUnit 属性指定。This method returns a SizeF structure that represents the size, in the units specified by the PageUnit property, of the string specified by the text parameter as drawn with the font parameter.

异常

fontnullfont is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 创建要度量的字符串,并将设置为 Arial (16磅)的字体对象。Creates a string to measure and a font object set to Arial (16 point).

  • 设置字符串的最大布局大小。Sets the maximum layout size of the string.

  • 创建大小对象并使用它、字体对象和最大布局大小来度量字符串的大小。Creates a size object and uses it, the font object, and the maximum layout size to measure the size of the string.

  • 使用度量的字符串大小绘制红色矩形。Draws a red rectangle using the measured size of the string.

  • 在绘制的矩形内绘制字符串。Draws the string within the drawn rectangle.

public:
   void MeasureStringSizeF( PaintEventArgs^ e )
   {

      // Set up string.
      String^ measureString = "Measure String";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );

      // Set maximum layout size.
      SizeF layoutSize = SizeF(200.0F,50.0F);

      // Measure string.
      SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize );

      // Draw rectangle representing size of string.
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
   }
private void MeasureStringSizeF(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Set maximum layout size.
    SizeF layoutSize = new SizeF(200.0F, 50.0F);

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont, layoutSize);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
Private Sub MeasureStringSizeF(ByVal e As PaintEventArgs)

    ' Set up string.
    Dim measureString As String = "Measure String"
    Dim stringFont As New Font("Arial", 16)

    ' Set maximum layout size.
    Dim layoutSize As New SizeF(200.0F, 50.0F)

    ' Measure string.
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, stringFont, _
    layoutSize)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    New PointF(0, 0))
End Sub

注解

MeasureString方法旨在用于单个字符串,并包括在字符串前后的少量额外空间,以允许延伸的字形。The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. 此外,此DrawString方法会调整标志符号点以优化显示质量,并显示比报告的MeasureString字符串窄。Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. 若要获取适用于布局中相邻字符串的度量值(例如,实现格式化文本时), MeasureCharacterRanges请使用方法或采用MeasureString StringFormat的方法之一,并通过GenericTypographicTo obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. 此外,请确保TextRenderingHintGraphicsAntiAliasAlso, ensure the TextRenderingHint for the Graphics is AntiAlias.

另请参阅

MeasureString(String, Font, Int32)

测量用指定的 Font 绘制的指定字符串。Measures the specified string when drawn with the specified Font.

public:
 System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, int width);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, int width);
member this.MeasureString : string * System.Drawing.Font * int -> System.Drawing.SizeF

参数

text
String

要测量的字符串。String to measure.

font
Font

Font,它定义字符串的格式。Font that defines the format of the string.

width
Int32

字符串的最大宽度(以像素为单位)。Maximum width of the string in pixels.

返回

此方法返回 SizeF 结构,该结构表示在 text 参数中指定的、使用 font 参数绘制的字符串的大小,单位由 PageUnit 属性指定。This method returns a SizeF structure that represents the size, in the units specified by the PageUnit property, of the string specified in the text parameter as drawn with the font parameter.

异常

fontnullfont is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 创建要度量的字符串,并将设置为 Arial (16磅)的字体对象。Creates a string to measure and a font object set to Arial (16 point).

  • 设置字符串的最大宽度。Sets the maximum width of the string.

  • 创建一个大小对象并使用它、字体对象和最大字符串宽度来度量字符串的大小。Creates a size object and uses it, the font object, and the maximum string width to measure the size of the string.

  • 使用度量的字符串大小绘制红色矩形。Draws a red rectangle using the measured size of the string.

  • 在绘制的矩形内绘制字符串。Draws the string within the drawn rectangle.

public:
   void MeasureStringWidth( PaintEventArgs^ e )
   {
      // Set up string.
      String^ measureString = "Measure String";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );

      // Set maximum width of string.
      int stringWidth = 200;

      // Measure string.
      SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, stringWidth );

      // Draw rectangle representing size of string.
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0) );
   }
private void MeasureStringWidth(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Set maximum width of string.
    int stringWidth = 200;

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0));
}
Private Sub MeasureStringWidth(ByVal e As PaintEventArgs)

    ' Set up string.
    Dim measureString As String = "Measure String"
    Dim stringFont As New Font("Arial", 16)

    ' Set maximum width of string.
    Dim stringWidth As Integer = 200

    ' Measure string.
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, _
    stringFont, stringWidth)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    New PointF(0, 0))
End Sub

注解

参数指定返回SizeF的结构(Width)的宽度分量的最大值。 widthThe width parameter specifies the maximum value of the width component of the returned SizeF structure (Width). 如果参数小于字符串的实际宽度,则返回Width的组件将被截断为一个值,该值表示将在指定宽度内容纳的最大字符数。 widthIf the width parameter is less than the actual width of the string, the returned Width component is truncated to a value representing the maximum number of characters that will fit within the specified width. 为了容纳整个字符串,返回Height的组件将调整为允许使用字符换行显示字符串的值。To accommodate the entire string, the returned Height component is adjusted to a value that allows displaying the string with character wrap.

MeasureString方法旨在用于单个字符串,并包括在字符串前后的少量额外空间,以允许延伸的字形。The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. 此外,此DrawString方法会调整标志符号点以优化显示质量,并显示比报告的MeasureString字符串窄。Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. 若要获取适用于布局中相邻字符串的度量值(例如,实现格式化文本时), MeasureCharacterRanges请使用方法或采用MeasureString StringFormat的方法之一,并通过GenericTypographicTo obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. 此外,请确保TextRenderingHintGraphicsAntiAliasAlso, ensure the TextRenderingHint for the Graphics is AntiAlias.

另请参阅

MeasureString(String, Font, PointF, StringFormat)

测量用指定的 Font 绘制并用指定的 StringFormat 格式化的指定字符串。Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

public:
 System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::PointF origin, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.PointF origin, System.Drawing.StringFormat stringFormat);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.PointF * System.Drawing.StringFormat -> System.Drawing.SizeF

参数

text
String

要测量的字符串。String to measure.

font
Font

Font 定义字符串的文本格式。Font defines the text format of the string.

origin
PointF

PointF 结构,它表示字符串的左上角。PointF structure that represents the upper-left corner of the string.

stringFormat
StringFormat

StringFormat,它表示字符串的格式化信息(如行距)。StringFormat that represents formatting information, such as line spacing, for the string.

返回

此方法返回 SizeF 结构,该结构表示 text 参数指定的、使用 font 参数和 stringFormat 参数绘制的字符串的大小,单位由 PageUnit 属性指定。This method returns a SizeF structure that represents the size, in the units specified by the PageUnit property, of the string specified by the text parameter as drawn with the font parameter and the stringFormat parameter.

异常

fontnullfont is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 创建要度量的字符串,并将字体对象设置为 Arial (16磅)Creates a string to measure and a font object set to Arial (16 point)

  • 创建一个点以定位字符串的左上角。Creates a point to locate the upper-left corner of the string.

  • 创建一个字符串格式对象并将其格式标志设置DirectionVertical为。Creates a string format object and sets its format flags to DirectionVertical.

  • 创建用于度量字符串的大小对象。Creates a size object to measure the string.

  • 使用字符串、字体对象、定位点和字符串格式测量字符串的大小。Measures the size of the string, using the string, the font object, the locating point, and the string format.

  • 使用定位点和字符串的度量大小绘制红色矩形。Draws a red rectangle using the locating point and the measured size of the string.

  • 在绘制的矩形内绘制字符串。Draws the string within the drawn rectangle.

结果是一个垂直矩形,其中包含一个垂直字符串。The result is a vertical rectangle enclosing a vertical string.

public:
   void MeasureStringPointFFormat( PaintEventArgs^ e )
   {

      // Set up string.
      String^ measureString = "Measure String";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );

      // Set point for upper-left corner of string.
      float x = 50.0F;
      float y = 50.0F;
      PointF ulCorner = PointF(x,y);

      // Set string format.
      StringFormat^ newStringFormat = gcnew StringFormat;
      newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;

      // Measure string.
      SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, ulCorner, newStringFormat );

      // Draw rectangle representing size of string.
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), x, y, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      e->Graphics->DrawString( measureString, stringFont, Brushes::Black, ulCorner, newStringFormat );
   }
private void MeasureStringPointFFormat(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Set point for upper-left corner of string.
    float x = 50.0F;
    float y = 50.0F;
    PointF ulCorner = new PointF(x, y);

    // Set string format.
    StringFormat newStringFormat = new StringFormat();
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont, ulCorner, newStringFormat);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), x, y, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, ulCorner, newStringFormat);
}
Private Sub MeasureStringPointFFormat(ByVal e As PaintEventArgs)

    ' Set up string.
    Dim measureString As String = "Measure String"
    Dim stringFont As New Font("Arial", 16)

    ' Set point for upper-left corner of string.
    Dim x As Single = 50.0F
    Dim y As Single = 50.0F
    Dim ulCorner As New PointF(x, y)

    ' Set string format.
    Dim newStringFormat As New StringFormat
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical

    ' Measure string.
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, stringFont, _
    ulCorner, newStringFormat)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), x, y, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    ulCorner, newStringFormat)
End Sub

注解

MeasureString方法旨在用于单个字符串,并包括在字符串前后的少量额外空间,以允许延伸的字形。The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. 此外,此DrawString方法会调整标志符号点以优化显示质量,并显示比报告的MeasureString字符串窄。Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. 若要获取适用于布局中相邻字符串的度量值(例如,实现格式化文本时), MeasureCharacterRanges请使用方法或采用MeasureString StringFormat的方法之一,并通过GenericTypographicTo obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. 此外,请确保TextRenderingHintGraphicsAntiAliasAlso, ensure the TextRenderingHint for the Graphics is AntiAlias.

另请参阅

MeasureString(String, Font, SizeF, StringFormat)

测量用指定的 Font 绘制并用指定的 StringFormat 格式化的指定字符串。Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

public:
 System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat stringFormat);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat -> System.Drawing.SizeF

参数

text
String

要测量的字符串。String to measure.

font
Font

Font 定义字符串的文本格式。Font defines the text format of the string.

layoutArea
SizeF

SizeF 结构,它指定文本的最大布局区域。SizeF structure that specifies the maximum layout area for the text.

stringFormat
StringFormat

StringFormat,它表示字符串的格式化信息(如行距)。StringFormat that represents formatting information, such as line spacing, for the string.

返回

此方法返回 SizeF 结构,该结构表示在 text 参数中指定的、用 font 参数和 stringFormat 参数绘制的字符串的大小,单位由 PageUnit 属性指定。This method returns a SizeF structure that represents the size, in the units specified by the PageUnit property, of the string specified in the text parameter as drawn with the font parameter and the stringFormat parameter.

异常

fontnullfont is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 创建要度量的字符串,并将设置为 Arial (16磅)的字体对象。Creates a string to measure and a font object set to Arial (16 point).

  • 设置字符串的最大布局大小,并创建大小对象来度量字符串。Sets the maximum layout size of the string, creating a size object to measure the string.

  • 创建一个字符串格式对象并将其格式标志设置DirectionVertical为。Creates a string format object and sets its format flags to DirectionVertical.

  • 使用字符串、字体对象、最大布局大小和字符串格式测量字符串的大小。Measures the size of the string, using the string, the font object, the maximum layout size, and the string format.

  • 使用度量的字符串大小绘制红色矩形。Draws a red rectangle using the measured size of the string.

  • 在绘制的矩形内绘制字符串。Draws the string within the drawn rectangle.

结果是一个垂直矩形,其中包含一个垂直字符串。The result is a vertical rectangle enclosing a vertical string.

public:
   void MeasureStringSizeFFormat( PaintEventArgs^ e )
   {
      // Set up string.
      String^ measureString = "Measure String";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );

      // Set maximum layout size.
      SizeF layoutSize = SizeF(100.0F,200.0F);

      // Set string format.
      StringFormat^ newStringFormat = gcnew StringFormat;
      newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;

      // Measure string.
      SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize, newStringFormat );

      // Draw rectangle representing size of string.
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );
   }
private void MeasureStringSizeFFormat(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Set maximum layout size.
    SizeF layoutSize = new SizeF(100.0F, 200.0F);

    // Set string format.
    StringFormat newStringFormat = new StringFormat();
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont, layoutSize, newStringFormat);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0), newStringFormat);
}
Private Sub MeasureStringSizeFFormat(ByVal e As PaintEventArgs)

    ' Set up string.
    Dim measureString As String = "Measure String"
    Dim stringFont As New Font("Arial", 16)

    ' Set maximum layout size.
    Dim layoutSize As New SizeF(100.0F, 200.0F)

    ' Set string format.
    Dim newStringFormat As New StringFormat
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical

    ' Measure string.
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, stringFont, _
    layoutSize, newStringFormat)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    New PointF(0, 0), newStringFormat)
End Sub

注解

MeasureString方法旨在用于单个字符串,并包括在字符串前后的少量额外空间,以允许延伸的字形。The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. 此外,此DrawString方法会调整标志符号点以优化显示质量,并显示比报告的MeasureString字符串窄。Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. 若要获取适用于布局中相邻字符串的度量值(例如,实现格式化文本时), MeasureCharacterRanges请使用方法或采用MeasureString StringFormat的方法之一,并通过GenericTypographicTo obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. 此外,请确保TextRenderingHintGraphicsAntiAliasAlso, ensure the TextRenderingHint for the Graphics is AntiAlias.

另请参阅

MeasureString(String, Font, Int32, StringFormat)

测量用指定的 Font 绘制并用指定的 StringFormat 格式化的指定字符串。Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

public:
 System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, int width, System::Drawing::StringFormat ^ format);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, int width, System.Drawing.StringFormat format);
member this.MeasureString : string * System.Drawing.Font * int * System.Drawing.StringFormat -> System.Drawing.SizeF

参数

text
String

要测量的字符串。String to measure.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

width
Int32

字符串的最大宽度。Maximum width of the string.

format
StringFormat

StringFormat,它表示字符串的格式化信息(如行距)。StringFormat that represents formatting information, such as line spacing, for the string.

返回

此方法返回 SizeF 结构,该结构表示在 text 参数中指定的、用 font 参数和 stringFormat 参数绘制的字符串的大小,单位由 PageUnit 属性指定。This method returns a SizeF structure that represents the size, in the units specified by the PageUnit property, of the string specified in the text parameter as drawn with the font parameter and the stringFormat parameter.

异常

fontnullfont is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 创建要度量的字符串,并将其字体对象设置为 Arial (16磅)。Creates a string to measure and a font object set it to Arial (16 point).

  • 设置字符串的最大宽度。Sets the maximum width of the string.

  • 创建一个字符串格式对象并将其格式标志设置DirectionVertical为。Creates a string format object and sets its format flags to DirectionVertical.

  • 创建用于度量字符串的大小对象。Creates a size object to measure the string.

  • 使用字符串、字体对象、最大宽度和字符串格式测量字符串的大小。Measures the size of the string, using the string, the font object, the maximum width, and the string format.

  • 使用度量的字符串大小绘制红色矩形。Draws a red rectangle using the measured size of the string.

  • 在绘制的矩形内绘制字符串。Draws the string within the drawn rectangle.

结果是一个垂直矩形,其中包含一个垂直字符串。The result is a vertical rectangle enclosing a vertical string.

public:
   void MeasureStringWidthFormat( PaintEventArgs^ e )
   {
      // Set up string.
      String^ measureString = "Measure String";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );

      // Set maximum width of string.
      int stringWidth = 100;

      // Set string format.
      StringFormat^ newStringFormat = gcnew StringFormat;
      newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;

      // Measure string.
      SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, stringWidth, newStringFormat );

      // Draw rectangle representing size of string.
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );
   }
private void MeasureStringWidthFormat(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Set maximum width of string.
    int stringWidth = 100;

    // Set string format.
    StringFormat newStringFormat = new StringFormat();
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

    // Measure string.
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont, stringWidth, newStringFormat);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0), newStringFormat);
}
Private Sub MeasureStringWidthFormat(ByVal e As PaintEventArgs)

    ' Set up string.
    Dim measureString As String = "Measure String"
    Dim stringFont As New Font("Arial", 16)

    ' Set maximum width of string.
    Dim stringWidth As Integer = 100

    ' Set string format.
    Dim newStringFormat As New StringFormat
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical

    ' Measure string.
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, stringFont, _
    stringWidth, newStringFormat)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    New PointF(0, 0), newStringFormat)
End Sub

注解

MeasureString方法旨在用于单个字符串,并包括在字符串前后的少量额外空间,以允许延伸的字形。The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. 此外,此DrawString方法会调整标志符号点以优化显示质量,并显示比报告的MeasureString字符串窄。Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. 若要获取适用于布局中相邻字符串的度量值(例如,实现格式化文本时), MeasureCharacterRanges请使用方法或采用MeasureString StringFormat的方法之一,并通过GenericTypographicTo obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat, and pass GenericTypographic. 此外,请确保TextRenderingHintGraphicsAntiAliasAlso, ensure the TextRenderingHint for the Graphics is AntiAlias.

另请参阅

MeasureString(String, Font, SizeF, StringFormat, Int32, Int32)

测量用指定的 Font 绘制并用指定的 StringFormat 格式化的指定字符串。Measures the specified string when drawn with the specified Font and formatted with the specified StringFormat.

public:
 System::Drawing::SizeF MeasureString(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::SizeF layoutArea, System::Drawing::StringFormat ^ stringFormat, [Runtime::InteropServices::Out] int % charactersFitted, [Runtime::InteropServices::Out] int % linesFilled);
public System.Drawing.SizeF MeasureString (string text, System.Drawing.Font font, System.Drawing.SizeF layoutArea, System.Drawing.StringFormat stringFormat, out int charactersFitted, out int linesFilled);
member this.MeasureString : string * System.Drawing.Font * System.Drawing.SizeF * System.Drawing.StringFormat * int * int -> System.Drawing.SizeF

参数

text
String

要测量的字符串。String to measure.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

layoutArea
SizeF

SizeF 结构,它指定文本的最大布局区域。SizeF structure that specifies the maximum layout area for the text.

stringFormat
StringFormat

StringFormat,它表示字符串的格式化信息(如行距)。StringFormat that represents formatting information, such as line spacing, for the string.

charactersFitted
Int32

字符串中的字符数。Number of characters in the string.

linesFilled
Int32

字符串中的文本行数。Number of text lines in the string.

返回

此方法返回 SizeF 结构,该结构表示 text 参数指定的、使用 font 参数和 stringFormat 参数绘制的字符串的大小,单位由 PageUnit 属性指定。This method returns a SizeF structure that represents the size of the string, in the units specified by the PageUnit property, of the text parameter as drawn with the font parameter and the stringFormat parameter.

异常

fontnullfont is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 创建要度量的字符串,并将字体对象设置为 Arial (16磅)Creates a string to measure and a font object set to Arial (16 point)

  • 设置字符串的最大布局大小。Sets the maximum layout size of the string.

  • 创建一个字符串格式对象并将其格式标志设置DirectionVertical为。Creates a string format object and sets its format flags to DirectionVertical.

  • 创建整数变量charactersFittedlinesFilled ,并使用大小对象来度量字符串。Creates the integer variables charactersFitted and linesFilled and a size object to measure the string.

  • 度量字符串的大小,并使用字符串、字体对象、最大布局大小和字符串格式来确定所填充的字符数和填充的行数。Measures the size of the string and determines the number of characters fitted and lines filled, using the string, the font object, the maximum layout size, and the string format.

  • 使用度量的字符串大小绘制红色矩形。Draws a red rectangle using the measured size of the string.

  • 在绘制的矩形内绘制字符串。Draws the string within the drawn rectangle.

  • 绘制所用字符数的值和填充的行数。Draws the values of the number of characters fitted and lines filled.

结果是一个垂直矩形,其中包含一个垂直字符串。The result is a vertical rectangle enclosing a vertical string.

public:
   void MeasureStringSizeFFormatInts( PaintEventArgs^ e )
   {
      // Set up string.
      String^ measureString = "Measure String";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Arial",16 );

      // Set maximum layout size.
      SizeF layoutSize = SizeF(100.0F,200.0F);

      // Set string format.
      StringFormat^ newStringFormat = gcnew StringFormat;
      newStringFormat->FormatFlags = StringFormatFlags::DirectionVertical;

      // Measure string.
      int charactersFitted;
      int linesFilled;
      SizeF stringSize = e->Graphics->MeasureString( measureString, stringFont, layoutSize, newStringFormat, charactersFitted, linesFilled );

      // Draw rectangle representing size of string.
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), 0.0F, 0.0F, stringSize.Width, stringSize.Height );

      // Draw string to screen.
      e->Graphics->DrawString( measureString, stringFont, Brushes::Black, PointF(0,0), newStringFormat );

      // Draw output parameters to screen.
      String^ outString = String::Format( "chars {0}, lines {1}", charactersFitted, linesFilled );
      e->Graphics->DrawString( outString, stringFont, Brushes::Black, PointF(100,0) );
   }
private void MeasureStringSizeFFormatInts(PaintEventArgs e)
{

    // Set up string.
    string measureString = "Measure String";
    Font stringFont = new Font("Arial", 16);

    // Set maximum layout size.
    SizeF layoutSize = new SizeF(100.0F, 200.0F);

    // Set string format.
    StringFormat newStringFormat = new StringFormat();
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

    // Measure string.
    int charactersFitted;
    int linesFilled;
    SizeF stringSize = new SizeF();
    stringSize = e.Graphics.MeasureString(measureString, stringFont, layoutSize, newStringFormat, out charactersFitted, out linesFilled);

    // Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), 0.0F, 0.0F, stringSize.Width, stringSize.Height);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, new PointF(0, 0), newStringFormat);

    // Draw output parameters to screen.
    string outString = "chars " + charactersFitted + ", lines " + linesFilled;
    e.Graphics.DrawString(outString, stringFont, Brushes.Black, new PointF(100, 0));
}
Private Sub MeasureStringSizeFFormatInts(ByVal e As PaintEventArgs)

    ' Set up string.
    Dim measureString As String = "Measure String"
    Dim stringFont As New Font("Arial", 16)

    ' Set maximum layout size.
    Dim layoutSize As New SizeF(100.0F, 200.0F)

    ' Set string format.
    Dim newStringFormat As New StringFormat
    newStringFormat.FormatFlags = StringFormatFlags.DirectionVertical

    ' Measure string.
    Dim charactersFitted As Integer
    Dim linesFilled As Integer
    Dim stringSize As New SizeF
    stringSize = e.Graphics.MeasureString(measureString, stringFont, _
    layoutSize, newStringFormat, charactersFitted, linesFilled)

    ' Draw rectangle representing size of string.
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), 0.0F, 0.0F, _
    stringSize.Width, stringSize.Height)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    New PointF(0, 0), newStringFormat)

    ' Draw output parameters to screen.
    Dim outString As String = "chars " & charactersFitted & _
    ", lines " & linesFilled
    e.Graphics.DrawString(outString, stringFont, Brushes.Black, _
    New PointF(100, 0))
End Sub

注解

MeasureString方法旨在用于单个字符串,并包括在字符串前后的少量额外空间,以允许延伸的字形。The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs. 此外,此DrawString方法会调整标志符号点以优化显示质量,并显示比报告的MeasureString字符串窄。Also, the DrawString method adjusts glyph points to optimize display quality and might display a string narrower than reported by MeasureString. 若要获取适用于布局中相邻字符串的度量值(例如,实现格式化文本时), MeasureCharacterRanges请使用方法或采用MeasureString StringFormatGenericTypographic的方法之一。To obtain metrics suitable for adjacent strings in layout (for example, when implementing formatted text), use the MeasureCharacterRanges method or one of the MeasureString methods that takes a StringFormat and pass GenericTypographic. TextRenderingHint Graphics应确保AntiAlias的为。Also ensure the TextRenderingHint for the Graphics is AntiAlias.

另请参阅

适用于