1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND hButton;
HBITMAP hMask = (HBITMAP)LoadImage((HINSTANCE)GetModuleHandle(NULL),"bmpmask.bmp",
IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
HBITMAP hColor = (HBITMAP)LoadImage((HINSTANCE)GetModuleHandle(NULL),"bmpcolor.bmp",
IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
switch(msg)
{
case WM_CREATE:
hButton = CreateWindow("BUTTON",NULL,WS_CHILD | WS_VISIBLE | BS_BITMAP,
110,10,40,40,hwnd,(HMENU)IDC_TEST,NULL,NULL);
//The following works in WM_CREATE case only:
//SendMessage(hButton,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hColor);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_TEST:
{
HDC hdc = GetDC(hwnd);
HDC hdcMem = CreateCompatibleDC(hdc);
HDC hdcBuffer = CreateCompatibleDC(hdc);
HBITMAP hbmButton = CreateCompatibleBitmap(hdc,40,40);
// The following BitBlt's ARE successful at copying the
// regular and transparent bitmaps to the main window:
HGDIOBJ hOldBitmap = SelectObject(hdcBuffer,hbmButton);
SelectObject(hdcMem,hMask);
BitBlt(hdc, 0, 40, 40, 40, hdcMem, 0, 0, SRCAND); //To main window, transparent image
BitBlt(hdc, 0, 0, 40, 40, hdcMem, 0, 0, SRCCOPY); //To main window, solid bitmap
BitBlt(hdcBuffer, 0, 0, 40, 40, hdcMem, 0, 0, SRCAND); //To buffer
SelectObject(hdcMem,hColor);
BitBlt(hdc, 0, 40, 40, 40, hdcMem, 0, 0, SRCPAINT); //To main window, transparent image
BitBlt(hdc, 40, 0, 40, 40, hdcMem, 0, 0, SRCCOPY); //To main window, solid bitmap
BitBlt(hdcBuffer, 0, 0, 40, 40, hdcMem, 0, 0, SRCPAINT); //To buffer
// This same function from above doesn't work here, however:
// (This DID work previously, so I'm not sure what I'm missing now...
SendMessage(hButton,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hColor);
// And it also doesn't work using the transparent bitmap in memory:
SendMessage(hButton,BM_SETIMAGE,IMAGE_BITMAP,(LPARAM)hbmButton);
SelectObject(hdcBuffer,hOldBitmap);
SelectObject(hdcMem,hOldBitmap);
DeleteObject(hbmButton);
DeleteDC(hdcBuffer);
DeleteDC(hdcMem);
ReleaseDC(hwnd,hdc);
}
break;
default:
return false;
}
break;
case WM_DESTROY:
DeleteObject(hMask);
DeleteObject(hColor);
PostQuitMessage (0);
break;
default:
return DefWindowProc (hwnd, msg, wParam, lParam);
}
return 0;
}
|