共用方式為


Graphics.MeasureCharacterRanges 方法

定義

多載

MeasureCharacterRanges(ReadOnlySpan<Char>, Font, RectangleF, StringFormat)

取得 Region 物件的陣列,每個物件都會係結指定字串內的字元位置範圍。

MeasureCharacterRanges(String, Font, RectangleF, StringFormat)

取得 Region 物件的陣列,每個物件都會係結指定字串內的字元位置範圍。

MeasureCharacterRanges(ReadOnlySpan<Char>, Font, RectangleF, StringFormat)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

取得 Region 物件的陣列,每個物件都會係結指定字串內的字元位置範圍。

public:
 cli::array <System::Drawing::Region ^> ^ MeasureCharacterRanges(ReadOnlySpan<char> text, System::Drawing::Font ^ font, System::Drawing::RectangleF layoutRect, System::Drawing::StringFormat ^ stringFormat);
public System.Drawing.Region[] MeasureCharacterRanges (ReadOnlySpan<char> text, System.Drawing.Font font, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat? stringFormat);
member this.MeasureCharacterRanges : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.RectangleF * System.Drawing.StringFormat -> System.Drawing.Region[]
Public Function MeasureCharacterRanges (text As ReadOnlySpan(Of Char), font As Font, layoutRect As RectangleF, stringFormat As StringFormat) As Region()

參數

text
ReadOnlySpan<Char>

要測量的字串。

font
Font

Font,定義字串的文字格式。

layoutRect
RectangleF

RectangleF 結構,指定字串的配置矩形。

stringFormat
StringFormat

StringFormat,代表字串的格式資訊,例如行距。

傳回

Region[]

這個方法會傳回 Region 對象的陣列,每個數位都會系結指定字串內的字元位置範圍。

適用於

MeasureCharacterRanges(String, Font, RectangleF, StringFormat)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

取得 Region 物件的陣列,每個物件都會係結指定字串內的字元位置範圍。

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);
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[]
Public Function MeasureCharacterRanges (text As String, font As Font, layoutRect As RectangleF, stringFormat As StringFormat) As Region()

參數

text
String

要測量的字串。

font
Font

Font,定義字串的文字格式。

layoutRect
RectangleF

RectangleF 結構,指定字串的配置矩形。

stringFormat
StringFormat

StringFormat,代表字串的格式資訊,例如行距。

傳回

Region[]

這個方法會傳回 Region 對象的陣列,每個數位都會系結指定字串內的字元位置範圍。

例外狀況

font null

範例

下列程式代碼範例是專為搭配 Windows Forms 使用而設計,而且需要 PaintEventArgse,這是 Paint 事件處理程式的參數。 程式代碼會執行下列動作:

  • 設定字串 「First 和 Second ranges」 和字型來顯示字串 (“Times New Roman”, 16 點)。

  • 設定字串內的兩個字元範圍(對應至 “First” 和 “Second” 字組)。

  • 建立要在其中顯示字串的矩形。

  • 設定字串 3/4 的格式設定,包括兩個字元範圍。

  • 將字串繪製到畫面。

  • 測量字元範圍,決定系結兩個指定範圍的矩形。

  • 將兩個矩形繪製到畫面。

結果是顯示字串,第一個範圍 (“First”) 以紅色矩形和第二個範圍 (“Second”) 系結於藍色矩形。

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

備註

這個方法傳回的區域相依於解析,因此,如果字串以一個解析度記錄在元檔中,稍後在不同的解析度播放時,可能會稍微遺失精確度。

另請參閱

適用於