• Welcome to Jose's Read Only Forum 2023.
 

GDI+: GdipSetTextRenderingHint

Started by José Roca, June 22, 2008, 03:38:19 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

José Roca



The following example sets the text rendering hint to two different values and draws text to demonstrate each value.

C++


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

   // Set the text rendering hint to TextRenderingHintSingleBitPerPixel.
   graphics.SetTextRenderingHint(TextRenderingHintSingleBitPerPixel);

   // Draw text.
   graphics.DrawString(
   L"Low quality rendering",
       21,
   &Font(L"Arial", 24),
   PointF(0, 0),
   &SolidBrush(Color(255, 0, 0, 0)));

   // Get the text rendering hint.
   TextRenderingHint hint = graphics.GetTextRenderingHint();

   // Set the text rendering hint to TextRenderingHintAntiAlias.
   graphics.SetTextRenderingHint(TextRenderingHintAntiAlias);

   // Draw more text to demonstrate the difference.
   graphics.DrawString(
   L"High quality rendering",
       22,
   &Font(L"Arial", 24),
   PointF(0, 50),
   &SolidBrush(Color(255, 0, 0, 0)));
}


PowerBASIC


SUB GDIP_SetTextRenderingHint (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 pFormat AS DWORD
   LOCAL pBrush AS DWORD
   LOCAL strFontName AS STRING
   LOCAL strText AS STRING
   LOCAL rcf AS RECTF

   hStatus = GdipCreateFromHDC(hdc, pGraphics)

   ' // Set the text rendering hint to TextRenderingHintSingleBitPerPixel.
   hStatus = GdipSetTextRenderingHint(pGraphics, %TextRenderingHintSingleBitPerPixel)

   ' // Create the font
   strFontName = UCODE$("Arial")
   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("Arial", 24, %FontStyleRegular, %UnitPoint, pFont)

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

   ' // Draw text
   strText = UCODE$("Low quality rendering")
   hStatus = GdipDrawString(pGraphics, STRPTR(strText), LEN(strText) \ 2, pFont, rcf, pFormat, pBrush)

   ' // Set the text rendering hint to TextRenderingHintAntiAlias.
   hStatus = GdipSetTextRenderingHint(pGraphics, %TextRenderingHintAntiAlias)

   ' // Draw more text to demonstrate the difference.
   strText = UCODE$("High quality rendering")
   rcf.y = 50
   hStatus = GdipDrawString(pGraphics, STRPTR(strText), LEN(strText) \ 2, pFont, rcf, pFormat, pBrush)

   ' // Cleanup
   IF pFont THEN GdipDeleteFont(pFont)
   IF pPen THEN GdipDeletePen(pPen)
   IF pBrush THEN GdipDeleteBrush(pBrush)
   IF pFormat THEN GdipDeleteStringFormat(pFormat)
   IF pGraphics THEN GdipDeleteGraphics(pGraphics)

END SUB