CRectTracker movement

How do i restrict the rectangle of a CRectTrack to move in horizontal direction only when the user LButtonDown on the rectangle and drags it  around?

I have managed to set the rectangle to 'Track' the mouse on LButton down.  What should i do after that?


 
leowlfAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rmaczConnect With a Mentor Commented:
Hi,
In order for the tracker to move in horizontal direction, you need to create new class
(e.g. CMyRectTracker) derived from CRectTracker and override the AdjustRect method.
The sample code is presented below:


// MyTracker.h file
class CMyRectTracker : public CRectTracker
{
      // remember the original rect before tracking
      CRect m_OriginalRect;
public:
      CMyRectTracker() : CRectTracker() {}
      CMyRectTracker( LPCRECT lpSrcRect, UINT nStyle )
            : CRectTracker( lpSrcRect, nStyle ) {}

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

      virtual void AdjustRect( int nHandle, LPRECT lpRect );
};


// MyTracker.cpp file

void CMyRectTracker::AdjustRect( int nHandle, LPRECT lpRect )
{
      CRectTracker::AdjustRect( nHandle, lpRect );
      if ( nHandle == 8 ) {
            lpRect->top = m_OriginalRect.top;
            lpRect->bottom = m_OriginalRect.bottom;
      }
};


BOOL CMyRectTracker::Track( CWnd* pWnd, CPoint point, BOOL bAllowInvert, CWnd* pWndClipTo )
{
      m_OriginalRect = m_rect;
      return CRectTracker::Track( pWnd, point, bAllowInvert, pWndClipTo );
}



You can use this class in the following way:
1. Declare variable of type CMyRectTracker in your View class, e.g.
class CMyView : public CView
{
public:
    CMyRectTracker tr;
};

2. Initialize the variable in the constructor, e.g.:
CMyView::CMyView()
{
    tr.m_rect = CRect( 100, 100, 200, 200 );
    tr.m_nStyle = CRectTracker::solidLine;
}

3. Add the drawing code
void CMyView::OnDraw( CDC *pDC )
{
    tr.Draw( pDC );
}


4. Add OnLButtonDown code
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
    CScrollView::OnLButtonDown(nFlags, point);
    int nHit = tr.HitTest( point );
    if ( nHit == 8 ) {
        // interior of the tracker was hit
      tr.Track( this, point );
      Invalidate();
    }
}

And that's it.


This is just a sample of what you can do. The most important function to deal with is the
CRectTracker::AdjustRect. As the parameter you get the sizing handle that was hit
(I assumed that you are interrested in moving of the entire rectangle, which is encoded
as handle: 8) and the new size of the rectangle. Because I also need the original rectangle
size, I have added new variable to the CMyRectTracker class (m_OriginalRect). Inside
the AdjustRect function you can make any adjustments to the rectangle e.g. guidelines,
grid etc.

If you need additional informations, just let me know

rmacz


0
 
Vinayak KumbarSr Program ManagerCommented:
Hi,

Take a look at the sample project "TRACKER" in VC++ 5.0 samples.
Hope it will help U.
0
All Courses

From novice to tech pro — start learning today.