|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionWhile 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 The implementation is very much like Ok, now let's get on to the part you've been waiting for. I have encapsulated a lot of logic for the 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 To make sure that the mouse cursor is changed to reflect the tracking operation status, you need to add some code to your 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 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.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||