views:

1363

answers:

3

Edit: I think the WM_CREATE message isn't sent during the creation of child windows (namely my button). So by calling SendMessage during WM_CREATE, I'm sending a message to a window that hasn't been created yet. The solution for now is to call SendMessage() during the WM_SHOWWINDOW message. Do child windows send WM_CREATE messages at creation?

Why isn't the bitmap displaying on the button? The bitmap is 180x180 pixels.

I have a resource file with:

Bit BITMAP bit.bmp

I then create the main window and a child "BUTTON" window with:

HWND b, d;

b = CreateWindow(L"a", NULL, WS_OVERLAPPEDWINDOW, 0, 0, 500, 500, 0, 0, 
                  hInstance, 0);

d = CreateWindow(L"BUTTON", NULL, WS_CHILD | WS_VISIBLE | BS_BITMAP, 
                 10, 10, 180, 180, b, 200, hInstance, 0);

Then, in my windows procedure, I send the "BUTTON" window the "BM_SETIMAGE" message with:

HBITMAP hbit; 

case WM_CREATE:    // It works if I change this to: case WM_SHOWWINDOW 

hbit = LoadBitmap(hInstance, L"Bit");

SendMessage(d, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);

LoadBitmap() is returning a valid handle because It isn't returning NULL, and I'm able to display the bitmap in the client area using the BitBlt() function. So I'm either not sending the message correctly, or I'm not creating the "BUTTON" window correctly.

What am I doing wrong?

Thanks!

A: 

How are you verifying that WM_CREATE isn't getting called? Since BUTTON isn't your window class (but rather defined by the OS) it owns the WndProc for the window, not you - therefore WM_CREATE shouldn't be called for the button in your code, because BUTTON isn't your class.

If you want to receive messages for the button, you'll have to subclass it, and then provide your own WndProc.

Andy
What I tried to explain in my edit is that WM_CREATE is only sent to the main window, not to the button. I wasn't saying that WM_CREATE isn't being sent. I thought that maybe my WndProc would recieve WM_CREATE messages during the creation of its child windows. It does after all receive WM_COMMAND messages that were generated from its child button window.
tyler
+2  A: 

The window procedure for for your window class "a" gets called with WM_CREATE when a window of that class is created. This is during your first call to CreateWindow, which is before you create the child BUTTON window. WM_CREATE means "you are being created" - it doesn't mean "a child is being created".

The solution is to call d = CreateWindow(L"BUTTON"...) in the WM_CREATE handler for class "a":

case WM_CREATE:
    d = CreateWindow(L"BUTTON", NULL, WS_CHILD | WS_VISIBLE | BS_BITMAP, 
                     10, 10, 180, 180, hwnd, 200, hInstance, 0);
    hbit = LoadBitmap(hInstance, L"Bit");
    SendMessage(d, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hbit);
RichieHindle
Thanks. I thought the same thing but when I tried it I actually get no button at all (not even an outline). The only thing that has worked has been to put the the SendMessage() in WM_SHOWWINDOW.
tyler
I bet that's because you're calling d = CreateWindow(..., b, ...) rather than d = CreateWindow(..., hwnd, ...) - remember, you're within the first call to CreateWindow, so b hasn't yet been assigned to.
RichieHindle
Wow, brilliant..that completely makes sense. Thanks man.
tyler
A: 

take a look here:

http://winapi.foosyerdoos.org.uk/info/user_cntrls.php

nir