Graphics.MeasureCharacterRanges(String, Font, RectangleF, StringFormat) 方法

定义

获取 Region 对象的数组,其中每个对象将字符位置的范围限定在指定字符串内。Gets an array of Region objects, each of which bounds a range of character positions within the specified string.

public:
 cli::array <System::Drawing::Region ^> ^ MeasureCharacterRanges(System::String ^ text, System::Drawing::Font ^ font, System::Drawing::RectangleF layoutRect, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.Region[] MeasureCharacterRanges (string text, System.Drawing.Font font, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat stringFormat);
member this.MeasureCharacterRanges : string * System.Drawing.Font * System.Drawing.RectangleF * System.Drawing.StringFormat -> System.Drawing.Region[]

参数

text
String

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

font
Font

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

layoutRect
RectangleF

RectangleF 结构,它指定字符串的布局矩形。RectangleF structure that specifies the layout rectangle for the string.

stringFormat
StringFormat

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

返回

Region[]

此方法返回 Region 对象的数组,其中每个对象将字符位置的范围限定在指定字符串内。This method returns an array of Region objects, each of which bounds a range of character positions within the specified string.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要 PaintEventArgse,它是 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:

  • 设置字符串 "第一和第二范围" 和用于显示字符串的字体("New Roman",16磅)。Sets a string "First and Second ranges" and a font for display of the string ("Times New Roman", 16 point).

  • 设置字符串中的两个字符范围(对应于 "First" 和 "Second" 这两个字符)。Sets two character ranges within the string (which correspond to the words "First" and "Second").

  • 创建一个要在其中显示字符串的矩形。Creates a rectangle in which to display the string.

  • 设置字符串3/4 的格式设置,包括两个字符范围。Sets the formatting of the string ¾ including the two character ranges.

  • 在屏幕上绘制字符串。Draws the string to the screen.

  • 度量字符范围,确定绑定两个指定范围的矩形。Measures the character ranges, determines rectangles that bound the two specified ranges.

  • 在屏幕上绘制两个矩形。Draws the two rectangles to the screen.

结果是显示的字符串,其第一个范围("First")由红色矩形和第二个范围("Second")(用蓝色矩形包围)包围。The result is the displayed string with the first range ("First") bounded by a red rectangle and the second range ("Second") bounded by a blue rectangle.

public:
   void MeasureCharacterRangesRegions( PaintEventArgs^ e )
   {
      // Set up string.
      String^ measureString = "First and Second ranges";
      System::Drawing::Font^ stringFont = gcnew System::Drawing::Font( "Times New Roman",16.0F );

      // Set character ranges to "First" and "Second".
      array<CharacterRange>^ characterRanges = {CharacterRange(0,5),CharacterRange(10,6)};

      // Create rectangle for layout.
      float x = 50.0F;
      float y = 50.0F;
      float width = 35.0F;
      float height = 200.0F;
      RectangleF layoutRect = RectangleF(x,y,width,height);

      // Set string format.
      StringFormat^ stringFormat = gcnew StringFormat;
      stringFormat->FormatFlags = StringFormatFlags::DirectionVertical;
      stringFormat->SetMeasurableCharacterRanges( characterRanges );

      // Draw string to screen.
      e->Graphics->DrawString( measureString, stringFont, Brushes::Black, x, y, stringFormat );

      // Measure two ranges in string.
      array<System::Drawing::Region^>^stringRegions = e->Graphics->MeasureCharacterRanges( measureString, 
    stringFont, layoutRect, stringFormat );

      // Draw rectangle for first measured range.
      RectangleF measureRect1 = stringRegions[ 0 ]->GetBounds( e->Graphics );
      e->Graphics->DrawRectangle( gcnew Pen( Color::Red,1.0f ), Rectangle::Round( measureRect1 ) );

      // Draw rectangle for second measured range.
      RectangleF measureRect2 = stringRegions[ 1 ]->GetBounds( e->Graphics );
      e->Graphics->DrawRectangle( gcnew Pen( Color::Blue,1.0f ), Rectangle::Round( measureRect2 ) );
   }
private void MeasureCharacterRangesRegions(PaintEventArgs e)
{

    // Set up string.
    string measureString = "First and Second ranges";
    Font stringFont = new Font("Times New Roman", 16.0F);

    // Set character ranges to "First" and "Second".
    CharacterRange[] characterRanges = { new CharacterRange(0, 5), new CharacterRange(10, 6) };

    // Create rectangle for layout.
    float x = 50.0F;
    float y = 50.0F;
    float width = 35.0F;
    float height = 200.0F;
    RectangleF layoutRect = new RectangleF(x, y, width, height);

    // Set string format.
    StringFormat stringFormat = new StringFormat();
    stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;
    stringFormat.SetMeasurableCharacterRanges(characterRanges);

    // Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, x, y, stringFormat);

    // Measure two ranges in string.
    Region[] stringRegions = e.Graphics.MeasureCharacterRanges(measureString, 
stringFont, layoutRect, stringFormat);

    // Draw rectangle for first measured range.
    RectangleF measureRect1 = stringRegions[0].GetBounds(e.Graphics);
    e.Graphics.DrawRectangle(new Pen(Color.Red, 1), Rectangle.Round(measureRect1));

    // Draw rectangle for second measured range.
    RectangleF measureRect2 = stringRegions[1].GetBounds(e.Graphics);
    e.Graphics.DrawRectangle(new Pen(Color.Blue, 1), Rectangle.Round(measureRect2));
}
Private Sub MeasureCharacterRangesRegions(ByVal e As PaintEventArgs)

    ' Set up string.
    Dim measureString As String = "First and Second ranges"
    Dim stringFont As New Font("Times New Roman", 16.0F)

    ' Set character ranges to "First" and "Second".
    Dim characterRanges As CharacterRange() = _
    {New CharacterRange(0, 5), New CharacterRange(10, 6)}

    ' Create rectangle for layout.
    Dim x As Single = 50.0F
    Dim y As Single = 50.0F
    Dim width As Single = 35.0F
    Dim height As Single = 200.0F
    Dim layoutRect As New RectangleF(x, y, width, height)

    ' Set string format.
    Dim stringFormat As New StringFormat
    stringFormat.FormatFlags = StringFormatFlags.DirectionVertical
    stringFormat.SetMeasurableCharacterRanges(characterRanges)

    ' Draw string to screen.
    e.Graphics.DrawString(measureString, stringFont, Brushes.Black, _
    x, y, stringFormat)

    ' Measure two ranges in string.
    Dim stringRegions() As [Region] = e.Graphics.MeasureCharacterRanges(measureString, _
stringFont, layoutRect, stringFormat)

    ' Draw rectangle for first measured range.
    Dim measureRect1 As RectangleF = _
    stringRegions(0).GetBounds(e.Graphics)
    e.Graphics.DrawRectangle(New Pen(Color.Red, 1), _
    Rectangle.Round(measureRect1))

    ' Draw rectangle for second measured range.
    Dim measureRect2 As RectangleF = _
    stringRegions(1).GetBounds(e.Graphics)
    e.Graphics.DrawRectangle(New Pen(Color.Blue, 1), _
    Rectangle.Round(measureRect2))
End Sub

注解

此方法返回的区域是依赖于解决方案的,因此,如果在一种解析中将字符串记录在图元文件中,并且以后以其他分辨率播放,则可能会稍微降低准确性。The regions returned by this method are resolution-dependent, so there might be a slight loss of accuracy if strings are recorded in a metafile at one resolution and later played back at a different resolution.

适用于

另请参阅