Click here to Skip to main content
Licence CPOL
First Posted 9 Apr 2002
Views 95,436
Downloads 1,968
Bookmarked 53 times

LineTracker: A CRectTracker-like Class for Lines

By | 9 Apr 2002 | Article
This class will track lines like CRectTracker tracks rects

Introduction

While working on a recent project, I noticed that Microsoft doesn't provide any kind of class to track line-based objects. I took a peek at the CRectTracker class and then I wrote my own class that is similar to CRectTracker.

The implementation is very much like CRectTracker and most of the function names are the same. I only need to track a single line in my project, so I didn't add support for multiple lines to be tracked at once. Although I didn't add multiple line support, there are a few variables and functions that are the beginnings of adding this functionality

Ok, now let's get on to the part you've been waiting for. I have encapsulated a lot of logic for the OnLButtonDown() handler inside my class. Let's take a look at the OnLButtonDown() hander from the demo application:

void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{

    // Let CLineTracker do its thing
    if (!m_pLineTracker->OnLButtonDown(this, nFlags, point, &m_LineList)) 
    {

        // If not lines were hit then start a local CLineTracker 
        // to rubber band a new line
        CLineTracker tracker;

        if (tracker.TrackRubberLine(this, point)) 
        {

            // Make sure we have a line that is long enough
            if ((abs(tracker.m_points.Width()) > 20) || 
                (abs(tracker.m_points.Height()) > 20)) 
            { 

                // Add the line to our (very simple) linked list
                CLineItem* pLine = new CLineItem();

                pLine->m_rcPoints.SetRect(tracker.m_points.left,
                    tracker.m_points.top, tracker.m_points.right,
                    tracker.m_points.bottom);
                m_LineList.Add(pLine);

            }

        }

    }

    Invalidate();

    CWnd::OnLButtonDown(nFlags, point);
}

The one parameter that may need to be explained is &m_LineList. This is a pointer to a simple linked list of lines. See the classes CLineList and CLineItem in the demo project. CLineTracker's OnLButtonDown() function scans through the linked list to see if any lines are selected, moved, or have their "handles" tracked.

To make sure that the mouse cursor is changed to reflect the tracking operation status, you need to add some code to your OnSetCursor() message handler. Here's the OnSetCursor() handler from the demo application:

BOOL CChildView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
    // Make sure the line tracker gets a chance to change the cursor
    if (m_pLineTracker->SetCursor(pWnd, nHitTest))
        return TRUE;

    return CWnd::OnSetCursor(pWnd, nHitTest, message);
}

Now we need to make sure that CLineTracker's "handles" are drawn. In the demo application, the OnPaint() handler takes care of this. Here's the demo applications's OnPaint() code:

void CChildView::OnPaint() 
{
    CPaintDC dc(this); // device context for painting
    
    // Draw the lines (arrows)
    CLineItem* pLine;

    pLine = m_LineList.GetFirst();

    CPoint ptStart;
    CPoint ptEnd;

    while(pLine) 
    {

        ptStart.x = pLine->m_rcPoints.left;
        ptStart.y = pLine->m_rcPoints.top;

        ptEnd.x = pLine->m_rcPoints.right;
        ptEnd.y = pLine->m_rcPoints.bottom;

        DrawArrow(&dc, ptStart, ptEnd);

        pLine = pLine->m_pNext;
    }
    
    // Give the tracker a chance to draw itself
    m_pLineTracker->Draw(&dc);

    // Do not call CWnd::OnPaint() for painting messages
}

This is my first-ever source code submission and I hope this class provides some useful ideas to some of you fellow developers out there.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dana Holt

Software Developer (Senior)
Xenos Software LLC
United States United States

Member

I started learning C and 68K assembly language on an Atari ST around 1985 or so using the Mark Williams compiler. I was instantly hooked.
 
My favorite languages are: C/C++, assembly language, Javascript, ASP, and PHP. I also enjoy database design, system hardware, networking, and n-tier web design.
 
My first PC clone was a 386SX-25MHz with 4MB of RAM. OMG | :OMG:
 
My company develops network monitoring software named Server Nanny™.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow can I serialize this class ? Pinmembermesajflaviu4:47 9 Jul '10  
Generalyour class in VC6++ by MFC library Pinmemberthanhvu16:33 8 Oct '05  
GeneralConvert DevicePoint To Logical Point Pinmemberrdfcode11:15 27 Apr '05  
QuestionHow is line erased PinsussAnonymous1:47 1 Mar '04  
Generalawesome Pinmembernonocast21:43 23 Dec '03  
GeneralRe: awesome PinmemberDana Holt4:37 24 Dec '03  
GeneralError In Compilation PinmemberDrC++2:05 19 Nov '03  
QuestionHow To Use It In CSrollView Pinmemberkhurram Mir5:00 15 Nov '03  
GeneralReally cool, thanks... PinmemberBrendan Tregear17:14 28 Aug '03  
GeneralRe: Really cool, thanks... PinmemberDana Holt4:40 24 Dec '03  
GeneralC# Counterpart Pinsusscoolshot2614:07 14 Mar '03  
GeneralRe: C# Counterpart PinmemberDana Holt13:49 10 May '03  
GeneralCan't compile.... PinmemberAnonymous7:12 29 Apr '02  
Do you have the code for VC++ 6?
I don't know how to compile a .vcproj
GeneralRe: Can't compile.... PinmemberDana Holt1:15 30 Apr '02  
GeneralRe: Can't compile.... PinmemberSkizmo4:06 29 Mar '05  
GeneralRe: Can't compile.... Pinmemberxox_c0bra_xox10:43 14 Apr '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 10 Apr 2002
Article Copyright 2002 by Dana Holt
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid