StringFormat.SetMeasurableCharacterRanges(CharacterRange[]) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
指定 CharacterRange 結構的陣列,代表由 MeasureCharacterRanges(String, Font, RectangleF, StringFormat) 方法的呼叫所測量的字元範圍。
public:
void SetMeasurableCharacterRanges(cli::array <System::Drawing::CharacterRange> ^ ranges);
public void SetMeasurableCharacterRanges (System.Drawing.CharacterRange[] ranges);
member this.SetMeasurableCharacterRanges : System.Drawing.CharacterRange[] -> unit
Public Sub SetMeasurableCharacterRanges (ranges As CharacterRange())
參數
- ranges
- CharacterRange[]
CharacterRange 結構的陣列,指定由 MeasureCharacterRanges(String, Font, RectangleF, StringFormat) 方法的呼叫所測量的字元範圍。
例外狀況
設定 32 個以上的字元範圍。
範例
下列範例是專為搭配Windows Forms使用而設計,而且需要 PaintEventArgse
,這是事件處理常式的參數 Paint 。 此程式碼會執行下列動作:
設定 的 StringFormat 字元範圍。
測量指定字串和版面配置矩形的字元範圍。
繪製字串和版面配置矩形。
繪製區域。 每個 Region 都會指定由一系列字元佔用的區域。 當方法測量 MeasureCharacterRanges 字元範圍時,會設定區域中的值。
重複前四個步驟,但在每個字元範圍的度量中包含尾端空格。
清除 的格式 StringFormat 旗標,讓尾端空格不會包含在每個字元範圍的度量中。
重複前四個步驟,但只使用不同的版面配置矩形來示範版面配置矩形會影響字元範圍的度量。 字型的大小也會影響度量。
void SetMeasCharRangesExample( PaintEventArgs^ e )
{
Graphics^ g = e->Graphics;
SolidBrush^ redBrush = gcnew SolidBrush( Color::FromArgb( 50, 255, 0, 0 ) );
// Layout rectangles, font, and string format used for displaying string.
Rectangle layoutRectA = Rectangle(20,20,165,80);
Rectangle layoutRectB = Rectangle(20,110,165,80);
Rectangle layoutRectC = Rectangle(20,200,240,80);
System::Drawing::Font^ tnrFont = gcnew System::Drawing::Font( "Times New Roman",16 );
StringFormat^ strFormat = gcnew StringFormat;
// Ranges of character positions within a string.
array<CharacterRange>^ charRanges = {CharacterRange(3,5),CharacterRange(15,2),CharacterRange(30,15)};
// Each region specifies the area occupied by the characters within a
// range of positions. the values are obtained by using a method that
// measures the character ranges.
array<System::Drawing::Region^>^charRegions = gcnew array<System::Drawing::Region^>(charRanges->Length);
// String to be displayed.
String^ str = "The quick, brown fox easily jumps over the lazy dog.";
// Set the char ranges for the string format.
strFormat->SetMeasurableCharacterRanges( charRanges );
// loop counter (unsigned 8-bit integer)
Byte i;
// Measure the char ranges for a given string and layout rectangle. Each
// area occupied by the characters in a range is stored as a region. Then
// draw the string and layout rectangle, and paint the regions.
charRegions = g->MeasureCharacterRanges( str, tnrFont, layoutRectA, strFormat );
g->DrawString( str, tnrFont, Brushes::Blue, layoutRectA, strFormat );
g->DrawRectangle( Pens::Black, layoutRectA );
// Paint the regions.
for ( i = 0; i < charRegions->Length; i++ )
g->FillRegion( redBrush, charRegions[ i ] );
// Repeat the above steps, but include trailing spaces in the char
// range measurement by setting the appropriate string format flag.
strFormat->FormatFlags = StringFormatFlags::MeasureTrailingSpaces;
charRegions = g->MeasureCharacterRanges( str, tnrFont, layoutRectB, strFormat );
g->DrawString( str, tnrFont, Brushes::Blue, layoutRectB, strFormat );
g->DrawRectangle( Pens::Black, layoutRectB );
for ( i = 0; i < charRegions->Length; i++ )
g->FillRegion( redBrush, charRegions[ i ] );
// Clear all the format flags.
strFormat->FormatFlags = StringFormatFlags(0);
// Repeat the steps, but use a different layout rectangle. the dimensions
// of the layout rectangle and the size of the font both affect the
// character range measurement.
charRegions = g->MeasureCharacterRanges( str, tnrFont, layoutRectC, strFormat );
g->DrawString( str, tnrFont, Brushes::Blue, layoutRectC, strFormat );
g->DrawRectangle( Pens::Black, layoutRectC );
// Paint the regions.
for ( i = 0; i < charRegions->Length; i++ )
g->FillRegion( redBrush, charRegions[ i ] );
}
public void SetMeasCharRangesExample(PaintEventArgs e)
{
Graphics g = e.Graphics;
SolidBrush redBrush = new SolidBrush(Color.FromArgb(50, 255, 0, 0));
// Layout rectangles, font, and string format used for displaying string.
Rectangle layoutRectA = new Rectangle(20, 20, 165, 80);
Rectangle layoutRectB = new Rectangle(20, 110, 165, 80);
Rectangle layoutRectC = new Rectangle(20, 200, 240, 80);
Font tnrFont = new Font("Times New Roman", 16);
StringFormat strFormat = new StringFormat();
// Ranges of character positions within a string.
CharacterRange[] charRanges = { new CharacterRange(3, 5),
new CharacterRange(15, 2), new CharacterRange(30, 15)};
// Each region specifies the area occupied by the characters within a
// range of positions. the values are obtained by using a method that
// measures the character ranges.
Region[] charRegions = new Region[charRanges.Length];
// String to be displayed.
string str =
"The quick, brown fox easily jumps over the lazy dog.";
// Set the char ranges for the string format.
strFormat.SetMeasurableCharacterRanges(charRanges);
// loop counter (unsigned 8-bit integer)
byte i;
// Measure the char ranges for a given string and layout rectangle. Each
// area occupied by the characters in a range is stored as a region. Then
// draw the string and layout rectangle, and paint the regions.
charRegions = g.MeasureCharacterRanges(str, tnrFont,
layoutRectA, strFormat);
g.DrawString(str, tnrFont, Brushes.Blue, layoutRectA, strFormat);
g.DrawRectangle(Pens.Black, layoutRectA);
// Paint the regions.
for (i = 0; i < charRegions.Length; i++)
g.FillRegion(redBrush, charRegions[i]);
// Repeat the above steps, but include trailing spaces in the char
// range measurement by setting the appropriate string format flag.
strFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
charRegions = g.MeasureCharacterRanges(str, tnrFont,
layoutRectB, strFormat);
g.DrawString(str, tnrFont, Brushes.Blue, layoutRectB, strFormat);
g.DrawRectangle(Pens.Black, layoutRectB);
for (i = 0; i < charRegions.Length; i++)
g.FillRegion(redBrush, charRegions[i]);
// Clear all the format flags.
strFormat.FormatFlags = 0;
// Repeat the steps, but use a different layout rectangle. the dimensions
// of the layout rectangle and the size of the font both affect the
// character range measurement.
charRegions = g.MeasureCharacterRanges(str, tnrFont,
layoutRectC, strFormat);
g.DrawString(str, tnrFont, Brushes.Blue, layoutRectC, strFormat);
g.DrawRectangle(Pens.Black, layoutRectC);
// Paint the regions.
for (i = 0; i < charRegions.Length; i++)
g.FillRegion(redBrush, charRegions[i]);
}
Public Sub SetMeasCharRangesExample(ByVal e As PaintEventArgs)
Dim g As Graphics = e.Graphics
Dim redBrush As New SolidBrush(Color.FromArgb(50, 255, 0, 0))
' Layout rectangles, font, and string format used for
' displaying string.
Dim layoutRectA As New Rectangle(20, 20, 165, 80)
Dim layoutRectB As New Rectangle(20, 110, 165, 80)
Dim layoutRectC As New Rectangle(20, 200, 240, 80)
Dim tnrFont As New Font("Times New Roman", 16)
Dim strFormat As New StringFormat
' Ranges of character positions within a string.
Dim charRanges As CharacterRange() = {New CharacterRange(3, 5), _
New CharacterRange(15, 2), New CharacterRange(30, 15)}
' Each region specifies the area occupied by the characters within
' a range of positions. The values are obtained by using a method
' that measures the character ranges.
Dim charRegions(charRanges.Length) As [Region]
' String to be displayed.
Dim str As String = _
"The quick, brown fox easily jumps over the lazy dog."
' Set the char ranges for the string format.
strFormat.SetMeasurableCharacterRanges(charRanges)
' Loop counter (unsigned 8-bit integer).
Dim i As Byte
' Measure the char ranges for a given string and layout rectangle.
' Each area occupied by the characters in a range is stored as a
' region. then draw the string and layout rectangle and paint the
' regions.
charRegions = g.MeasureCharacterRanges(str, tnrFont, _
RectangleF.op_Implicit(layoutRectA), strFormat)
g.DrawString(str, tnrFont, Brushes.Blue, _
RectangleF.op_Implicit(layoutRectA), strFormat)
g.DrawRectangle(Pens.Black, layoutRectA)
' Paint the regions.
For i = 0 To charRegions.Length - 1
g.FillRegion(redBrush, charRegions(i))
Next i
' Repeat the above steps, but include trailing spaces in the char
' range measurement by setting the appropriate string format flag.
strFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces
charRegions = g.MeasureCharacterRanges(str, tnrFont, _
RectangleF.op_Implicit(layoutRectB), strFormat)
g.DrawString(str, tnrFont, Brushes.Blue, _
RectangleF.op_Implicit(layoutRectB), strFormat)
g.DrawRectangle(Pens.Black, layoutRectB)
' Paint the regions.
For i = 0 To charRegions.Length - 1
g.FillRegion(redBrush, charRegions(i))
Next i
' Clear all the format flags.
strFormat.FormatFlags = 0
' Repeat the steps, but use a different layout rectangle. The
' dimensions of the layout rectangle and the size of the font both
' affect the character range measurement.
charRegions = g.MeasureCharacterRanges(str, tnrFont, _
RectangleF.op_Implicit(layoutRectC), strFormat)
g.DrawString(str, tnrFont, Brushes.Blue, _
RectangleF.op_Implicit(layoutRectC), strFormat)
g.DrawRectangle(Pens.Black, layoutRectC)
' Paint the regions.
For i = 0 To charRegions.Length - 1
g.FillRegion(redBrush, charRegions(i))
Next i
End Sub
備註
不允許設定超過 32 個字元的範圍,而且會導致 System.OverflowException 。
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應