Click here to Skip to main content
6,596,602 members and growing! (20,137 online)
Email Password   helpLost your password?
Desktop Development » Miscellaneous » Custom Controls     Intermediate License: The Code Project Open License (CPOL)

LineTracker: A CRectTracker-like Class for Lines

By Dana Holt

This class will track lines like CRectTracker tracks rects
VC6, VC7Win2K, WinXP, Visual Studio, MFC, Dev
Posted:9 Apr 2002
Views:80,090
Bookmarked:46 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
27 votes for this article.
Popularity: 6.46 Rating: 4.52 out of 5

1

2
2 votes, 25.0%
3
2 votes, 25.0%
4
4 votes, 50.0%
5

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


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

My company develops network monitoring software named Server Nanny™.
Occupation: Software Developer (Senior)
Company: Xenos Software LLC
Location: United States United States

Other popular Miscellaneous articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 14 of 14 (Total in Forum: 14) (Refresh)FirstPrevNext
Generalyour class in VC6++ by MFC library Pinmemberthanhvu17:33 8 Oct '05  
GeneralConvert DevicePoint To Logical Point Pinmemberrdfcode12:15 27 Apr '05  
GeneralHow is line erased PinsussAnonymous2:47 1 Mar '04  
Generalawesome Pinmembernonocast22:43 23 Dec '03  
GeneralRe: awesome PinsupporterDana Holt5:37 24 Dec '03  
GeneralError In Compilation PinmemberDrC++3:05 19 Nov '03  
GeneralHow To Use It In CSrollView Pinmemberkhurram Mir6:00 15 Nov '03  
GeneralReally cool, thanks... PinmemberBrendan Tregear18:14 28 Aug '03  
GeneralRe: Really cool, thanks... PinsupporterDana Holt5:40 24 Dec '03  
GeneralC# Counterpart Pinsusscoolshot2615:07 14 Mar '03  
GeneralRe: C# Counterpart PinsupporterDana Holt14:49 10 May '03  
GeneralCan't compile.... PinmemberAnonymous8:12 29 Apr '02  
GeneralRe: Can't compile.... PinsupporterDana Holt2:15 30 Apr '02  
GeneralRe: Can't compile.... PinmemberSkizmo5:06 29 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Apr 2002
Editor: Genevieve Sovereign
Copyright 2002 by Dana Holt
Everything else Copyright © CodeProject, 1999-2009
Web12 | Advertise on the Code Project