看vc技术内幕产生的问题
vc技术内幕里面在5.2章里面有如下的一段讲解,说的是在生成新的GUI对象时,应先保存旧的对象,使用新对象之后再调用旧的GUI对象,析构函数会自动删除新建的对象,原文及代码如下:
OK, so you know that you have to delete your GDI objects and that they must first be disconnected from their device contexts. How do you disconnect them? A member of the CDC::SelectObject family of functions does the work of selecting a GDI object into the device context, and in the process it returns a pointer to the previously selected object (which gets deselected in the process). Trouble is, you can't deselect the old object without selecting a new object. One easy way to track the objects is to "save" the original GDI object when you select your own GDI object and "restore" the original object when you're finished. Then you'll be ready to delete your own GDI object. Here's an example:
void CMyView::OnDraw(CDC* pDC)
{
CPen newPen(PS_DASHDOTDOT, 2, (COLORREF) 0); // black pen,
// 2 pixels wide
CPen* pOldPen = pDC->SelectObject(&newPen); //奇怪呀,这不是把新生成的对象又赋给了CPen* pOldPen吗?哪有保存呢?老对象存在哪里呢?
pDC->MoveTo(10, 10);
pDC->Lineto(110, 10); //这应该是用新画笔画线
pDC->SelectObject(pOldPen); // newPen is deselected
} // newPen automatically destroyed on exit
难道,pDC->SelectObject(&newPen)返回值是老GUI对象不成?
本人不才,望各位指点,多谢了。
问题点数:20、回复次数:3Top




