// Class defining a circle object class CCircle : public CElement { public: virtual void Draw(CDC* pDC) const; // Function to display circle // Constructor for a circle object CCircle(const CPoint& Start, const CPoint& End, const COLORREF& Color ); protected: CCircle() {} // Default constructor - should not be used }; ////////// Circle ////////////////////// // CCircle class constructor CCircle::CCircle(const CPoint& Start, const CPoint& End, const COLORREF& Color ) { // First calculate the radius // We use floating point because that is required by // library function in (math.h) for calculating a square root. long Radius = static_cast(sqrt(static_cast((End.x - Start.x)* (End.x - Start.x) + (End.y - Start.y)*(End.y - Start.y)))); // Now calculate the rectangel enclosing // the circle assuming the MM_TEXT mapping mode m_EnclosingRect = CRect( Start.x - Radius, Start.y - Radius, Start.x + Radius, Start.y + Radius ); m_Color = Color; // Set Circle color m_Pen = 1; // Set pen width } // Draw a Circle object void CCircle::Draw( CDC* pDC ) const { // Create a new 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 )) { AfxMessageBox("Pen creation failed drawing a rectangle", MB_OK ); AfxAbort(); } // Select the pen CPen* pOldPen = pDC->SelectObject( &aPen ); // Select the brush CBrush* pOldBrush = static_cast(pDC->SelectStockObject(NULL_BRUSH)); // Now draw the rectangle pDC->Ellipse( m_EnclosingRect ); pDC->SelectObject( pOldBrush ); // Restore old brush pDC->SelectObject( pOldPen ); // Restore old pen }