• Welcome to Powerbasic Museum 2020-B.
 

News:

Forum in repository mode. No new members allowed.

Main Menu

GDI+: GdipGetStringFormatTrimming

Started by José Roca, July 01, 2008, 05:37:29 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates a StringFormat object, sets the string's trimming style, and then gets the trimming style and stores it in a variable. The code then creates a second StringFormat object and uses the stored trimming style to set the trimming style of the second StringFormat object. Next, the code uses the second StringFormat object to draw a formatted string. The code also draws the string's layout rectangle.

C++


VOID Example_GetTrimming(HDC hdc)
{
   Graphics graphics(hdc);

   SolidBrush  solidBrush(Color(255, 255, 0, 0));
   FontFamily  fontFamily(L"Times New Roman");
   Font        font(&fontFamily, 24, FontStyleRegular, UnitPixel);
   
   // Create a StringFormat object, and set its trimming style.
   StringFormat stringFormat;
   stringFormat.SetTrimming(StringTrimmingWord);

   // Get the trimming setting from the StringFormat object.
   StringTrimming stringTrimming = stringFormat.GetTrimming();

   // Create a second StringFormat object with the same trimming style.
   StringFormat stringFormat2;
   stringFormat2.SetTrimming(stringTrimming);

   // Use the second StringFormat object in a call to DrawString.
   graphics.DrawString(
      L"One two three four five six seven eight nine ten",
      47,  // string length
      &font,
      RectF(30, 30, 160, 60),
      &stringFormat2,
      &solidBrush);

   // Draw the rectangle that encloses the text.
   Pen pen(Color(255, 255, 0, 0));
   graphics.DrawRectangle(&pen, 30, 30, 160, 60);
}


PowerBASIC


SUB GDIP_GetStringFormatTrimming (BYVAL hdc AS DWORD)

   LOCAL hStatus AS LONG
   LOCAL pGraphics AS DWORD
   LOCAL pPen AS DWORD
   LOCAL pFont AS DWORD
   LOCAL pFontFamily AS DWORD
   LOCAL pStringFormat AS DWORD
   LOCAL pStringFormat2 AS DWORD
   LOCAL pSolidBrush AS DWORD
   LOCAL stringTrimming AS LONG
   LOCAL strFontName AS STRING
   LOCAL strText AS STRING
   LOCAL rcf AS RECTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Create a solid brush
   hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pSolidBrush)

   ' // Create the font
   strFontName = UCODE$("Times New Roman")
   hStatus = GdipCreateFontFamilyFromName(STRPTR(strFontName), %NULL, pFontFamily)
   IF hStatus = %StatusOk AND pFontFamily <> %NULL THEN
      hStatus = GdipCreateFont(pFontFamily, 24, %FontStyleRegular, %UnitPoint, pFont)
      GdipDeleteFontFamily(pFontFamily)
   END IF

   ' Note: You can use the wrapper function GdiPlusCreateFontFromName to create the font:
   ' hStatus = GdiPlusCreateFontFromName("Times New Roman", 24, %FontStyleRegular, %UnitPoint, pFont)

   ' // Create a StringFormat object, and set its trimming style.
   hStatus = GdipCreateStringFormat(0, %LANG_NEUTRAL, pStringFormat)
   hStatus = GdipSetStringFormatTrimming(pStringFormat, %StringTrimmingWord)

   ' // Get the trimming setting from the StringFormat object.
   hStatus = GdipGetStringFormatTrimming(pStringFormat, stringTrimming)

   ' // Create a second StringFormat object with the same trimming style.
   hStatus = GdipCreateStringFormat(0, %LANG_NEUTRAL, pStringFormat2)
   hStatus = GdipSetStringFormatTrimming(pStringFormat, stringTrimming)

   ' // Use the second StringFormat object in a call to DrawString.
   rcf.x = 30 : rcf.y = 30 : rcf.Width = 160 : rcf.Height = 60
   strText = UCODE$("One two three four five six seven eight nine ten")
   hStatus = GdipDrawString(pGraphics, STRPTR(strText), LEN(strText) \ 2, pFont, rcf, pStringFormat2, pSolidBrush)

   ' // Draw the rectangle that encloses the text.
   hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitPixel, pPen)
   hStatus = GdipDrawRectangle(pGraphics, pPen, rcf.x, rcf.y, rcf.Width, rcf.Height)

   ' // Cleanup
   IF pFont THEN GdipDeleteFont(pFont)
   IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
   IF pPen THEN GdipDeletePen(pPen)
   IF pStringFormat2 THEN GdipDeleteStringFormat(pStringFormat2)
   IF pStringFormat THEN GdipDeleteStringFormat(pStringFormat)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB


The following illustration shows the output of the preceding code.