Drawing a bitmap or icon in a button

I'm trying to draw a icon or a bitmap in a button... I have set BS_OWNERDRAW style to the button and í'm handling WM_DRAWITEM message but a cannot draw icon or bitmap i don't know why... I'm using pure windows API. I tried with DrawIcon, DrawIconEx, BitBlt...

PS. Sorry for my bad english.
First off you English is fine. Next, do you know that you are opening the image file correctly? Have you checked "GetLastError()" for any error codes? Are you getting compile errors? Linker errors? Or is the icon just not rendering?
Can you post your code for creating the button and handling WM_DRAWITEM?
Use LoadImage() to load your icon, then do this to put it on your button --

SendMessage(GetDlgItem(hwnd,IDB_BUTTON),BM_SETIMAGE, (WPARAM)IMAGE_ICON,(LPARAM)hMyIcon);

Where IDB_BUTTON is your button identifier and where hMyIcon is the icon you loaded with LoadImage().

Also, instead of BS_OWNERDRAW style for your button, you should make it BS_ICON, assuming your button is coded in your resource file.
Last edited on
Ok sorry that was my bad using LoadImage() because i was trying to load from a file and i didn't put LR_LOADFROMFILE sorry thank you guys for your time.
i am also trying to make a bitmap a button but im still a bit confused, i dont know how to use any of functions mentioned already. i am a beginner and i have never done buttons before. could you post some sample code?

I have made the button but need to insert the bitmap

thanks
1
2
3
4
5
6
#define IDB_BUTTON 100
#define IDI_ICON 101

HICON hIcon = LoadImage(hinstance,MAKEINTRESOURCE(IDI_ICON),IMAGE_ICON,25,25,NULL);

SendDlgItemMessage(hwnd,IDB_BUTTON,BM_SETIMAGE,(WPARAM)IMAGE_ICON,(LPARAM)hIcon);

The above assumes that you have registered your icon in your resource file and that you have set your button to BS_ICON in your resource file. For example --

IDI_ICON ICON "myicon.ico"

then when you put your button further below in your resource file --

PUSHBUTTON "", IDB_BUTTON, 10, 10, 25, 25, BS_ICON

If you still can't get it working it means you have not accurately followed the code I've posted, for I do this ALL the time.
Last edited on
heres the code i used to create the button
1
2
3
4
5
6
7
#define IDC_BUTTON5  4014

HWND hwndButton5;

hwndButton5 = CreateWindow( L"button",L"Drum",
WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON, cxBlock, cyBlock*20, 5*cxBlock, 2*cyBlock,
hWnd, (HMENU)IDC_BUTTON5, hInst,NULL);


does the button have to have the code you posted to become an image?
Did your code work???

If not, then adapt it to what I've showed you above. As long as you have an ID to the button, as in your code above, i.e., IDC_BUTTON5, and as long as you have registered your icon, as I have demonstrated, then you should have zero problem adapting it to work. You should also set the BS_ICON flag in your CreateWindow() function if you insist on making your button that way.
Last edited on
Oh, and if you are going to create your button using CreateWindow() then instead of using SendDlgItemMessage() you would simply use SendMessage().
the code you gave is just causing errors. the hinstance and hwnd have red underlines

You have to use YOUR OWN HINSTANCE variable and YOUR OWN HWND variable in those places!!!

Instead of using CreateWindow(), why don't you try following my instructions to the letter, which means using the RESOURCE file to indentify your icon and to create your button. Then use the code I supplied to set the icon on the button.

As I said, I do this ALL THE TIME with the EXACT SAME CODE as given you.
Topic archived. No new replies allowed.