次の方法で共有


Graphics.MeasureCharacterRanges メソッド

指定した文字列内の文字位置の範囲にそれぞれが外接する Region オブジェクトの配列を取得します。

Public Function MeasureCharacterRanges( _
   ByVal text As String, _   ByVal font As Font, _   ByVal layoutRect As RectangleF, _   ByVal stringFormat As StringFormat _) As Region()
[C#]
public Region[] MeasureCharacterRanges(stringtext,Fontfont,RectangleFlayoutRect,StringFormatstringFormat);
[C++]
public: Region* MeasureCharacterRanges(String* text,Font* font,RectangleFlayoutRect,StringFormat* stringFormat) [];
[JScript]
public function MeasureCharacterRanges(
   text : String,font : Font,layoutRect : RectangleF,stringFormat : StringFormat) : Region[];

パラメータ

  • text
    計測する文字列。
  • font
    文字列のテキスト形式を定義する Font オブジェクト。
  • layoutRect
    文字列のレイアウト四角形を指定する RectangleF 構造体。
  • stringFormat
    行間など、文字列の書式情報を表す StringFormat オブジェクト。

戻り値

このメソッドは、指定した文字列内の文字位置の範囲にそれぞれが外接する Region オブジェクトの配列を返します。

解説

このメソッドで返される領域は解像度に依存するため、文字列をメタファイル中に記録するときの解像度と表示するときの解像度が異なると、正確性がわずかに損なわれることがあります。

使用例

[Visual Basic, C#] 次の例は、Windows フォームでの使用を意図してデザインされており、 Paint イベント ハンドラのパラメータである PaintEventArgs e が必要です。このコードは次のアクションを実行します。

  • 文字列 "First and Second ranges" とその文字列を表示するフォント ("Times New Roman"、16 ポイント) を設定します。
  • 文字列内に 2 つの文字範囲 (それぞれ "First" と "Second" に相当) を設定します。
  • 文字列の表示先となる四角形を作成します。
  • 2 つの文字範囲を含む文字列の書式を設定します。
  • 画面に文字列を描画します。
  • 文字範囲を計測し、指定の 2 つの範囲に外接する四角形を決定します。
  • 画面に 2 つの四角形を描画します。

[Visual Basic, C#] 赤の四角形が外接する最初の範囲 ("First") と、青の四角形が外接する 2 つ目の範囲 ("Second") が示された文字列が表示されます。

 
Public Sub MeasureCharacterRangesRegions(e As PaintEventArgs)
' Set up string.
Dim measureString As String = "First and Second ranges"
Dim stringFont As New Font("Times New Roman", 16F)
' 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 = 50F
Dim y As Single = 50F
Dim width As Single = 35F
Dim height As Single = 200F
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(2) As [Region]
stringRegions = 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
        
[C#] 
public 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 = new Region[2];
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));
}
        

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

Graphics クラス | Graphics メンバ | System.Drawing 名前空間