Click here to Skip to main content
14,119,479 members
Rate this:
 
Please Sign up or sign in to vote.
Hello, anyone can tell me how to use a bitmap as a button, actually i can create a static control and could set a picture to it but the thing is that i don't know how to use it as a button, i am using c++ win32.
This is how i create the bitmap

Code:
HWND Profile_Stuff(HWND hWnd, HINSTANCE hInst)
{
        HWND Profile_Pic;

Profile_Pic = CreateWindow("STATIC", NULL, SS_BITMAP|WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_BORDER, 5,5,33,33, hWnd, NULL, hInst, NULL);
	HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "camera1.jpg", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
	if(hBmp == NULL){
		MessageBox(NULL, "Error while loading image", "Error", MB_OK|MB_ICONERROR);
	}
	SendMessage(Profile_Pic, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
	return 0;
    }


then i call the function in main window wm_create message handler which creates it successfully, now i don't know to use it as a button.
Posted

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The button class actually suports this. So no need to use a static control. This link should get you going...


How can I create button showing icon and text using BS_ICON and sending BM_SETIMAGE?[^]

   
Comments
Member 11593571 5-Aug-15 14:41pm
   
oh man the thing you recommended works perfect but now there is another problem, how do i change the picture when the mouse cursor is on the button. like when you bring your mouse pointer on a normal button the color changes a to a little blue, how am going to do that in my bitmap kind of button. and thank
Member 11593571 7-Aug-15 0:26am
   
hey can anybody answer me?
Jeremy Falcon 7-Aug-15 9:51am
   
Look into the WM_MOUSEHOVER and WM_MOUSELEAVE messages. You can use a similar mechanism to set one bitmap on entry and revert it to the original upon exit. The only thing different to note is you'd now want to store your bitmaps in memory and keep them there for the life of the control rather than load them with every event.
Member 11593571 8-Aug-15 0:32am
   
well how am i going to store bitmaps in memory, if i create a bitmap in resources of my project would it store it in memory
Jeremy Falcon 10-Aug-15 10:07am
   
Use this as a point of reference.

http://www.codeproject.com/Articles/530/A-Better-Bitmap-Button-Class

It's in MFC, but it should get you pointed in the right direction to port it to pure API.
Jeremy Falcon 10-Aug-15 10:08am
   
This is a good example too, that uses a web technique...

http://www.codeproject.com/Articles/758/CHoverButton-A-simple-hoverbutton-with-one-bitmap

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190518.1 | Last Updated 5 Aug 2015
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100