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;
}
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.
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?
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? :confused:
Quote:
If I put IMAGE_BITMAP in it still does not function correctly?
Did you set the SS_BITMAP style to your pic control?
Cheers
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 :)
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
Re: Bitmap change on button press..
Also, if IDB_RED and IDB_GREEN are resources, you should:
- Pass a valid hInstance as a first parameter to LoadImage
- Do not specify LR_LOADFROMFILE flag
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;
}
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
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..
Re: Bitmap change on button press..
Look at this thread for sample.
Cheers