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

m_ptEnd=point;<br />

ptrDoc->AddLine(CRect(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y));<br />

m_bNeedErase=FALSE;<br />

Invalidate();<br />

if(m_bCapture == TRUE)<br />

{<br />

::ReleaseCapture();<br />

m_bCapture=FALSE;<br />

}<br />

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

Here a dotted pen is created again to erase the old line. Then function CView::GetDocument() is called<br />

to obtain a pointer to the document. After the line information is retrieved from the mouse position,<br />

function CGDIDoc::AddLine(…) is called to add new data to the document. Then flag m_bNeedErase is<br />

cleared, and window capture is released. Finally function CWnd::Invalidate() is called to update the client<br />

window. By default, this action will cause function CView::OnDraw(…) to be called.<br />

For SDI and MDI applications, function CView::OnDraw(…) will be added to the project at the<br />

beginning by Application Wizard. In order to implement our own interface, we need to rewrite this member<br />

function as follows:<br />

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

{<br />

CPen pen;<br />

CPen *ptrPenOld;<br />

int nMode;<br />

int nNumOfLines;<br />

int i;<br />

CRect *ptrRect;<br />

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

ASSERT_VALID(pDoc);<br />

}<br />

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

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

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

nNumOfLines=pDoc->GetNumOfLines();<br />

for(i=0; iGetLine(i);<br />

pDC->MoveTo(ptrRect->TopLeft());<br />

pDC->LineTo(ptrRect->BottomRight());<br />

}<br />

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

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

In the above function CPen::CreatePen(…) is called to create a red solid pen whose width is 1 device<br />

unit. Since the window DC is passed as a parameter to this funciton, we do not need to call CWnd::GetDC()<br />

or declare CClientDC type variable to obtain window’s device context. After the pen is selected into the<br />

DC, the drawing mode is set to R2_COPYPEN, which will output the pen’s color to the target device. Next<br />

function CGDIDoc::GetNumOfLines() is called to retrieve the total number of lines stored in the document.<br />

Then a loop is used to draw every line in the client window. Finally the DC’s original drawing mode is<br />

resumed and the new pen is selected out of it.<br />

8.2 Rectangle and Ellipse<br />

It is easy to implement rectangle drawing after we understand the previous sample. To draw a<br />

rectangle, we need to call function CDC::Rentangle(…) and pass a CRect type value to it. The rectangle’s<br />

border will be drawn using current pen selected by the DC, and its interior will be filled with the currently<br />

selected brush.<br />

207

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

Saved successfully!

Ooh no, something went wrong!