The MFC CRectTracker Class

The CRectTracker class is useful in both OLE and non-OLE programs. It allows the user to move and resize a rectangular object in a view window. There are two important data members: the m_nStyle member determines the border, resize handle, and other characteristics; and the m_rect member holds the device coordinates for the rectangle.

The important member functions follow.

void Draw(CDC* pDC) const;

The Draw function draws the tracker, including border and resize handles, but it does not draw anything inside the rectangle. That's your job.

BOOL Track(CWnd* pWnd, CPoint point, BOOL bAllowInvert = FALSE, CWnd* pWndClipTo = NULL);

You call this function in a WM_LBUTTONDOWN handler. If the cursor is on the rectangle border, the user can resize the tracker by holding down the mouse button; if the cursor is inside the rectangle, the user can move the tracker. If the cursor is outside the rectangle, Track returns FALSE immediately; otherwise, Track returns TRUE only when the user releases the mouse button. That means Track works a little like CDialog::DoModal. It contains its own message dispatch logic.

int HitTest(CPoint point) const;

Call HitTest if you need to distinguish between mouse button hits inside and on the tracker rectangle. The function returns immediately with the hit status in the return value.

BOOL SetCursor(CWnd* pWnd, UINT nHitTest) const;

Call this function in your view's WM_SETCURSOR handler to ensure that the cursor changes during tracking. If SetCursor returns FALSE, call the base class OnSetCursor function; if SetCursor returns TRUE, you return TRUE.

CRectTracker Rectangle Coordinate Conversion

You must deal with the fact that the CRectTracker::m_rect member stores device coordinates. If you are using a scrolling view or have otherwise changed the mapping mode or viewport origin, you must do coordinate conversion. Here's a strategy:

  1. Define a CRectTracker data member in your view class. Use the name m_tracker.

  2. Define a separate data member in your view class to hold the rectangle in logical coordinates. Use the name m_rectTracker.

  3. In your view's OnDraw function, set m_rect to the updated device coordinates, and then draw the tracker. This adjusts for any scrolling since the last OnDraw. Some sample code appears below.

     m_tracker.m_rect = m_rectTracker; pDC->LPtoDP(m_tracker.m_rect); // tracker requires device                                //  coordinates m_tracker.Draw(pDC); 

  4. In your mouse button down message handler, call Track, set m_rectTracker to the updated logical coordinates, and call Invalidate, as shown here:

     if (m_tracker.Track(this, point, FALSE, NULL)) {     CClientDC dc(this);     OnPrepareDC(&dc);     m_rectTracker = m_tracker.m_rect;     dc.DPtoLP(m_rectTracker);     Invalidate(); } 


Programming Microsoft Visual C++
Programming Microsoft Visual C++
ISBN: 1572318570
EAN: 2147483647
Year: 1997
Pages: 332

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net