CPen,HFONT用完一定要释放,不然会造成GDI对象的增加,程序崩溃

1.HFONT的释放

    HFONT hFont,hOldFont;    
    LOGFONT lf;
    ::ZeroMemory(&lf, sizeof(LOGFONT));
    lf.lfHeight = m_pGrid->pCell[i][k].lfFont.lfHeight;// nHeight
    lf.lfWidth = 0;        // nWidth
    lf.lfEscapement = 0;      // nEscapement
    lf.lfOrientation = 0;      // nOrientation
    lf.lfWeight = m_pGrid->pCell[i][k].lfFont.lfWeight; // nWeight
    lf.lfItalic = m_pGrid->pCell[i][k].lfFont.lfItalic;// bItalic
    lf.lfUnderline = m_pGrid->pCell[i][k].lfFont.lfUnderline;// bUnderline
    lf.lfStrikeOut = FALSE;     // cStrikeOut
    lf.lfCharSet = DEFAULT_CHARSET;    // nCharSet
    lf.lfOutPrecision = OUT_DEFAULT_PRECIS;  // nOutPrecision
    lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;   // nClipPrecision
    lf.lfQuality = DEFAULT_QUALITY;    // nQuality
    lf.lfPitchAndFamily = DEFAULT_PITCH|FF_SWISS;// nPitchAndFamily
    lstrcpy(lf.lfFaceName,m_pGrid->pCell[i][k].lfFont.lfFaceName);  // lpszFacename 
   

    hFont = ::CreateFontIndirect(&lf);//创建字体
    hOldFont = (HFONT)::SelectObject(hDstDC,hFont);//DC选中字体

    ... ...

    pdc->SelectObject(hOldFont);
    ::DeleteObject(hFont);

 

2.CPen的释放

 CPen pen(PS_SOLID,1,m_GP_clrFrame);
 CPen *pOldPen;
 pOldPen = pdc->SelectObject(&pen);

 ... ...

 pdc->SelectObject(pOldPen);
 pen.DeleteObject();

 

你可能感兴趣的:(CPen,HFONT用完一定要释放,不然会造成GDI对象的增加,程序崩溃)