CharacterRange Struktur
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Menentukan rentang posisi karakter dalam string.
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)
- Warisan
- Penerapan
Contoh
Contoh kode berikut menunjukkan cara membuat CharacterRange dan menggunakannya untuk menyoroti bagian dari string. Contoh ini dirancang untuk digunakan dengan Formulir Windows. Tempelkan contoh ke dalam formulir dan panggil HighlightACharacterRange
metode saat menangani peristiwa formulir Paint , meneruskan e
sebagai PaintEventArgs.
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
Konstruktor
CharacterRange(Int32, Int32) |
Menginisialisasi instans CharacterRange baru struktur, menentukan rentang posisi karakter dalam string. |
Properti
First |
Mendapatkan atau mengatur posisi dalam string karakter pertama ini CharacterRange. |
Length |
Mendapatkan atau mengatur jumlah posisi dalam ini CharacterRange. |
Metode
Equals(CharacterRange) |
Menunjukkan apakah instans saat ini sama dengan instans lain dengan jenis yang sama. |
Equals(Object) |
Mendapatkan nilai yang menunjukkan apakah objek ini setara dengan objek yang ditentukan. |
GetHashCode() |
Mengembalikan kode hash untuk instans ini. |
Operator
Equality(CharacterRange, CharacterRange) |
Membandingkan dua CharacterRange objek. Mendapatkan nilai yang menunjukkan apakah First nilai dan Length dari dua CharacterRange objek sama. |
Inequality(CharacterRange, CharacterRange) |
Membandingkan dua CharacterRange objek. Mendapatkan nilai yang menunjukkan apakah First nilai atau Length dari dua CharacterRange objek tidak sama. |