Graphics.MeasureCharacterRanges(String, Font, RectangleF, StringFormat) Graphics.MeasureCharacterRanges(String, Font, RectangleF, StringFormat) Graphics.MeasureCharacterRanges(String, Font, RectangleF, StringFormat) Method

定義

指定した文字列内の文字位置の範囲にそれぞれが外接する 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 String

計測する文字列。String to measure.

font
Font Font Font

文字列のテキスト形式を定義する FontFont that defines the text format of the string.

layoutRect
RectangleF RectangleF RectangleF

文字列のレイアウト四角形を指定する RectangleF 構造体。RectangleF structure that specifies the layout rectangle for the string.

stringFormat
StringFormat StringFormat StringFormat

行間など、文字列の書式情報を表す StringFormatStringFormat 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 フォームで使用するために設計されています。 また必要が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:

  • 文字列「1 つ目と 2 つ目の範囲」を設定し、文字列 ("Times 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"は、単語に対応しています)、文字列内の範囲 2 文字のセット。Sets two character ranges within the string (which correspond to the words "First" and "Second").

  • 文字列を表示するための四角形を作成します。Creates a rectangle in which to display the string.

  • 2 つの文字の範囲を含む文字列の書式を設定します。Sets the formatting of the string ¾ including the two character ranges.

  • 文字列を画面に描画します。Draws the string to the screen.

  • 文字範囲を計測、2 つの指定した範囲の外接する四角形を決定します。Measures the character ranges, determines rectangles that bound the two specified ranges.

  • 画面に 2 つの四角形を描画します。Draws the two rectangles to the screen.

結果では、赤い長方形で囲まれた ("First")、最初の範囲と 2 番目の範囲 (「秒」) される青い四角形で範囲指定された文字列が表示されます。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

注釈

このメソッドによって返される領域と解像度に依存は、文字列が 1 つの解像度でのメタファイルに記録され、後で別の解像度で再生する場合、わずかに精度の低下にすることがあります。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.

適用対象

こちらもご覧ください