读书人

哪位高手可以帮小弟我瞧瞧两个函数中D

发布时间: 2014-01-19 01:28:51 作者: rapoo

谁可以帮我瞧瞧两个函数中DC释放的错误?急
有两个要求:
在所有GetDC()和ReleaseDC()的地方,用CClientDC
在所有CreateXXXDC和DeleteDC的地方,用CDC。
求大神帮忙修改,分数都给大家了

void Shadow::DrawSemiTransparentRect(CDC *pDstDC, CRect rtTran, COLORREF clrSrc)
{
int nSavedDC = pDstDC->SaveDC();

int nWidth = rtTran.Width();
int nHeight = rtTran.Height();

CDC bmpDC;
bmpDC.CreateCompatibleDC(pDstDC);
CBitmap bmp;

bmp.CreateCompatibleBitmap(pDstDC, nWidth, nHeight);
CBitmap* pOldBmp = (CBitmap*)bmpDC.SelectObject(&bmp);
bmpDC.BitBlt(0, 0, nWidth, nHeight, pDstDC, rtTran.left, rtTran.top, SRCCOPY);

HDC hDIBDC = CreateCompatibleDC(NULL);

BITMAPINFO hdr;
ZeroMemory(&hdr, sizeof(BITMAPINFO));

hdr.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
hdr.bmiHeader.biWidth = nWidth;
hdr.bmiHeader.biHeight = nHeight;
hdr.bmiHeader.biPlanes = 1;
hdr.bmiHeader.biBitCount = 32;

BYTE * pbtPixels = NULL;
HBITMAP hDIBitmap = CreateDIBSection(hDIBDC, (BITMAPINFO *)&hdr, DIB_RGB_COLORS, (void**)&pbtPixels, NULL, 0);
HBITMAP hOldBmp = (HBITMAP)SelectObject(hDIBDC, hDIBitmap);
BitBlt(hDIBDC, 0, 0, nWidth, nHeight, bmpDC.m_hDC, 0, 0, SRCCOPY);
SelectObject(hDIBDC, hOldBmp);

int nPixelSize = 4;

BYTE btSR = GetRValue(clrSrc);
BYTE btSG = GetGValue(clrSrc);
BYTE btSB = GetBValue(clrSrc);

for (int i = 0 ; i < nHeight ; i ++)
{
for (int j = 0 ; j < nWidth ; j ++)
{
BYTE btB = pbtPixels[i * nWidth * nPixelSize + j * nPixelSize ] ;
BYTE btG = pbtPixels[i * nWidth * nPixelSize + j * nPixelSize + 1 ] ;
BYTE btR = pbtPixels[i * nWidth * nPixelSize + j * nPixelSize + 2 ] ;

btB = (btSB + btB ) >> 1 ;
btG = (btSG + btG ) >> 1 ;
btR = (btSR + btR ) >> 1 ;

pbtPixels[i * nWidth * nPixelSize + j * nPixelSize ] = btB ;
pbtPixels[i * nWidth * nPixelSize + j * nPixelSize + 1 ] = btG ;
pbtPixels[i * nWidth * nPixelSize + j * nPixelSize + 2 ] = btR ;

}
}

SetDIBitsToDevice(pDstDC->GetSafeHdc(),rtTran.left,rtTran.top,nWidth,nHeight,
0,0,0,nHeight,(void*)pbtPixels,(BITMAPINFO*)&hdr,DIB_RGB_COLORS);

bmpDC.SelectObject(pOldBmp);
bmp.DeleteObject();
delete [] pbtPixels ;
bmpDC.DeleteDC();
DeleteObject(hDIBDC);
DeleteObject(hDIBitmap);
pDstDC->RestoreDC(nSavedDC);
pbtPixels=NULL;

pDstDC->RestoreDC(0);
}

HBITMAP Shadow::GetScreenHBITMAP()
{
HDC hScrDC = ::GetDC(NULL);
ASSERT(hScrDC);

HDC hMemDC = CreateCompatibleDC(hScrDC);
ASSERT(hMemDC);

int nWidth = GetSystemMetrics(SM_CXSCREEN);


int nHeight = GetSystemMetrics(SM_CYSCREEN);

HBITMAP hBitmap = CreateCompatibleBitmap(hScrDC, nWidth, nHeight);
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);

BitBlt(hMemDC, 0, 0, nWidth, nHeight, hScrDC, 0, 0, SRCCOPY);

hBitmap = (HBITMAP)SelectObject(hMemDC, hOldBitmap);

DeleteDC(hMemDC);
DeleteDC(hScrDC);

return hBitmap;
}


CreateCompatibleDC创建的请用DeleteDC释放。不要用DeleteObject(hDIBDC);
[解决办法]

CreateCompatibleDC创建的请用DeleteDC释放。不要用DeleteObject(hDIBDC);那你知道pbtPixels这玩意怎么释放吗?
DeleteObject 释放那个返回的HBITMAP,那个内存里面会自己释放掉的。
msdn原话:The system closes the handle to that memory when you later delete the DIB by calling the DeleteObject function

读书人网 >C++

热点推荐