首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

运行下面这个函数为什么会引起GDI泄漏?怎么删除这个GDI泄漏

2013-12-23 
运行下面这个函数为什么会引起GDI泄漏?如何删除这个GDI泄漏?[解决办法]dlg.GetDefaults()) 后会得到一个DC

运行下面这个函数为什么会引起GDI泄漏?如何删除这个GDI泄漏?

                                                                            
[解决办法]
dlg.GetDefaults()) 后会得到一个DC
这个DC需要释放. MSDN中的例子中有说明.
CPrintDialog dlg(FALSE);

if (!dlg.GetDefaults())
{
   AfxMessageBox(_T("You have no default printer!"));
}
else
{
   // attach to the DC we were given
   CDC dc;
   dc.Attach(dlg.m_pd.hDC);

   // ask for the measurements
   int nHorz = dc.GetDeviceCaps(LOGPIXELSX);
   int nVert = dc.GetDeviceCaps(LOGPIXELSY);

   // almost always the same in both directions, but sometimes not!
   CString str;
   if (nHorz == nVert)
   {
      str.Format(_T("Your printer supports %d pixels per inch"), nHorz);
   }
   else
   {
      str.Format(_T("Your printer supports %d pixels per inch ")
         _T("horizontal resolution, and %d pixels per inch vertical ")
         _T("resolution"), nHorz, nVert);
   }

   // tell the user
   AfxMessageBox(str);

   // Note: no need to call Detach() because we want the CDC destructor
   // to call FreeDC() on the DC we borrowed from the common dialog
}


注意代码的最后两句注释.  因为它使用CDC这类, 所以可以自动释放GetDefaut获得的DC. 而你自己什么也没做.
[解决办法]
不是说你是猜的有GDI漏泄, 而是说"猜测的代码", ::DeleteObject( dlgPaint.m_pd.hDC );

使用MSDN上例子程序中的方式.

热点排行