// Constructor for a curve object CCurve::CCurve( const CPoint& FirstPoint, const CPoint& SecondPoint, const COLORREF& Color ) { m_PointList.AddTail( FirstPoint ); // Add the 1st point to the list m_PointList.AddTail( SecondPoint ); // Add the second point to the list m_Color = Color; // Set Circle color m_Pen = 1; // Set pen width // Construct the enclosing rectangle assumming MM_TEXT mode m_EnclosingRect = CRect( FirstPoint, SecondPoint ); m_EnclosingRect.NormalizeRect(); } void CCurve::AddSegment( const CPoint& Point ) { m_PointList.AddTail( Point ); // Add the point to the end // Modify the enclosing rectangle for the new point m_EnclosingRect = CRect( min(Point.x, m_EnclosingRect.left), min(Point.y, m_EnclosingRect.top), max(Point.x, m_EnclosingRect.right), max(Point.y, m_EnclosingRect.bottom) ); } // Draw a curve void CCurve::Draw( CDC* pDC ) const { // Create a pen for this object and // initialize it to the object color and line width CPen aPen; if (!aPen.CreatePen(PS_SOLID, m_Pen, m_Color )) { // Pen creation failed, Abort the program AfxMessageBox("Pen creation failed drawing a line", MB_OK ); AfxAbort(); } CPen* pOldPen = pDC->SelectObject( &aPen ); // Now draw the curve // Get the position in the list of the first element POSITION aPosition = m_PointList.GetHeadPosition(); // As long as it's good, move to that point if ( aPosition ) pDC->MoveTo( m_PointList.GetNext(aPosition) ); // Draw a segment for each of the following points while (aPosition ) pDC->LineTo( m_PointList.GetNext(aPosition) ); pDC->SelectObject( pOldPen ); // Restore the old pen }