Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


The same pen can be created using the CreatePen() method as follows:

CPen NewPen;

NewPen.CreatePen(PS_DASHDOTDOT, 1, RGB(255, 25, 5));

After creating a pen, you can select it into the desired device context variable and then
use it as you see fit, such as drawing a rectangle. Here is an example:

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

CPen NewPen;

NewPen.CreatePen(PS_DASHDOTDOT, 1, RGB(255, 25, 5));

pDC->SelectObject(&NewPen);

pDC->Rectangle(20, 22, 250, 125);
}

Once a pen has been selected, any drawing performed and that uses a pen would use the
currently selected pen. If you want to use a different pen, you can either create a new pen
or change the characteristics of the current pen.

After using a pen, between exiting the function or event that created it, you should get rid
of it and restore the pen that was selected previously. Here is an example:

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

CPen NewPen;

NewPen.CreatePen(PS_DASHDOTDOT, 6, RGB(255, 25, 5));

CPen* pPen = pDC->SelectObject(&NewPen);

pDC->Rectangle(20, 22, 250, 125);

// Restore the previous pen
pDC->SelectObject(pPen);
}
Free download pdf