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 11. Sample: Simple Paint<br />

void CGDIView::DrawLine(CPoint ptStart, CPoint ptEnd);<br />

This function draws a line on the bitmap from point ptStart to ptend using the current foreground<br />

color, which is stored in the document. The input parameters must be normalized points.<br />

void CGDIView::BackupCurrentBmp();<br />

For the purpose of backing up the current bitmap, a new CBitmap type variable m_bmpBackup is<br />

declared in class CGDIView. When function CGDIView::BackupCurrentBmp() is called, we create a new<br />

bitmap and attaches it to m_bmpBackup then initialize the bitmap with the current bitmap image<br />

(CGDIView:: m_bmpDraw).<br />

void CGDIView::ResumeBackupBmp();<br />

This function does the opposite of the previous function, it copies the bitmap stored in CGDIView::<br />

m_bmpBackup to CGDIView::m_bmpDraw.<br />

With the above new functions, we are able to implement dot and line drawing. To implement<br />

interactive line drawing, another new variable m_ptMouseDown is declared in class CGDIView. This variable<br />

is used to record the position of mouse cursor when its left button is being pressed down. As the mouse<br />

moves or the left button is released, we can use it along with the new mouse position to draw a straight line.<br />

The following is the implementation of WM_LBUTTONDOWN message handler:<br />

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

{<br />

int nCurrentTool;<br />

CGDIDoc *pDoc;<br />

}<br />

if(m_bmpDraw.GetSafeHandle() != NULL)<br />

{<br />

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

nCurrentTool=pDoc->GetCurrentTool();<br />

SetCapture();<br />

m_ptMouseDown=NormalizePtPosition(point);<br />

switch(nCurrentTool)<br />

{<br />

case TOOL_PEN:<br />

{<br />

DrawPoint(m_ptMouseDown);<br />

pDoc->SetModifiedFlag(TRUE);<br />

break;<br />

}<br />

case TOOL_LINE:<br />

{<br />

BackupCurrentBmp();<br />

DrawPoint(m_ptMouseDown);<br />

pDoc->SetModifiedFlag(TRUE);<br />

break;<br />

}<br />

}<br />

}<br />

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

After left button of the mouse is pressed down, we must set window capture in order to receive mouse<br />

messages even when the cursor is not within the client window. The window capture is released when the<br />

left button is released. If the current drawing object is dot, we need to call function CGDIView::<br />

DrawPoint(…) to draw the dot at the current mouse position; if the current drawing object is line, we need<br />

to first backup the current bitmap then draw a dot at the current mouse position.<br />

For WM_MOUSEMOVE message, first we must check if the left button is being held down. If so, we can<br />

further proceed to implement drawing. For dot drawing, we need to draw a new dot at the current mouse<br />

343

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

Saved successfully!

Ooh no, something went wrong!