Graphics::MeasureCharacterRanges 方法 (gdiplusgraphics.h)
Graphics::MeasureCharacterRanges方法會取得一組區域,每個區域都會系結字串內的字元位置範圍。
語法
Status MeasureCharacterRanges(
[in] const WCHAR *string,
[in] INT length,
[in] const Font *font,
[in, ref] const RectF & layoutRect,
[in] const StringFormat *stringFormat,
[in] INT regionCount,
[out] Region *regions
);
參數
[in] string
類型: const WCHAR*
寬字元字串的指標。
[in] length
類型: INT
指定 字串 陣列中字元數的整數。 如果 字串 參數指向 Null終止的字串,這個參數可以設定為 –1。
[in] font
類型:const字型*
Font物件的指標,指定要套用至字串之字型) 系列名稱、大小和樣式 (字型特性。
[in, ref] layoutRect
類型: const Rectf
系結字串的矩形參考。
[in] stringFormat
類型:const StringFormat*
StringFormat物件的指標,指定字元範圍和版面配置資訊,例如對齊、修剪、定位停駐點等等。
[in] regionCount
類型: INT
整數,指定預期要接收到 區域 陣列的區域數目。 這個數位應該等於 StringFormat 物件中目前字元範圍的數目。
[out] regions
類型: 區域*
接收區域之 Region 物件的陣列指標,每個物件都會系結一系列文字。
傳回值
類型: 狀態
如果方法成功,它會傳回 Ok,這是 Status 列舉的元素。
如果方法失敗,它會傳回 Status 列舉的其中一個其他元素。
備註
字元範圍是文字字串內的字元位置範圍。 由字元範圍所指定的字元群組所佔用的顯示器區域是周框區域。 字元範圍是由 SetMeasurableCharacterRanges所設定。 目前設定的範圍數目可藉由呼叫 GetMeasurableCharacterRangeCount來決定。 此數位也是 MeasureCharacterRanges 方法預期取得的區域數目。
範例
下列範例會定義字串內的三個字元位置範圍,並在 StringFormat 物件中設定這些範圍。 接下來, MeasureCharacterRanges 方法可用來取得範圍所指定字元所佔用的顯示三個區域。 這適用于三個不同的版面配置矩形,以顯示區域如何根據字串的配置而變更。 此外,在此第三次重複時,字串格式旗標會變更,以便測量的區域會包含尾端空格。
VOID MeasureCharRanges(HDC hdc)
{
Graphics graphics(hdc);
// Brushes and pens used for drawing and painting
SolidBrush blueBrush(Color(255, 0, 0, 255));
SolidBrush redBrush(Color(100, 255, 0, 0));
Pen blackPen(Color(255, 0, 0, 0));
// Layout rectangles used for drawing strings
RectF layoutRect_A(20.0f, 20.0f, 130.0f, 130.0f);
RectF layoutRect_B(160.0f, 20.0f, 165.0f, 130.0f);
RectF layoutRect_C(335.0f, 20.0f, 165.0f, 130.0f);
// Three different ranges of character positions within the string
CharacterRange charRanges[3] = { CharacterRange(3, 5),
CharacterRange(15, 2),
CharacterRange(30, 15), };
// Font and string format to apply to string when drawing
Font myFont(L"Times New Roman", 16.0f);
StringFormat strFormat;
// Other variables
Region* pCharRangeRegions; // pointer to CharacterRange regions
short i; // loop counter
INT count; // number of character ranges set
WCHAR string[] = L"The quick, brown fox easily jumps over the lazy dog.";
// Set three ranges of character positions.
strFormat.SetMeasurableCharacterRanges(3, charRanges);
// Get the number of ranges that have been set, and allocate memory to
// store the regions that correspond to the ranges.
count = strFormat.GetMeasurableCharacterRangeCount();
pCharRangeRegions = new Region[count];
// Get the regions that correspond to the ranges within the string when
// layout rectangle A is used. Then draw the string, and show the regions.
graphics.MeasureCharacterRanges(string, -1,
&myFont, layoutRect_A, &strFormat, count, pCharRangeRegions);
graphics.DrawString(string, -1,
&myFont, layoutRect_A, &strFormat, &blueBrush);
graphics.DrawRectangle(&blackPen, layoutRect_A);
for ( i = 0; i < count; i++)
{
graphics.FillRegion(&redBrush, pCharRangeRegions + i);
}
// Get the regions that correspond to the ranges within the string when
// layout rectangle B is used. Then draw the string, and show the regions.
graphics.MeasureCharacterRanges(string, -1,
&myFont, layoutRect_B, &strFormat, count, pCharRangeRegions);
graphics.DrawString(string, -1,
&myFont, layoutRect_B, &strFormat, &blueBrush);
graphics.DrawRectangle(&blackPen, layoutRect_B);
for ( i = 0; i < count; i++)
{
graphics.FillRegion(&redBrush, pCharRangeRegions + i);
}
// Get the regions that correspond to the ranges within the string when
// layout rectangle C is used. Set trailing spaces to be included in the
// regions. Then draw the string, and show the regions.
strFormat.SetFormatFlags(StringFormatFlagsMeasureTrailingSpaces);
graphics.MeasureCharacterRanges(string, -1,
&myFont, layoutRect_C, &strFormat, count, pCharRangeRegions);
graphics.DrawString(string, -1,
&myFont, layoutRect_C, &strFormat, &blueBrush);
graphics.DrawRectangle(&blackPen, layoutRect_C);
for ( i = 0; i < count; i++)
{
graphics.FillRegion(&redBrush, pCharRangeRegions + i);
}
// Delete memory for the range regions.
delete [] pCharRangeRegions;
}
需求
最低支援的用戶端 | Windows XP、Windows 2000 Professional [僅限傳統型應用程式] |
最低支援的伺服器 | Windows 2000 Server [僅限傳統型應用程式] |
目標平台 | Windows |
標頭 | gdiplusgraphics.h (包含 Gdiplus.h) |
程式庫 | Gdiplus.lib |
Dll | Gdiplus.dll |