CharacterRange 結構
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定字串中的字元位置範圍。
public value class CharacterRange
public value class CharacterRange : IEquatable<System::Drawing::CharacterRange>
public struct CharacterRange
public struct CharacterRange : IEquatable<System.Drawing.CharacterRange>
type CharacterRange = struct
Public Structure CharacterRange
Public Structure CharacterRange
Implements IEquatable(Of CharacterRange)
- 繼承
- 實作
範例
下列程式代碼範例示範如何建立 CharacterRange ,並使用它反白顯示字串的一部分。 此範例的設計目的是要與 Windows Forms 搭配使用。 將範例貼到表單中,並在處理表單的事件時呼叫 HighlightACharacterRange
方法,並e
傳遞為 PaintEventArgs。Paint
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.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();
}
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
建構函式
CharacterRange(Int32, Int32) |
初始化 CharacterRange 結構的新執行個體,指定字串中的字元位置範圍。 |
屬性
First |
取得或設定這個 CharacterRange 的第一個字元之字串中的位置。 |
Length |
取得或設定在這個 CharacterRange 中的位置數目。 |
方法
Equals(CharacterRange) |
指出目前的實例是否等於相同類型的另一個實例。 |
Equals(Object) |
取得值,指出此物件是否相當於指定的物件。 |
GetHashCode() |
傳回這個執行個體的雜湊碼。 |
運算子
Equality(CharacterRange, CharacterRange) |
比較兩個 CharacterRange 物件。 取得值,指出兩個 First 物件的 Length 和 CharacterRange 值是否相等。 |
Inequality(CharacterRange, CharacterRange) |
比較兩個 CharacterRange 物件。 取得值,指出兩個 First 物件的 Length 或 CharacterRange 值是否不相等。 |