通常,我們使用的hicon對象隻需用destroyicon後就不存在記憶體洩漏了,但是當我們使用geticoninfo後會發現程式gdi資源存在洩漏,原因是geticoninfo會産生2個hbitmap對象,即iconinfo結構體中的hbmcolor和hbmmask,需要deleteobject掉。切記切記!!!
cstring strvalue(_t(""));
hicon hicon = null;
hicon = extracticon(afxgetinstancehandle(), strvalue,
0);
if(hicon)
{
crect rcicon;
getclientrect(&rcicon);
iconinfo icoinfo;
geticoninfo(hicon,
&icoinfo);
rcicon.left +=
rcicon.width()/2-icoinfo.xhotspot;
rcicon.top +=
rcicon.height()/2-icoinfo.yhotspot;
deleteobject(icoinfo.hbmcolor);
deleteobject(icoinfo.hbmmask);
drawicon(pdc->m_hdc,
rcicon.left, rcicon.top,
hicon);
destroyicon(hicon);
}