一般用CDC时,都是选中Pen或Brush时保存原来的数据。
然后画完之后,再SelectObject恢复。但我在画完之后不调用SelectObject恢复原来的Pen之类的,
看样子也没有什么特别的情况发生。这样是不是不用恢复,可以简单很多

解决方案 »

  1.   

    要调的,不然会GDI泄露,泄露多了程序就挂了。可以写个程序帮意泄露一下,在任务管理器中看一下GDI对象数就明白了。
      

  2.   

    楼主可能没有遇到这方面的问题,记着当初开发一个图像编辑器的软件,会在使用的过程中出现内存不够的提示,而且也不是经常出现,要等程序用了一段时间才出现,查找了很久都没有找出问题,最后在任务管理器中发现GDI句柄有几万个,吓到了,最后才发现没有释放资源导致.
      

  3.   

    需要啊
    An application should always replace a new object with the original, default object after it has finished drawing with the new object. 
      

  4.   

    这样啊,原本还以为很好用哪。
    不过上面的前辈讲会造成泄露、还会产生上万个,是不是有些夸张啊毕竟我用的CPen之类的声明的是局部变量,Ondraw函数结束后是会自动释放的吧。
    就算不把原来的oldPen选择回去(SelectObject),也没有什么问题吧
    请前辈指点,谢谢!
      

  5.   

    看DeleteObject的说明你就明白了If the specified handle is not valid or is currently selected into a DC, the return value is zero. Do not delete a drawing object (pen or brush) while it is still selected into a DC.如果你的GDI对象每次都创建,但GDI对象每次用完后 DeleteObject都失败,这就造成了泄露.但如果你用的是全局变量,GDI对象仅创建一次,那就肯定没那么夸张
    还有补充一下:
    如果GDI对象如果被选进的是内存DC,当内存DC被DeleteDC后,DeleteObject是成功
    我看过很多人家的双缓冲画图代码最后都有用SelectObject恢复原来的,然而我觉得这种情况是不必的,因为都是要DeleteDC的,DeleteObject是成功。
      

  6.   

    Return Values
    If the function succeeds, the return value is nonzero.If the specified handle is not valid or is currently selected into a DC, the return value is zero. Windows NT/2000/XP: To get extended error information, call GetLastError.Res
    Do not delete a drawing object (pen or brush) while it is still selected into a DC.When a pattern brush is deleted, the bitmap associated with the brush is not deleted. The bitmap must be deleted independently.