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?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vinayak KumbarSr Program ManagerCommented:

Take a look at the sample project "TRACKER" in VC++ 5.0 samples.
Hope it will help U.
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;
      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 =;
            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
    CMyRectTracker tr;

2. Initialize the variable in the constructor, e.g.:
    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 );

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



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.