// Create a black pen object CPen* pPen = new CPen(PS_SOLID, 1, RGB(0, 0, 0)); // Use the pen object to draw a line pDC->SelectObject(pPen); pDC->MoveTo(0, 0); pDC->LineTo(100, 100); // Delete the pen object pPen->DeleteObject();
// Create a red pen object CPen pen(PS_SOLID, 1, RGB(255, 0, 0)); // Use the pen object to draw a rectangle pDC->SelectObject(&pen); pDC->Rectangle(50, 50, 100, 100); // Delete the pen object (automatic cleanup)In this example, a red pen object is created using the constructor for CPen. The pen object is then selected into the device context (pDC) using the SelectObject function, and a rectangle is drawn using the Rectangle function. Since the pen object was created on the stack and not using new, it will be automatically deleted when it goes out of scope. Package Library: The CPen DeleteObject function is part of the MFC (Microsoft Foundation Class) library, which is included with Microsoft Visual Studio.