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

should be drawn outside or inside the rectangle. All the above styles can be combined together using bitwise<br />

OR operation.<br />

Tracker<br />

Resize button<br />

Resize button<br />

Moving and Resizing Tracker<br />

Figure 11-4. Tracker<br />

The tracker’s position and dimension are stored in variable CRectTracker::m_rect. We can modify it<br />

at any time to move the tracker or resize it. If this variable specifies a valid rectangle, we can draw the<br />

tracker by calling function CRectTracker::Draw(…) and display it in a window.<br />

To let user resize the tracker through clicking and dragging tracker’s resizing buttons (See Figure 11-<br />

4), we need to handle WM_LBUTTONDOWN message and call function CRectTracker::HitTest(…) to find out<br />

if the current mouse cursor hits any portion of the tracker. The following is the format of this function:<br />

int CRectTracker::HitTest(CPoint point);<br />

The following is a list of values that can be returned from this function along their meanings:<br />

Valure<br />

CRectTracker::hitNothing<br />

CRectTracker::hitTopLeft<br />

CRectTracker::hitTopRight<br />

CRectTracker::hitBottomRight<br />

CRectTracker:hitBottomLeft<br />

CRectTracker:hitTop<br />

CRectTracker:hitRight<br />

CRectTracker:hitBottom<br />

CRectTracker:hitLeft<br />

CRectTracker:hitMiddle<br />

Meaning<br />

The mouse cursor is not over the tracker.<br />

The mouse cursor is over the top-left resize button.<br />

The mouse cursor is over the top-right resize button<br />

The mouse cursor is over the bottom-right resize button<br />

The mouse cursor is over the bottom-left resize button<br />

The mouse cursor is over the resize button on the top border.<br />

The mouse cursor is over the resize button on the right border.<br />

The mouse cursor is over the resize button on the bottom border.<br />

The mouse cursor is over the resize button on the left border.<br />

The mouse cursor is within the tracker rectangle.<br />

If mouse cursor hits any of the resize buttons, we can call CRectTracker::Track() to track the mouse<br />

moving activities from now on until the left button is released. With this function, there is no need for us to<br />

handle other two messages WM_MOUSEMOVE and WM_LBUTTONUP, because once it is called, the function will<br />

not return until the left button is released. Of course, we can also write code to implement right button<br />

tracking. When we call function CRectTrack::Track(), the tracker’s owner window should not set window<br />

capture, otherwise the mouse message will not be routed to the tracker.<br />

Customizing Cursor Shape<br />

To let the mouse cursor shape change automatically when it is over tracker’s region, we need to call<br />

function CRectTracker::SetCursor(…) inside window’s CWnd::OnSetCursor(…) function (in the window<br />

that contains the tracker). If the function returns TRUE, it means that the cursor shape has already been<br />

346

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

Saved successfully!

Ooh no, something went wrong!