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 />

CDC::LineTo(…) to complete drawing. We don’t need to call CDC::MoveTo(…) each time when drawing<br />

several connected line segments continuously. After function CDC::LineTo(…) is called, the DC’s origin<br />

will always be updated to the end point of that line. The following function shows how the mouse moving<br />

activities are handled in the sample:<br />

void CGDIView::OnMouseMove(UINT nFlags, CPoint point)<br />

{<br />

CClientDC dc(this);<br />

CPen pen;<br />

CPen *ptrPenOld;<br />

int nMode;<br />

}<br />

if(nFlags & MK_LBUTTON)<br />

{<br />

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

ptrPenOld=dc.SelectObject(&pen);<br />

nMode=dc.SetROP2(R2_XORPEN);<br />

if(m_bNeedErase == TRUE)<br />

{<br />

dc.MoveTo(m_ptStart);<br />

dc.LineTo(m_ptEnd);<br />

}<br />

else<br />

{<br />

m_bNeedErase=TRUE;<br />

}<br />

m_ptEnd=point;<br />

dc.MoveTo(m_ptStart);<br />

dc.LineTo(m_ptEnd);<br />

dc.SetROP2(nMode);<br />

dc.SelectObject(ptrPenOld);<br />

}<br />

CView::OnMouseMove(nFlags, point);<br />

We declare CClientDC type variable to obtain the device context of the client window. First function<br />

CPen::CreatePen(…) is called to create a dotted pen with black color. Then this pen is selected into the<br />

device context and the old pen is stored in ptrPenOld. Next, the device context’s drawing mode is set to<br />

R2_XORPEN, and the original drawing mode is stored in nMode. If this is the first time the function is called<br />

after the user pressed mouse’s left button, m_bNeedErase flag must be FALSE. In this case we need to set it<br />

to TRUE. Otherwise if m_bNeedErase is TRUE, we need to first draw the old outline (This will erase the<br />

outline). Next the current mouse position is stored in variable m_ptEnd, and CDC::MoveTo(…),<br />

CDC::LineTo(…) are called to draw the new outline. Finally device context’s old drawing mode is resumed.<br />

Also, the old pen is selected back into the DC (which will also select the new pen out of the DC).<br />

For WM_LBUTTONUP message handler, we also need to erase the old line if necessary. Because the new<br />

line is fixed now, we need to add new data to the document and update the client window. The following<br />

function shows how this message is handled:<br />

void CGDIView::OnLButtonUp(UINT nFlags, CPoint point)<br />

{<br />

CClientDC dc(this);<br />

CPen pen;<br />

CPen *ptrPenOld;<br />

int nMode;<br />

CGDIDoc *ptrDoc;<br />

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

ptrPenOld=dc.SelectObject(&pen);<br />

nMode=dc.SetROP2(R2_XORPEN);<br />

dc.MoveTo(m_ptStart);<br />

dc.LineTo(m_ptEnd);<br />

dc.SetROP2(nMode);<br />

dc.SelectObject(ptrPenOld);<br />

pen.DeleteObject();<br />

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

206

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

Saved successfully!

Ooh no, something went wrong!