Everytime you do GetDC
, you are leaking a DC. You NEED to ReleaseDC
.
Even if you do DC = GetDC(NULL)
you need to do ReleaseDC(NULL, DC);
. If you do DC = GetDC(hwnd)
you need to do ReleaseDC(hwnd, DC);
.
In the above code, you don't need GetDC.. Use the DC that is inside the PaintStruct.
Secondly, for usage with menu, it must be COMPATIBLE.. You can't just use CreateBitmap
and hope that the DC's compatible..
HBITMAP bitmap(image imgImage) //creates a copy of imgImage or whatever is passed to this function! Beware!
{
BITMAP bm;
GetObject((HBITMAP)imgImage, sizeof(bm), &bm);
HDC DC = GetDC(NULL);
HDC MemDCExercising = CreateCompatibleDC(DC); //Create a DC compatible with our Display/Monitor.. 32-bit.
HBITMAP outbitmap = CreateCompatibleBitmap(DC, bm.bmWidth, bm.bmHeight); //create a bitmap that is compatible with our DC.
HBITMAP oldbitmap = (HBITMAP) SelectObject(MemDCExercising, outbitmap);
DrawHBITMAPtoHDC(imgImage, MemDCExercising); //good.
SelectObject(MemDCExercising, oldbitmap);
DeleteDC(MemDCExercising);
ReleaseDC(NULL, DC); //good.
HMENU hMenu = NULL;
if(primeiromenu)
hMenu = mnuBar;
else
hMenu = MenuHandle;
SetMenuItemBitmaps(hMenu,ID,MF_BYCOMMAND,(HBITMAP)outbitmap ,(HBITMAP)outbitmap);
return outbitmap; //we return it so that later we can call DeleteObject to free its memory.
}
The only thing I'm not sure about is if the DrawHBITMAPToHDC
call will work. But try it and let me know.