Thread: displaying a bitmap on a button

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    54

    Question displaying a bitmap on a button

    is there an easy way to display a .bmp image onto a button, i have looked at the dialog design functions of msvc but cant find anywhere to add it

    i have also done a search for this but can only find help and code for creating a user-designed button, all i want is the image on the button

    thanx in advance

    korbitz

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Code:
    m_Bitmap = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_BITMAP));
    m_Button.SetBitmap(m_Bitmap);
    Of course, the button must created with the bitmap flag on.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    thanx for the reply, but i should have said i am writing the code in win32 not MFC

    sorry for the confusion, here is what i have gotten so far, but it dont work

    Code:
    BOOL CALLBACK ControlDlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
    	HBITMAP hbitmap;
    	HINSTANCE hInst;
    	int len = 0;
    	int node = 0;
    	switch(message)
    	{
    	case WM_INITDIALOG:
    		
    
    		/* Load the button images */
    		hbitmap = (HBITMAP)LoadImage( hInst,MAKEINTRESOURCE(IDB_BIT_X1), 
    											 IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION );
    
    								
    		/* Now attatch the bitmaps to the buttons */
    		SendDlgItemMessage( hwnd, IDC_BUT_X1_AXIS, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, 
    				   (LPARAM)(HBITMAP)hbitmap );
    
    		
    		return TRUE;
    		break;
    	case WM_CREATE:
    		
    
    		
    		break;

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Can you confirm that the button has the BS_BITMAP style? I'm not sure if you should be using LR_CREATEDIBSECTION.

  5. #5
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    from script.rc

    PUSHBUTTON "+X",IDC_BUT_X1_AXIS,0,0,32,27,BS_BITMAP,
    WS_EX_DLGMODALFRAME

    yeah it has the BS_BITMAP style

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    What's the return value from LoadImage?
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    i set the style in the dialog create thingy and checked that its in the dialog resource

    i also done a debug of the code and it returned NULL, any reason why its returning NULL?

  8. #8
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>i also done a debug of the code and it returned NULL, any reason why its returning NULL?<<

    If you're referring to the return value from LoadImage then that's an error ie a failure to load the image. Try changing, as anonytmouse has implied, the last parameter of LoadImage. Make sure that 'IDB_BIT_X1' is defined as the numeric id of your bitmap. If those quick fixes don't help then check out what GetLastError has to say (use FormatMessage to get some sort of meaningful error message).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  9. #9
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    okay i entered that getlasterror and the error that it reports is

    Invalid Window Handle

  10. #10
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    haha! That's helpful (the joys of GetLastError ).

    Things to check:
    1. Have you included the bitmap you want to use in your project (ie is it listed with the other resources)?
    2. Assuming you have the bitmap included, try a complete rebuild (rebuild all) to ensure the resources are compiled.
    3. Change the last parameter of LoadImage to LR_DEFAULTCOLOR (shouldn't make a difference, but...)
    4. Make sure 'hInst' is valid (try with GetModuleHandle(0) ).

    If you still have no luck after that then zip up and attach a small example project so that someone can take a closer look at it.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  11. #11
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    1. yep
    2. done, no joy
    3. done also, no joy
    4. i changed HINSTANCE hInst; to HINSTANCE hInst = GetModuleHandle(0);, no joy still

    i have included the entire code, but the code in question is on lines 1529-1565

    when running the code, to display the dialog box with the buttons select View->Remote Control

    thanx again for the help

  12. #12
    Registered User
    Join Date
    Nov 2001
    Posts
    54
    no matter, managed to fix it

    Code:
    HBITMAP hbitmap;
    	HINSTANCE hInst = GetModuleHandle(0);
    	HWND g_hTemp;
    	LPVOID lpMsgBuf;
    	int len = 0;
    	int node = 0;
    	switch(message)
    	{
    	case WM_INITDIALOG:
    				
    		/* Load the button images */
    		hbitmap = (HBITMAP)LoadImage( hInst,MAKEINTRESOURCE(IDB_BIT_Z2), 
    											 IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR );
    								
    		/* Now attatch the bitmaps to the buttons */
    		SendDlgItemMessage( hwnd, IDC_BUT_X1_AXIS, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, 
    				   (LPARAM)(HBITMAP)hbitmap );
    not entirely sure what i did but it works

    thanx to everyone that helped, much appreciated

    one last question though, i have 8 buttons on the dialog, do i need to add 8 handles, 8 loadimages and 8 sendmessages???

  13. #13
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>not entirely sure what i did but it works<<

    You used your application instance as the first parameter of LoadImage.

    >>i have 8 buttons on the dialog, do i need to add 8 handles, 8 loadimages and 8 sendmessages<<

    Yes, if they're all bitmap buttons.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  14. #14
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You just posted nearly an exact copy of the original code that you posted! The code in the zip was very different.

    1. You must call GetLastError() immediately after an error occurs, before you call another function. In this case you were picking up the error from SendDlgItemMessage() instead of LoadImage().

    >> one last question though, i have 8 buttons on the dialog, do i need to add 8 handles, 8 loadimages and 8 sendmessages??? <<

    You can do this nicely with some arrays and a loop if you wish:
    Code:
    const WORD wControls[] = { IDC_BUT_X1_AXIS, IDC_BUTTON2,       etc };
    const WORD wBitmaps[]  = { IDB_BIT_Z2,      IMAGE_FOR_BUTTON2, etc };
    
    for (i = 0; i < number_of_controls;i++)
    {
    	hBitmap = LoadImage(hInst, MAKEINTRESOURCE(wBitmaps[i]), ...);
    	SendDlgItemMessage(hwnd, wControls[i], ...);
    }
    Remember that you should be deleting these bitmaps when the dialog is destroyed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replace button with a bitmap
    By Swarvy in forum Windows Programming
    Replies: 10
    Last Post: 09-12-2008, 11:43 AM
  2. Replies: 2
    Last Post: 06-28-2008, 08:30 PM
  3. Bitmap Displaying Problem
    By MadCow257 in forum Game Programming
    Replies: 0
    Last Post: 01-25-2005, 05:21 PM
  4. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM
  5. Displaying a seperate bitmap for each menu button
    By Magriep in forum Windows Programming
    Replies: 1
    Last Post: 03-26-2002, 11:40 PM