11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8. DC, Pen, Brush and Palette<br />

void CGDIView::OnDraw(CDC* pDC)<br />

{<br />

int nNumOfPts;<br />

int nMode;<br />

int i;<br />

CPen pen;<br />

CPen *ptrPenOld;<br />

CPoint pt[4];<br />

}<br />

CGDIDoc* pDoc = GetDocument();<br />

ASSERT_VALID(pDoc);<br />

pen.CreatePen(PS_SOLID, 1, RGB(0, 255, 255));<br />

ptrPenOld=pDC->SelectObject(&pen);<br />

nMode=pDC->SetROP2(R2_COPYPEN);<br />

nNumOfPts=pDoc->GetNumOfPts();<br />

for(i=0; iGetOnePt(4*i);<br />

pt[1]=pDoc->GetOnePt(4*i+1);<br />

pt[2]=pDoc->GetOnePt(4*i+2);<br />

pt[3]=pDoc->GetOnePt(4*i+3);<br />

pDC->PolyBezier(pt, 4);<br />

}<br />

pDC->SetROP2(nMode);<br />

pDC->SelectObject(ptrPenOld);<br />

Since we need 4 control points to draw a curve, the number of curves we can draw should be equal to<br />

the number of points stored in array CGDIDoc::m_dwaPts divided by 4. In the function, a loop is used to<br />

draw each single curve. Within each loop, four control points are retrieved one by one, and stored in a local<br />

CPoint type array pt. After all four control points are retrieved, function CDC::PolyBezier(…) is called to<br />

draw the curve. Here, we also need to create a pen and select it into the DC before any drawing operation is<br />

performed.<br />

The rest thing we need to implement is recording control points. In order to do this, we need to handle<br />

two mouse related messages: WM_LBUTTONUP and WM_MOUSEMOVE. In the sample, their message handlers are<br />

added through using Class Wizard, the corresponding member functions are CGDIView::OnLButtonUp(…)<br />

and CGDIView::OnMouseMove(…) respectively.<br />

Since a curve needs four control points, we use mouse’s left button up event to record them. In the<br />

application, a counter is implemented to count how many control points have been added. Before a new<br />

curve is added, this counter is set to 0. As we receive message WM_LBUTTONUP, the counter will be<br />

incremented by 1. As it reaches 4, we finish recording the control points, store the data in the document and<br />

update the client window.<br />

In the sample, to implement curve drawing, some new variables are declared in class CGDIView as<br />

follows:<br />

……<br />

……<br />

class CGDIView : public CView<br />

{<br />

protected:<br />

CPoint m_ptCurve[4];<br />

int m_nCurrentPt;<br />

BOOL m_bNeedErase;<br />

BOOL m_bCapture;<br />

};<br />

We are familiar with variables m_bCapture and m_bNeedErase. Here, variable m_ptCurve will be used<br />

to record temporary control points, and m_nCurrentPt will act as a counter.<br />

Some of the variables are initialized in the constructor:<br />

CGDIView::CGDIView()<br />

{<br />

m_nCurrentPt=0;<br />

m_bNeedErase=FALSE;<br />

212

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!