• Welcome to Powerbasic Museum 2020-B.
 

GDI+: GdipGetStringFormatMeasurableCharacterRangeCount

Started by José Roca, July 01, 2008, 12:27:17 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example defines three ranges of character positions within a string and sets those ranges in a StringFormat object. Next, the GdipGetMeasurableCharacterRangeCount function is used to get the number of character ranges that are currently set in the StringFormat object. This number is then used to allocate a buffer large enough to store the regions that correspond with the ranges. Then, 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.

C++


VOID GetMeasCharRangeCount(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 rectangle used for drawing string
   RectF   layoutRect(20.0f, 20.0f, 130.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.
   // Then draw the string and show the regions.
   graphics.MeasureCharacterRanges(string, -1,
      &myFont, layoutRect, &strFormat, count, pCharRangeRegions);
   graphics.DrawString(string, -1,
      &myFont, layoutRect, &strFormat, &blueBrush);
   graphics.DrawRectangle(&blackPen, layoutRect);
   for ( i = 0; i < count; i++)
   {
      graphics.FillRegion(&redBrush, pCharRangeRegions + i);
   }
}


PowerBASIC


SUB GDIP_GetStringFormatMeasurableCharacterRangeCount (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
   LOCAL layoutRect AS RECTF

   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)

   ' // 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)

   ' // Layout rectangle used for drawing string
   layoutRect.x = 20.0! : layoutRect.y = 20.0! : layoutRect.Width = 130.0! : layoutRect.Height = 130.0!

   ' // 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.
   ' // Then draw the string and show the regions.
   hStatus = GdipMeasureCharacterRanges(pGraphics, STRPTR(strText), -1, _
      pFont, layoutRect, pStringFormat, count, rgCharRangeRegions(0))

   hStatus = GdipDrawString(pGraphics, STRPTR(strText), -1, _
      pFont, layoutRect, pStringFormat, pBlueBrush)
   hStatus = GdipDrawRectangle(pGraphics, pBlackPen, layoutRect.x, layoutRect.y, layoutRect.Width, layoutRect.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.