Click here to Skip to main content
Email Password   helpLost your password?

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.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalyour class in VC6++ by MFC library
thanhvu
17:33 8 Oct '05  
hello,
I'm Vienamese and don't speak english, excuse for my English...
How do I want to write your class in VC6++ by MFC library ? Can you show me a way to excute that ?
thank you, see your soon.

thanhvu
GeneralConvert DevicePoint To Logical Point
rdfcode
12:15 27 Apr '05  
hello,
I'm italian and don't speak english, excuse for my English...
how I make to work in logical coordinates with your class?

Thank you
JoGiò
GeneralHow is line erased
Anonymous
2:47 1 Mar '04  
I really appreciate the project. It's of a great help.
I was trying to understand the code but was not really able to get how you manged to erase the line.
Is the logic like u draw the previos line over the drawn line with the background color.
Thanks in advance!!!
Generalawesome
nonocast
22:43 23 Dec '03  
the article is too simple.
can you do some further analysis?
thanks
GeneralRe: awesome
Dana Holt
5:37 24 Dec '03  
When I created the class my needs were pretty simple. It's meant to be a base to build on, just a concept of how this problem could be approached.

--
Dana Holt
Xenos Software LLC
GeneralError In Compilation
DrC++
3:05 19 Nov '03  
well when i tried to compile ur src , i found an error in this file LineTracker.cpp

at m_LineObjects.GetCount() Confused

and found that the error that GetCount not a member of CArray ,so i change it to GetSize() and it works so fine ..

it is really a good program Smile
GeneralHow To Use It In CSrollView
khurram Mir
6:00 15 Nov '03  
Can Anybody Help me how to use it in CScrollView
GeneralReally cool, thanks...
Brendan Tregear
18:14 28 Aug '03  
Just what I was looking for. Your code looks very well structured, first time I've seen a 'goto' for quite a while though! Smile I'm also writing tools for a text box (a bit tricky because I want the user to actually be able to edit the text in the control, not just type in a dialog box) and a cup-to-disc tool (medical, term. Basically two circles...), so I'll post those when I'm finished.


GeneralRe: Really cool, thanks...
Dana Holt
5:40 24 Dec '03  
Thanks.

Well, I know that goto has fallen out of favor in more recent times. I just didn't want it to feel too left out. Wink



--
Dana Holt
Xenos Software LLC
GeneralC# Counterpart
coolshot26
15:07 14 Mar '03  
Great program. I was looking for something similar.

Is there any possibility that this program can be duplicated in C# using GDI+?
GeneralRe: C# Counterpart
Dana Holt
14:49 10 May '03  
Sure, it could be duplicated in C#, GDI+. I have thought about it, but I don't have the time right now. As you can see by how long it took me to reply to this message. Smile

--
Dana Holt
Xenos Software LLC
GeneralCan't compile....
Anonymous
8: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....
Dana Holt
2:15 30 Apr '02  
This project was created using Visual Studio .NET

The source should compile if you create a blank VC++ 6 project and add the files to it.

HTH



--
Dana Holt
Xenos Software
GeneralRe: Can't compile....
Skizmo
5:06 29 Mar '05  
sorry this is not true. . .your project CANNOT be used in VC6


Last Updated 10 Apr 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010