Multiple CRectTracker Class


Environment: VC6 SP2, NT4 SP3, Win95/98

The class I present performs the classic dragging and resizing of multiple objects, like vectoriel editors or CAD programs.
It does the same operations that the CRectTracker, with an array of rectangles instead of a single rectangle.
Those rectangles could be implemented with a CArray<CRect*, CRect*>, but the code in the project's view would be complex and not portable. So my solution is the CMRTObject (for CMultiRectTrackerObject), a base class for the edited objects, with just a rectangle information and two functions Get/Set. In a normal project, those objects are often based on CObject, but with multiple inheritance, this add-in is easy to be implemented. Another good news is that the CMultiRectTracker class gets the possibility to directly change the position/size of selected objects, so the CView code for the manipulation is light, as listed below.
// This is a demo object. As long as it contains 
// CMRTObject in its base classes, the multitrack works.
// Implement your own Draw fct, with your datas, etc..
// If derived from CObject, the serialize capacities can 
// be added.
class CDemoObject : public CMRTObject, public CObject
 CDemoObject (COLORREF rgb)
 : CMRTObject(), m_color(rgb) {;}
 CDemoObject (LPCRECT lpSrcRect, COLORREF rgb)
 : CMRTObject(lpSrcRect), m_color(rgb) {;} 

 ~CDemoObject () {;}

 void Draw (CDC* pDC) {
 pDC->FillSolidRect (GetRect(), m_color);

 COLORREF m_color;

Listed below is a possible implementation for your CxxView::OnLButtonDown(). It performs multiple selection with a local CRectTracker, and updates the CMultiRectTracker object contained by the view. If there is no rubber band, it selects the object clicked, and if the HitTest is different than -1 (CRectTracker::hitNothing), it starts the CMultiRectTracker::Track() function, wich contains the similar internal loop than CRectTracker. All objects moved or sized are directly updated in the Track() fct, the only work the CxxView needs to do is a call to Invalidate(). The CTRL key is scanned, and selected objects are removed if the user don't hit CTRL.

void CDemoView::OnLButtonDown(UINT nFlags, CPoint point) 
 CDemoDoc* pDoc = GetDocument(); 

 // Ask the multitrack if an object is already 
 // selected or a handle. If not, start the 
 // local tracker. 
 if (multiTrack.HitTest(point) < 0) { 

  // Reset the multitrack only if there 
  // is no CTRL key. 
  if (!(nFlags & MK_CONTROL)) 

  CRect rcObject; 
  CDemoObject* pObject; 

  // local tracker... 
  CRectTracker tracker; 
  if (tracker.TrackRubberBand(this, point, TRUE)) { 

   // see if rubber band intersects with the objects 
   CRect rectT; 
   POSITION pos = pDoc->m_objects.GetHeadPosition (); 
   while (pos != NULL) { 
    pObject = (CDemoObject*)(pDoc->m_objects.GetNext(pos)); 
    rcObject = pObject->GetRect(); 
    rectT.IntersectRect(tracker.m_rect, rcObject); 
    if (rectT == rcObject) { 
     multiTrack.Add(pObject); // add it to the multitrack, 
     // and continue the loop 
  } else { 
   // No rubber band, see if the point selects an object. 
   POSITION pos = pDoc->m_objects.GetHeadPosition (); 
   while (pos != NULL) { 
    pObject = (CDemoObject*)(pDoc->m_objects.GetNext(pos)); 
    rcObject = pObject->GetRect(); 
    if (rcObject.PtInRect(point)) { 
		 // add just one object to the multitrack 
 } else { 
  // Click on the multitrack, forward actions to it. 
  if (multiTrack.Track(this, point, FALSE)) 
 // Update drawing. 
 CView::OnLButtonDown(nFlags, point); 

In order to be classic, the cursor needs to be updated, function of its position on the objects, so a call to SetCursor() is needed before the CView-base class OnSetCursor() call, like the CRectTracker usage.

BOOL CDemoView::OnSetCursor(CWnd* pWnd, UINT nHitTest, 
 UINT message) 
 // forward to multitracker 
 if (pWnd == this && multiTrack.SetCursor(this, nHitTest)) 
 return TRUE; 
 return CView::OnSetCursor(pWnd, nHitTest, message); 


Download demo project - 34 Kb
Download source - 5 Kb


  • How to expand CRectTracker's handles size for easy use with touch panel?

    Posted by yyqqhy2000 on 07/24/2005 06:14am

    Hello all,
    I developed a program with CRectTracker for a computer with touch panel. But the eight handles are very hard to click or select with touch pen. Who can tell me how to expand the handles size?
    Thanks a lot in advance.

  • How to expand CRectTracker's handles size for easy use with touch panel?

    Posted by yyqqhy2000 on 07/24/2005 06:10am

    Hello all,
    I developed a program with CRectTracker for a computer with touch panel. But the eight handles are very hard to click or select with touch pen. Who can tell me how to expand the handles size?
    Thanks a lot in advance.

  • CScrollView

    Posted by Legacy on 01/30/2004 12:00am

    Originally posted by: Thierry

    What I do change in code to use it in CScrollView

    Thank you


  • Max size, and limite moving

    Posted by Legacy on 01/29/2004 12:00am

    Originally posted by: Thierry

    How can I limite the size of rectangle, and limite or control the moving?

    Can you help me

    Thank you


  • Normal printing

    Posted by Legacy on 05/10/2002 12:00am

    Originally posted by: Andrew

    I see preview, but objects too small:-(
    How repair?

  • Little problem

    Posted by Legacy on 02/01/2002 12:00am

    Originally posted by: mr

    If you keep your dragging limited to the view window, it is ok. But when you take the mouse out of the view window to the left, it leaves a blue rectangle. If you then come inside and again go out, this creates another blue rectangle. This goes on until you drop, or cancel the operation.
    What could be the possible solution?

  • Good job!

    Posted by Legacy on 11/26/2001 12:00am

    Originally posted by: ALX

    This is a great source code, but I have trouble whit trckrect.cpp file, line 195

    VERIFY(pDC->SaveDC() != 0)

    sometimes, this sentence failure and I can't restore the DC and the application is over. The SaveDC function doesn't work under windows 9x?

    What can I do to solve that?

    Thenk you.

  • Great piece of code

    Posted by Legacy on 02/12/2000 12:00am

    Originally posted by: kaizen

    The demo works very nicely. But in my application I have trouble setting color for the selected rec which is always drawn in black regardless of the pen color. Here is my code snippet:

    // Don't forget the multitrack
    CPen Pen, *POldPen;

    // Select white pen (on black background):
    Pen.CreatePen (PS_SOLID, 1, RGB(255,255,255));
    POldPen = pDC->SelectObject (&Pen);

    // remove pen/brush:
    pDC->SelectObject (POldPen);

    Thanks for any advice that you may have.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Hundreds of millions of users have adopted public cloud storage solutions to satisfy their Private Online File Sharing and Collaboration (OFS) needs. With new headlines on cloud privacy issues appearing almost daily, the need to explore private alternatives has never been stronger. Join ESG Senior Analyst Terri McClure and Connected Data in this on-demand webinar to take a look at the business drivers behind OFS adoption, how organizations can benefit from on-premise deployments, and emerging private OFS …

Most Popular Programming Stories

More for Developers

RSS Feeds