Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


void CExoView::OnDraw(CDC* pDC)
{
CExoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

pDC->SetTextColor(RGB(255, 25, 2));
pDC->TextOut(50, 42, "Johnny Carson", 13);
}

As you will learn from now on concerning the device context, once you change one of its
characteristics, it stays there until you change it again. It is similar to picking a spoon and
start eating. As long as you are eating, you can use only the spoon and only that spoon. It
you want to cut the meat, you must replace the spoon in your hand with a knife. In the
same way, if you change the color of text and draw more than one line of text, all of them
would use the same color. If you want to use a different color, you must change the color
used by calling the SetTextColor() method again. Here is an example:

void CExoView::OnDraw(CDC* pDC)
{
CExoDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);

pDC->SetTextColor(RGB(255, 25, 2));
pDC->TextOut(50, 42, "Johnny Carson", 13);

pDC->SetTextColor(RGB(12, 25, 255));
pDC->TextOut(50, 80, "The once king of late-night", 27);
}

If you want to highlight the text, which is equivalent to changing its background, you can
call the CDC::SetBkColor() method. Its syntax is:

virtual COLORREF SetBkColor(COLORREF crColor);

You must provide the color you want to use as the crColor argument. If this method
succeed, it changes the background of the next text that would be drawn and it returns the
previous background color, which you can restore at a later time. Here is an example:

void CExoView::OnDraw(CDC* pDC)
{
Free download pdf