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

……<br />

……<br />

protected:<br />

};<br />

BOOL m_bFloodFill;<br />

Function CGDIDoc::GetFloodFill() allows m_bFloodFill to be accessed outside class CGDIDoc.<br />

Variable m_bFloodFill is initialized in the constructor of class CGDIDoc as follows:<br />

CGDIDoc::CGDIDoc()<br />

{<br />

m_bFloodFill=FALSE;<br />

}<br />

Functions CGDIDoc::OnFloodfill() and CGDIDoc::OnUpdateFloodfill(…) are implemented as<br />

follows:<br />

void CGDIDoc::OnFloodfill()<br />

{<br />

m_bFloodFill=m_bFloodFill ? FALSE:TRUE;<br />

}<br />

void CGDIDoc::OnUpdateFloodfill(CCmdUI* pCmdUI)<br />

{<br />

pCmdUI->SetCheck(m_bFloodFill);<br />

}<br />

We need to implement flood filling in response to left button up events when CGDIDoc::m_bFloodFill<br />

is TRUE. So we need to modify function CGDIView::OnLButtonDown(…). In the sample, we first check flag<br />

CGDIDoc::m_bFloodFill. If it is set, we create a gray brush, select it into the DC and implement the flood<br />

filling. Otherwise we prepare for line drawing as we did before:<br />

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

{<br />

CGDIDoc *ptrDoc;<br />

CClientDC dc(this);<br />

CBrush brush;<br />

CBrush *ptrBrushOld;<br />

}<br />

ptrDoc=GetDocument();<br />

if(ptrDoc->GetFloodFill() == TRUE)<br />

{<br />

brush.CreateSolidBrush(RGB(127, 127, 127));<br />

ptrBrushOld=dc.SelectObject(&brush);<br />

dc.FloodFill(point.x, point.y, RGB(255, 0, 0));<br />

dc.SelectObject(ptrBrushOld);<br />

}<br />

else<br />

{<br />

m_ptStart=point;<br />

SetCapture();<br />

m_bCapture=TRUE;<br />

}<br />

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

In the other two mouse message handlers CGDIView::OnMouseMove(…) and CGDIView::<br />

OnLButtonUp(…), we also need to check flag CGDIDoc::m_bFloodFill. If it is not set, we can proceed to<br />

implement line drawing:<br />

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

{<br />

CClientDC dc(this);<br />

CPen pen;<br />

CPen *ptrPenOld;<br />

int nMode;<br />

218

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

Saved successfully!

Ooh no, something went wrong!