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

m_bmpDraw.GetBitmap(&bm);<br />

rectBmp=CRect(0, 0, bm.bmWidth*nRatio, bm.bmHeight*nRatio);<br />

}<br />

return rectBmp.PtInRect(point);<br />

First we retrieve the current image ratio, horizontal and vertical scrolled positions of the client<br />

window. Then function ::GetCursorPos(…) is called to obtain the current position of mouse cursor.<br />

Because the returned value of this function (a POINT type value) is measured in the coordinate system of the<br />

desktop window (whole screen), we need to convert it to the coordinate system of the client window before<br />

judging if the cursor is within the bitmap image. Next, the image rectangle is stored in variable rectBmp,<br />

and function CRect::PtInRect(…) is called to make the judgment.<br />

This function is called in WM_SETCURSOR message handler. The following is the implementation of the<br />

corresponding function:<br />

BOOL CGDIView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)<br />

{<br />

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

{<br />

if<br />

(<br />

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

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

)<br />

{<br />

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

return TRUE;<br />

}<br />

}<br />

return CScrollView::OnSetCursor(pWnd, nHitTest, message);<br />

}<br />

If the cursor is within the bitmap image and is over neither the horizontal scroll bar nor the vertical<br />

scroll bar, we set the cursor to IDC_CROSS (a standard cursor). Otherwise by calling the default<br />

implementation of function OnSetCursor(…), the cursor will be set to the default arrow cursor.<br />

11.4 Tracker<br />

Sample 11.4\GDI is based on sample 11.3\GDI.<br />

Tracker can be implemented to let the user select a rectangular area very easily, and is widely used in<br />

applications supporting OLE to provide a graphical interface that lets the user interact with OLE client<br />

items. When implementing a tracker, we can select different styles. This can let the tracker be displayed<br />

with a variety of visual effects such as hatched borders, resize handles, etc.<br />

Tracker can also be applied to any normal application. In a graphic editor, tracker can be used to select<br />

a rectangular region, move and drop it anywhere within the image. It can also be used to indicate the<br />

selected rectangular area when we implement cut, copy and paste commands (Figure 11-4).<br />

Implementing Tracker<br />

Tracker is supported by <strong>MFC</strong> class CRectTracker. To enable a rectangular tracker, we need to first use<br />

this class to declare a variable, then set its style. When the window owns the tracker is being painted, we<br />

need to call a member function of CRectTracker to draw the tracker.<br />

We can set the tracker to different styles. The style of tracker is specified by variable CRectTracker::<br />

m_nStyle. The following values are defined in class CRectTracker and can be used to specify the border<br />

styles of a tracker: CRectTracker::solidLine, CRectTracker::dottedLine, CRectTracker::<br />

hatchedBorder. The following values can also be assigned to CRectTracker::m_nStyle to specify how the<br />

tracker can be resized: CRectTracker::resizeInside, CRectTracker::resizeOutside. Finally,<br />

CRectTracker::hatchInside can be assigned to CRectTracker::m_nStyle to specify if the hatched border<br />

345

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

Saved successfully!

Ooh no, something went wrong!