class CRectangle : public CElement { public: virtual void Draw(CDC* pDC) const; // Function to display rectangle // Constructor for a rectangle object CRectangle(const CPoint& Start, const CPoint& End, const COLORREF& Color ); protected: CRectangle() {} }; // CRectangle class constructor CRectangle::CRectangle(const CPoint& Start, const CPoint& End, const COLORREF& Color ) { m_Color = Color; // Set rectangle color m_Pen = 1; // Set pen width // Define the enclosing rectangle m_EnclosingRect = CRect( Start, End ); m_EnclosingRect.NormalizeRect(); } // Draw a CRectangle object void CRectangle::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->Rectangle( m_EnclosingRect ); pDC->SelectObject( pOldBrush ); // Restore old brush pDC->SelectObject( pOldPen ); // Restore old pen }