Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 8 GDI Orientation and Transformations Visual C++ and MFC Fundamentals


7.2.3 Drawing Text........................................................................................


By default, the CDC class is able to draw text using a font pre-selected, known as the
System Font. To draw text, you can use the CDC::TextOut() method. Its syntax is:

virtual BOOL TextOut(int x, int y, LPCTSTR lpszString, int nCount);

To us this method, you must specify where the text would start. This location is
determined from the (0, 0) origin to the right (x) and to the bottom (y). The text to display
is the lpszString. The nCount value is the length of the text. Here is an example:
void CExoView::OnDraw(CDC* pDC)
{
CExoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

pDC->TextOut(50, 42, "Johnny Carson", 13);
}

If you want to control the color used to draw the text, use the CDC::SetTextColor()
method whose syntax is:

virtual COLORREF SetTextColor(COLORREF crColor);

The argument can be provided as a COLORREF variable or by calling the RGB macro. is
an example:
Free download pdf