• Welcome to Powerbasic Museum 2020-B.
 

GDI+: GdipStringFormatGetGenericTypographic

Started by José Roca, June 30, 2008, 05:56:41 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example creates a generic, typographic StringFormat object and then uses it to draw a formatted string. The code also draws the string's layout rectangle.

C++


VOID Example_GenericTypographic(HDC hdc)
{
   Graphics graphics(hdc);
   SolidBrush  solidBrush(Color(255, 255, 0, 0));
   FontFamily  fontFamily(L"Times New Roman");
   Font        font(&fontFamily, 12, FontStyleRegular, UnitPoint);
   
   // Create a generic typographic StringFormat object.
   const StringFormat* pStringFormat = StringFormat::GenericTypographic();
   // Use the generic typographic StringFormat object
   // in a call to DrawString.
  graphics.DrawString(
      L"Formatted by a generic typographic StringFormat object",
      54,  // string length
      &font,
      RectF(30, 30, 100, 120),
      pStringFormat,
      &solidBrush);
   // Draw the rectangle that encloses the text.
   Pen pen(Color(255, 255, 0, 0));
   graphics.DrawRectangle(&pen, 30, 30, 100, 120);
}


PowerBASIC


SUB GDIP_StringFormatGetGenericTypographic (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 pSolidBrush AS DWORD
   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, 12, %FontStyleRegular, %UnitPoint, pFont)
      GdipDeleteFontFamily(pFontFamily)
   END IF

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

   ' // Create a generic typographic StringFormat object.
   hStatus = GdipStringFormatGetGenericTypographic(pStringFormat)

   ' // Use the generic typographic StringFormat object in a call to DrawString.
   rcf.x = 30 : rcf.y = 30 : rcf.Width = 100 : rcf.Height = 120
   strText = UCODE$("This text was formatted by a generic typographic StringFormat object.")
   hStatus = GdipDrawString(pGraphics, STRPTR(strText), LEN(strText) \ 2, pFont, rcf, pStringFormat, 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 pStringFormat THEN GdipDeleteStringFormat(pStringFormat)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB


The following illustration shows the output of the preceding code.