CharacterRange 结构
指定字符串内字符位置的范围。
**命名空间:**System.Drawing
**程序集:**System.Drawing(在 system.drawing.dll 中)
语法
声明
Public Structure CharacterRange
用法
Dim instance As CharacterRange
public struct CharacterRange
public value class CharacterRange
public final class CharacterRange extends ValueType
JScript 支持使用结构,但不支持进行新的声明。
示例
下面的代码示例演示如何创建 CharacterRange 并使用它突出显示字符串的一部分。此示例是针对使用 Windows 窗体而设计的。将示例粘贴到一个窗体中,然后在处理该窗体的 Paint 事件时调用 HighlightACharacterRange
方法,并将 e 作为 PaintEventArgs 传递。
Private Sub HighlightACharacterRange(ByVal e As PaintEventArgs)
' Declare the string to draw.
Dim message As String = _
"This is the string to highlight a word in."
' Declare the word to highlight.
Dim searchWord As String = "string"
' Create a CharacterRange array with the searchWord
' location and length.
Dim ranges() As CharacterRange = _
New CharacterRange() _
{New CharacterRange(message.IndexOf(searchWord), _
searchWord.Length)}
' Construct a StringFormat object.
Dim stringFormat1 As New StringFormat
' Set the ranges on the StringFormat object.
stringFormat1.SetMeasurableCharacterRanges(ranges)
' Declare the font to write the message in.
Dim largeFont As Font = New Font(FontFamily.GenericSansSerif, _
16.0F, GraphicsUnit.Pixel)
' Construct a new Rectangle.
Dim displayRectangle As New Rectangle(20, 20, 200, 100)
' Convert the Rectangle to a RectangleF.
Dim displayRectangleF As RectangleF = _
RectangleF.op_Implicit(displayRectangle)
' Get the Region to highlight by calling the
' MeasureCharacterRanges method.
Dim charRegion() As Region = _
e.Graphics.MeasureCharacterRanges(message, _
largeFont, displayRectangleF, stringFormat1)
' Draw the message string on the form.
e.Graphics.DrawString(message, largeFont, Brushes.Blue, _
displayRectangleF)
' Fill in the region using a semi-transparent color.
e.Graphics.FillRegion(New SolidBrush(Color.FromArgb(50, _
Color.Fuchsia)), charRegion(0))
End Sub
private void HighlightACharacterRange(PaintEventArgs e)
{
// Declare the string to draw.
string message = "This is the string to highlight a word in.";
// Declare the word to highlight.
string searchWord = "string";
// Create a CharacterRange array with the searchWord
// location and length.
CharacterRange[] ranges =
new CharacterRange[]{new CharacterRange
(message.IndexOf(searchWord), searchWord.Length)};
// Construct a StringFormat object.
StringFormat stringFormat1 = new StringFormat();
// Set the ranges on the StringFormat object.
stringFormat1.SetMeasurableCharacterRanges(ranges);
// Declare the font to write the message in.
Font largeFont = new Font(FontFamily.GenericSansSerif, 16.0F,
GraphicsUnit.Pixel);
// Construct a new Rectangle.
Rectangle displayRectangle = new Rectangle(20, 20, 200, 100);
// Convert the Rectangle to a RectangleF.
RectangleF displayRectangleF = (RectangleF)displayRectangle;
// Get the Region to highlight by calling the
// MeasureCharacterRanges method.
Region[] charRegion = e.Graphics.MeasureCharacterRanges(message,
largeFont, displayRectangleF, stringFormat1);
// Draw the message string on the form.
e.Graphics.DrawString(message, largeFont, Brushes.Blue,
displayRectangleF);
// Fill in the region using a semi-transparent color.
e.Graphics.FillRegion(new SolidBrush(Color.FromArgb(50, Color.Fuchsia)),
charRegion[0]);
largeFont.Dispose();
}
void HighlightACharacterRange( PaintEventArgs^ e )
{
// Declare the string to draw.
String^ message = "This is the string to highlight a word in.";
// Declare the word to highlight.
String^ searchWord = "string";
// Create a CharacterRange array with the searchWord
// location and length.
array<CharacterRange>^ temp = {CharacterRange( message->IndexOf( searchWord ), searchWord->Length )};
array<CharacterRange>^ranges = temp;
// Construct a StringFormat object.
StringFormat^ stringFormat1 = gcnew StringFormat;
// Set the ranges on the StringFormat object.
stringFormat1->SetMeasurableCharacterRanges( ranges );
// Declare the font to write the message in.
System::Drawing::Font^ largeFont = gcnew System::Drawing::Font( FontFamily::GenericSansSerif,16.0F,GraphicsUnit::Pixel );
// Construct a new Rectangle.
Rectangle displayRectangle = Rectangle(20,20,200,100);
// Convert the Rectangle to a RectangleF.
RectangleF displayRectangleF = displayRectangle;
// Get the Region to highlight by calling the
// MeasureCharacterRanges method.
array<System::Drawing::Region^>^charRegion = e->Graphics->MeasureCharacterRanges( message, largeFont, displayRectangleF, stringFormat1 );
// Draw the message string on the form.
e->Graphics->DrawString( message, largeFont, Brushes::Blue, displayRectangleF );
// Fill in the region using a semi-transparent color.
e->Graphics->FillRegion( gcnew SolidBrush( Color::FromArgb( 50, Color::Fuchsia ) ), charRegion[ 0 ] );
delete largeFont;
}
private void HighlightACharacterRange(PaintEventArgs e)
{
// Declare the string to draw.
String message = "This is the string to highlight a word in.";
// Declare the word to highlight.
String searchWord = "string";
// Create a CharacterRange array with the searchWord
// location and length.
CharacterRange ranges[] = new CharacterRange[]
{ new CharacterRange(message.IndexOf(searchWord),
searchWord.get_Length()) };
// Construct a StringFormat object.
StringFormat stringFormat1 = new StringFormat();
// Set the ranges on the StringFormat object.
stringFormat1.SetMeasurableCharacterRanges(ranges);
// Declare the font to write the message in.
Font largeFont = new Font(FontFamily.get_GenericSansSerif(),
16, GraphicsUnit.Pixel);
// Construct a new Rectangle.
Rectangle displayRectangle = new Rectangle(20, 20, 200, 100);
// Convert the Rectangle to a RectangleF.
RectangleF displayRectangleF = RectangleF.op_Implicit(displayRectangle);
// Get the Region to highlight by calling the
// MeasureCharacterRanges method.
Region charRegion[] = e.get_Graphics().MeasureCharacterRanges(message,
largeFont, displayRectangleF, stringFormat1);
// Draw the message string on the form.
e.get_Graphics().DrawString(message, largeFont, Brushes.get_Blue(),
displayRectangleF);
// Fill in the region using a semi-transparent color.
e.get_Graphics().FillRegion(new SolidBrush(Color.FromArgb(50,
Color.get_Fuchsia())), charRegion[0]);
largeFont.Dispose();
} //HighlightACharacterRange
线程安全
此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。
平台
Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0