11.04.2014 Views

Advanced MFC Programming

Advanced MFC Programming

Advanced MFC Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11. Sample: Simple Paint<br />

position by calling function CGDIView::DrawPoint(…); for line drawing, we need to first erase the old<br />

drawings by copying the backup bitmap to CGDIView::m_bmpDraw, then draw a new line:<br />

……<br />

……<br />

case TOOL_PEN:<br />

{<br />

if(nFlags & MK_LBUTTON)<br />

{<br />

if(pt != m_ptMouseDown)<br />

{<br />

DrawPoint(point);<br />

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

}<br />

}<br />

break;<br />

}<br />

case TOOL_LINE:<br />

{<br />

if(nFlags & MK_LBUTTON)<br />

{<br />

ResumeBackupBmp();<br />

DrawLine(m_ptMouseDown, pt);<br />

}<br />

break;<br />

}<br />

The implementation of WM_LBUTTONUP message handler is almost the same with that of WM_MOUSEMOVE<br />

message handler: for dot drawing, a new dot is drawn at the current mouse position by calling function<br />

CGDIView::DrawPoint(…). For line drawing, the backup bitmap is first resumed to CGDIView::m_bmpdraw.<br />

Then a new line is drawn between points represented by CGDIView::m_ptMouseDown and current mouse<br />

position.<br />

Mouse Cursor<br />

It is desirable to change the shape of mouse cursor when it is within the bitmap image. We can either<br />

choose a standard mouse cursor or design our own cursor. A standard cursor can be loaded by calling<br />

function CWinApp::LoadStandardCursor(…). There are many standard cursors that can be used in the<br />

application, which include beam cursor (IDC_IBEAM), cross cursor (IDC_CROSS), etc. The mouse cursor can<br />

be changed by handling WM_SETCURSOR message. In this message handler, we can call ::SetCursor(…) to<br />

change the current cursor shape if we do not want the default arrow cursor.<br />

We need another function to judge if the current mouse cursor is within the bitmap image contained in<br />

the client window. In the sample, function CGDIView::MouseWithinBitmap() is added for this purpose. The<br />

current image ratio and scrolled positions are all taken into consideration when doing the calculation. The<br />

following is the implementation of this function:<br />

BOOL CGDIView::MouseWithinBitmap()<br />

{<br />

CPoint point;<br />

CPoint ptScroll;<br />

CRect rect;<br />

CRect rectBmp;<br />

BITMAP bm;<br />

int nRatio;<br />

CGDIDoc *pDoc;<br />

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

nRatio=pDoc->GetRatio();<br />

ptScroll=GetScrollPosition();<br />

::GetCursorPos(&point);<br />

GetWindowRect(rect);<br />

point.Offset(-rect.left, -rect.top);<br />

point.Offset(ptScroll);<br />

ASSERT(m_bmpDraw.GetSafeHandle());<br />

344

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

Saved successfully!

Ooh no, something went wrong!