Printing Text

two ways to print text:

·       TextOut() – simple, no formatting or processing ability

·       DrawText() – complex, justification, clipping, etc.

 

BOOL TextOut( HDC hdc,               // device context

                            int     nXStart,        // starting x pos

                            int     nYStart,        // stating y pos

                            LPCSTR lpString, // str to print

                            int cbString);         // # of chars in str

 

you need to use sprintf() to format your string.

 

len = sprintf(buff, “High Score = %d“, (int)hScore);

TextOut(hdc, 0,0, buff, len); 

 

int DrawText( HDC  hdc,            // device context

                        LPCSTR lpString, // str to print

                        int   nCount,          // # chars

                        LPRECT lpRect,   // clipping rect

                        UNIT uFormat);   // fmt flags

 

Color

set the background mode of the text

int SetBkMode(HDC hdc, int iBkMode);

      where iBkMode = TRANSPARENT/OPAQUE

 

set foreground & background colors

COLORREF SetTextColor(HDC hdc, COLORREF crColor);

      where: crColor is the text color

 

COLORREF SetBkColor(HDC hdc, COLORREF crColor);

      where: crColor is the background color

 

both return the current color before the change, use to restore after you’re done printing.

 

hdc = GetDC(hwnd);

SetBkMode(hdc, TRANSPARENT);

oldTxColor = SetTextColor(hdc, RGB(255,0,0));

 

   // print your text

   TextOut(hdc, 100,10,  “Press <ESC> to Quit”, 19);

   SetBkMode(hdc, OPAQUE);

   SetTextColor(hdc, oldTextColor));

   ReleaseDC(hwnd, hdc);