The following example defines three ranges of character positions within a string and sets those ranges in a
StringFormat object. Next, the
GdipMeasureCharacterRanges function is used to get the three regions of the display that are occupied by the characters that are specified by the ranges. This is done for three different layout rectangles to show how the regions change according to the layout of the string. Also, on the third repetition, the string format flags are changed so that the regions measured will include trailing spaces.
C++
VOID SetMeasCharRanges(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 ranges of character positions within the string
CharacterRange charRanges[3] = { CharacterRange(3, 5),
CharacterRange(15, 2),
CharacterRange(30, 15), };
// Font and string format used 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);
}
}
PowerBASIC
SUB GDIP_SetStringFormatMeasurableCharacterRanges (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pBlueBrush AS DWORD
LOCAL pRedBrush AS DWORD
LOCAL pBlackPen AS DWORD
LOCAL pFont AS DWORD
LOCAL pFontFamily AS DWORD
LOCAL pStringFormat AS DWORD
LOCAL strFontName AS STRING
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Brushes and pens used for drawing and painting
hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 255), pBlueBrush)
hStatus = GdipCreateSolidFill(GDIP_ARGB(100, 255, 0, 0), pRedBrush)
hStatus = GdipCreatePen1(GDIP_ARGB(255, 0, 0, 0), 1, %UnitPixel, pBlackPen)
' // Layout rectangles used for drawing strings
LOCAL layoutRect_A AS RECTF
LOCAL layoutRect_B AS RECTF
LOCAL layoutRect_C AS RECTF
layoutRect_A.x = 20.0! : layoutRect_A.y = 20.0! : layoutRect_A.Width = 130.0! : layoutRect_A.Height = 130.0!
layoutRect_B.x = 160.0! : layoutRect_B.y = 20.0! : layoutRect_B.Width = 165.0! : layoutRect_B.Height = 130.0!
layoutRect_C.x = 335.0! : layoutRect_C.y = 20.0! : layoutRect_C.Width = 165.0! : layoutRect_C.Height = 130.0!
' // Three ranges of character positions within the string
DIM charRanges(2) AS CharacterRange
charRanges(0).First = 3 : charRanges(0).Length = 5
charRanges(1).First = 15 : charRanges(1).Length = 2
charRanges(2).First = 30 : charRanges(2).Length = 15
' // Font and string format used to apply to string when drawing
strFontName = UCODE$("Times New Roman")
hStatus = GdipCreateFontFamilyFromName(STRPTR(strFontName), %NULL, pFontFamily)
IF hStatus = %StatusOk AND pFontFamily <> %NULL THEN
hStatus = GdipCreateFont(pFontFamily, 16.0!, %FontStyleRegular, %UnitPoint, pFont)
GdipDeleteFontFamily(pFontFamily)
END IF
' Note: You can use the wrapper function GdiPlusCreateFontFromName to create the font:
' hStatus = GdiPlusCreateFontFromName("Times New Roman"", 16.0!, %FontStyleRegular, %UnitPoint, pFont)
hStatus = GdipCreateStringFormat(0, %LANG_NEUTRAL, pStringFormat)
' // Other variables
DIM rgCharRangeRegions(0) AS DWORD ' // array of CharacterRange regions
LOCAL i AS LONG ' // loop counter
LOCAL count AS LONG ' // number of character ranges set
LOCAL strText AS STRING
strText = UCODE$("The quick, brown fox easily jumps over the lazy dog.")
' // Set three ranges of character positions.
hStatus = GdipSetStringFormatMeasurableCharacterRanges(pStringFormat, 3, charRanges(0))
' // Get the number of ranges that have been set, and allocate memory to
' // store the regions that correspond to the ranges.
hStatus = GdipGetStringFormatMeasurableCharacterRangeCount(pStringFormat, count)
REDIM rgCharRangeRegions(count - 1)
FOR i = 0 TO count - 1
hStatus = GdipCreateRegion(rgCharRangeRegions(i))
NEXT
' // 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.
hStatus = GdipMeasureCharacterRanges(pGraphics, STRPTR(strText), -1, _
pFont, layoutRect_A, pStringFormat, count, rgCharRangeRegions(0))
hStatus = GdipDrawString(pGraphics, STRPTR(strText), -1, _
pFont, layoutRect_A, pStringFormat, pBlueBrush)
hStatus = GdipDrawRectangle(pGraphics, pBlackPen, layoutRect_A.x, layoutRect_A.y, layoutRect_A.Width, layoutRect_A.Height)
FOR i = 0 TO count - 1
hStatus = GdipFillRegion(pGraphics, pRedBrush, rgCharRangeRegions(i))
NEXT
' // 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.
hStatus = GdipMeasureCharacterRanges(pGraphics, STRPTR(strText), -1, _
pFont, layoutRect_B, pStringFormat, count, rgCharRangeRegions(0))
hStatus = GdipDrawString(pGraphics, STRPTR(strText), -1, _
pFont, layoutRect_B, pStringFormat, pBlueBrush)
hStatus = GdipDrawRectangle(pGraphics, pBlackPen, layoutRect_B.x, layoutRect_B.y, layoutRect_B.Width, layoutRect_B.Height)
FOR i = 0 TO count - 1
hStatus = GdipFillRegion(pGraphics, pRedBrush, rgCharRangeRegions(i))
NEXT
' // 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.
hStatus = GdipGetStringFormatFlags(pStringFormat, %StringFormatFlagsMeasureTrailingSpaces)
hStatus = GdipMeasureCharacterRanges(pGraphics, STRPTR(strText), -1, _
pFont, layoutRect_C, pStringFormat, count, rgCharRangeRegions(0))
hStatus = GdipDrawString(pGraphics, STRPTR(strText), -1, _
pFont, layoutRect_C, pStringFormat, pBlueBrush)
hStatus = GdipDrawRectangle(pGraphics, pBlackPen, layoutRect_C.x, layoutRect_C.y, layoutRect_C.Width, layoutRect_C.Height)
FOR i = 0 TO count - 1
hStatus = GdipFillRegion(pGraphics, pRedBrush, rgCharRangeRegions(i))
NEXT
FOR i = 0 TO count - 1
IF rgCharRangeRegions(i) THEN GdipDeleteRegion(rgCharRangeRegions(i))
NEXT
' // Cleanup
IF pFont THEN GdipDeleteFont(pFont)
IF pStringFormat THEN GdipDeleteStringFormat(pStringFormat)
IF pBlackPen THEN GdipDeletePen(pBlackPen)
IF pBlueBrush THEN GdipDeleteBrush(pBlueBrush)
IF pRedBrush THEN GdipDeleteBrush(pRedBrush)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
The following illustration shows the output of the preceding code.
(http://www.jose.it-berater.org/captures/GdipSetStringFormatMeasurableCharacterRanges.png)