Transparent Bitmap on Button Control?

Hello -

I'm having an issue placing a transparent bitmap on a button control. Below is the current code.

At one point, I was able to put a loaded bitmap (not the transparent one, though) on the button after clicking the button, so I'm not sure why that doesn't work anymore. It only works now during the handling of WM_CREATE for some reason. More importantly, I need to know how to get a transparent bitmap, from the compatible bitmap in memory, to the button control.

The operation of my test program is simple - it just creates a button, and upon clicking that button, the color and mask images show up, along with the transparent image. That works fine. Then the button is supposed to display the transparent bitmap as well. That doesn't work.

Here's my WndProc:

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;
}


I'm fairly new to GDI stuff, so I'm sure it's something obvious I'm missing.
Your raster op codes should be, in order:

SRCAND --where all pixels in the mask image are white (keep dest color) or black (blacken dest color)

SRCPAINT -- where all pixels in the image are the colors you want, and only lie in the area where there was black in the mask image.

I know that it is a bit confusing with all the zillions of raster ops, but for now stick to those two (and SRCCOPY for stuff without transparency).

Hope this helps.
Last edited on
It does not help. Perhaps you should refrain from replying to a post when you clearly have not read the contents of the included source code. You're wasting anyone's time who reads this, because the code I included already contains the raster ops you mention, and in the correct order. My question is not about how to use them. In the code, they work perfectly when I'm placing images on the main window. My question is about why this does not work in the same manner for button controls, and how to correctly do so.
Perhaps if you want any help at all you might consider that I know something you don't, and that you have misunderstood the answer. Or, at the very worst, you can add me to your "ignore" list.

Asking for help and then insulting those who reply will not encourage anyone else to try to help, even if he can explain the answer in a way you find more palatable.

[edit] I regret that my answer was incomplete, but that doesn't justify the abuse...
Last edited on
closed account (z05DSL3A)
http://msdn.microsoft.com/en-us/library/bb775941(VS.85).aspx
I would consider that you know something I don't, if in fact you had been able to tell me something I didn't know! Quite a silly statement on your part.
Last edited on
Topic archived. No new replies allowed.