Graphics.MeasureCharacterRanges 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
MeasureCharacterRanges(ReadOnlySpan<Char>, Font, RectangleF, StringFormat) |
获取 Region 对象的数组,其中每个对象绑定指定字符串中的字符位置范围。 |
MeasureCharacterRanges(String, Font, RectangleF, StringFormat) |
获取 Region 对象的数组,其中每个对象绑定指定字符串中的字符位置范围。 |
MeasureCharacterRanges(ReadOnlySpan<Char>, Font, RectangleF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- 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>
要度量的字符串。
- layoutRect
- RectangleF
RectangleF 结构,该结构指定字符串的布局矩形。
- stringFormat
- StringFormat
表示字符串的格式信息(如行距)的 StringFormat。
返回
此方法返回 Region 对象的数组,其中每个对象都绑定指定字符串中的字符位置范围。
适用于
MeasureCharacterRanges(String, Font, RectangleF, StringFormat)
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- Graphics.cs
- Source:
- 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
要度量的字符串。
- layoutRect
- RectangleF
RectangleF 结构,该结构指定字符串的布局矩形。
- stringFormat
- StringFormat
表示字符串的格式信息(如行距)的 StringFormat。
返回
此方法返回 Region 对象的数组,其中每个对象都绑定指定字符串中的字符位置范围。
例外
font
null
。
示例
下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse
,这是 Paint 事件处理程序的参数。 该代码执行以下操作:
设置字符串“First and 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
注解
此方法返回的区域依赖于分辨率,因此,如果字符串以一个分辨率记录在图元文件中,然后以不同的分辨率播放,则可能会略有准确性损失。