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

……<br />

)<br />

{<br />

)<br />

if<br />

(<br />

)<br />

{<br />

m_trackerSel.m_rect.IsRectEmpty() != TRUE &&<br />

m_trackerSel.SetCursor(this, nHitTest) == TRUE<br />

MouseWithinBitmap() == TRUE &&<br />

HTVSCROLL != nHitTest && HTHSCROLL != nHitTest<br />

::SetCursor(AfxGetApp()->LoadStandardCursor(IDC_CROSS));<br />

return TRUE;<br />

}<br />

}<br />

else return TRUE;<br />

Here we first check if the cursor can be set automatically. If CRectTracker::SetCursor(…) returns<br />

TRUE, we can exit and return a TRUE value (If this function returns TRUE, it means currently the mouse<br />

cursor is within the tracker region, and the cursor is customized by the tracker). If not, we check if the<br />

mouse is over the bitmap image, if so, the cursor’s shape is set to IDC_CROSS and the function exits (We<br />

need to return TRUE every time the cursor has been customized). If all these fail, we need to call function<br />

CWnd::SetCursor(…) to set the cursor to the default shape.<br />

If Mouse Clicking Doesn’t Hit the Tracker<br />

We must implement a way of letting the user create tracker interactively. Like line drawing<br />

implementations, we need to handle WM_LBUTTONDOWN, WM_RBUTTONDOWN and WM_MOUSEMOVE messages in<br />

order to let the user create tracker by mouse clicking. This procedure is similar to rectangle drawing: when<br />

left button is pressed down, we need to record the current mouse position as the starting point; as the mouse<br />

is dragged around, we draw a series of temporary rectangles; when the left button is released, we use the<br />

current mouse position as the ending point and use it along with the starting point to draw the tracker.<br />

For the tracker, there are two possibilities when the mouse button is pressed down. If currently there is<br />

an existing tracker and the mouse hits the tracker, we should let the tracker be moved or resized instead of<br />

creating a new tracker. If there is no tracker currently implemented or the mouse did not hit the existing<br />

tracker, we should start creating a new tracker.<br />

This situation can be judged by calling function CTracker::HitTest(…), whose input parameter<br />

should be set to the current position of mouse cursor that is measured in the client window’s coordinate<br />

system. If the function returns CRectTracker::hitNothing, either there is no existing tracker or the mouse<br />

didn’t hit any portion of the tracker. In the sample, this situation is handled as follows:<br />

……<br />

……<br />

case TOOL_RECTSEL:<br />

{<br />

int nHitTest;<br />

nHitTest=m_trackerSel.HitTest(point);<br />

if(nHitTest == CRectTracker::hitNothing)<br />

{<br />

m_trackerSel.m_rect.left=<br />

m_trackerSel.m_rect.right=<br />

point.x+GetScrollPosition().x;<br />

m_trackerSel.m_rect.top=<br />

m_trackerSel.m_rect.bottom=<br />

point.y+GetScrollPosition().y;<br />

}<br />

We record the starting position in the upper-left point of the tracker rectangle. As the mouse moves,<br />

the rectangle’s bottom-right point is updated with the current mouse position, and temporary rectangles are<br />

drawn and erased before the tracker is finally fixed.<br />

348

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

Saved successfully!

Ooh no, something went wrong!