CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2004
    Posts
    148

    Bitmap change on button press..

    I wrote this as a lead up to something I need to develop further down the track..
    All I want to do at the moment is change the bitmap image when the button is pressed!

    It is not working this way, and I am stuck for ideas ATM!
    Can anyone tell me why it is not working correctly..

    Cheers


    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <commctrl.h>
    #include <iostream>
    #include "Resource.h"
    
    using namespace std;
    
    #pragma comment(lib, "comctl32.lib")
    
    
    INT_PTR CALLBACK DlgProc( HWND hwnd , UINT msg , WPARAM wparam , LPARAM lparam)
    {
    	HWND Pic  = GetDlgItem(hwnd, IDC_GREEN);
    	HWND Button = GetDlgItem(hwnd, IDC_BUTTON1);
    	
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    		{
    			static HBITMAP hbmp;
    	
    			hbmp = (HBITMAP)LoadImage(0, MAKEINTRESOURCE(IDB_GREEN), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    			SendMessage(Pic, BM_SETIMAGE, IDC_GREEN, (LPARAM)hbmp);
    			SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(0)));
    		}
    	break;
    
    	case WM_COMMAND:
    		switch(HIWORD(wparam))
    		{
    			case BN_CLICKED:
    					if(LOWORD(wparam) == IDC_BUTTON1)
    					{
    						//MessageBox(hwnd, "check for Message", 0 ,0);
    						static HBITMAP hbmp = (HBITMAP)LoadImage(0, MAKEINTRESOURCE(IDB_RED), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_LOADTRANSPARENT);	
    						SendMessage(Pic, BM_SETIMAGE, IDC_GREEN, (LPARAM)hbmp);
    					}
    			break;
    		}
    	break;
    		
    	case WM_CLOSE:
    		{
    			PostQuitMessage(0);
    		}
    	break;		
    			
    	}
    	return false;
    }
    
    INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
        LPSTR lpcmdline, int ncmdshow)
    {
    	
    	InitCommonControls();
    	
    	DialogBox(hinstance, MAKEINTRESOURCE(IDD_MAIN), 0, DlgProc);
            
        return 0;
    }

  2. #2
    Join Date
    Jun 2005
    Location
    Sarajevo, Bosnia and Herzegovina
    Posts
    33

    Re: Bitmap change on button press..

    On first look I think your SendMessage function isn't good, exactly 3rd parameter is IMAGE_BITMAP (WPARAM), not IDC_GREEN.

  3. #3
    Join Date
    Aug 2004
    Posts
    148

    Re: Bitmap change on button press..

    I always thought IMAGE_BITMAP is of UINT type??

    If I put IMAGE_BITMAP in it still does not function correctly?

  4. #4
    Join Date
    May 2005
    Posts
    4,954

    Re: Bitmap change on button press..

    Quote Originally Posted by Code_Nerd
    I always thought IMAGE_BITMAP is of UINT type??
    And how is that related to the fact you are passing the wrong ID?

    If I put IMAGE_BITMAP in it still does not function correctly?
    Did you set the SS_BITMAP style to your pic control?

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  5. #5
    Join Date
    Aug 2004
    Posts
    148

    Re: Bitmap change on button press..

    I am very new to WIN32 programming..

    I am not sure where I am meant to set SS_BITMAP?

    Thankyou

  6. #6
    Join Date
    May 2005
    Posts
    4,954

    Re: Bitmap change on button press..

    • Go to the resource editor
    • Right click on the image ( static ) control
    • Choose properties
    • In the type combo box choose Bitmap


    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  7. #7
    Join Date
    Aug 2004
    Posts
    294

    Re: Bitmap change on button press..

    Also, if IDB_RED and IDB_GREEN are resources, you should:
    1. Pass a valid hInstance as a first parameter to LoadImage
    2. Do not specify LR_LOADFROMFILE flag
    Boris Karadjov
    Brainbench MVP for Visual C++
    http://www.brainbench.com/

  8. #8
    Join Date
    Aug 2004
    Posts
    148

    Re: Bitmap change on button press..

    OK I think I have passed the correct hInstance to LoadImage() and now it seems as if the bitmap is loading correctly and getting put into hbmp..

    @ golanshahar the picture control was already set as Bitmap type in the resource editor.. Thankyou

    My code now looks like this:
    Code:
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <commctrl.h>
    #include <iostream>
    #include <stdlib.h>
    #include "Resource.h"
    
    using namespace std;
    
    #pragma comment(lib, "comctl32.lib")
    
    
    INT_PTR CALLBACK DlgProc( HWND hwnd , UINT msg , WPARAM wparam , LPARAM lparam)
    {
    	HWND Pic  = GetDlgItem(hwnd, IDC_GREEN);
    	HWND Button = GetDlgItem(hwnd, IDC_BUTTON1);
    	
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    		{
    			static HBITMAP hbmp;
    	
    			hbmp = (HBITMAP)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDB_GREEN), IMAGE_BITMAP, 0, 0, 0);
    			if(!hbmp)
    			{
    				MessageBox(hwnd, "Test if no bitmap loaded", 0 ,0);
    			}
    			DWORD dw = GetLastError();
    			char poop[123]; itoa(dw, poop, 100);
    			MessageBox(hwnd, poop, 0 ,0);
    			SendMessage(Pic, BM_SETIMAGE, IDC_GREEN, (LPARAM)hbmp);
    			SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM)LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(0)));
    		}
    	break;
    
    	case WM_COMMAND:
    		switch(HIWORD(wparam))
    		{
    			case BN_CLICKED:
    					if(LOWORD(wparam) == IDC_BUTTON1)
    					{
    						
    						static HBITMAP hbmp = (HBITMAP)LoadImage(GetModuleHandle(0), MAKEINTRESOURCE(IDB_RED), IMAGE_BITMAP, 0, 0, 0);	
    						if(!hbmp)
    							MessageBox(hwnd, "check for Message", 0 ,0);
    						else
    							SendMessage(Pic, BM_SETIMAGE, IDC_GREEN, (LPARAM)hbmp);
    						DWORD dw = GetLastError();
    						char poop[123]; itoa(dw, poop, 100);
    						MessageBox(hwnd, poop, 0 ,0);
    					}
    			break;
    		}
    	break;
    		
    	case WM_CLOSE:
    		{
    			PostQuitMessage(0);
    		}
    	break;		
    			
    	}
    	return false;
    }
    
    INT WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance,
        LPSTR lpcmdline, int ncmdshow)
    {
    	
    	InitCommonControls();
    	
    	DialogBox(hinstance, MAKEINTRESOURCE(IDD_MAIN), 0, DlgProc);
            
        return 0;
    }

  9. #9
    Join Date
    May 2005
    Posts
    4,954

    Re: Bitmap change on button press..

    Your code still incorrect, instead of:
    Code:
    ::SendMessage(Pic, BM_SETIMAGE, IDC_GREEN, (LPARAM)hbmp);
    It should be:
    Code:
    ::SendMessage(Pic, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hbmp);
    Read the documentation of BM_SETIMAGE.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

  10. #10
    Join Date
    Aug 2004
    Posts
    148

    Re: Bitmap change on button press..

    Thankyou for your reply...
    I understand where I went wrong now, however the image is still not changing with the button press?

    What else could it possibly be? Do I have to create a special type of bitmap (8bit etc etc) I am trying to use 24 bit .bmps...

    Thanks again..

  11. #11
    Join Date
    May 2005
    Posts
    4,954

    Re: Bitmap change on button press..

    Look at this thread for sample.

    Cheers
    If a post helped you dont forget to "Rate This Post"

    My Article: Capturing Windows Regardless of Their Z-Order

    Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured