How can i show my button bitmap picture?

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2276 Views - Last Post: 31 August 2011 - 05:29 PM Rate Topic: -----

#1 Helegurbann  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-July 11

How can i show my button bitmap picture?

Posted 27 August 2011 - 12:24 AM

#include <windows.h>
HBITMAP hBmp;
HWND button;
LRESULT CALLBACK P(HWND A,UINT B,WPARAM C,LPARAM D){
        switch(B)/>{
                  case WM_CREATE:
                      button= CreateWindow("button","Button Pic",WS_CHILD|WS_VISIBLE|BS_BITMAP,30,75,75,25,A,(HMENU)hBmp,NULL,NULL);
                SendMessage(
            (HWND)button,
            (UINT) BM_SETIMAGE,
            (WPARAM) IMAGE_BITMAP,
            (LPARAM) hBmp
            );
                  break;
                  case 0x2:
                       PostQuitMessage(0);
                       break;
                       default:
                       return DefWindowProc(A,B,C,D);
                  }
                  return 0;
        }
        char title[]="Button Pic";
int WINAPI WinMain(HINSTANCE A,HINSTANCE B,LPSTR C,int d){
    hBmp=(HBITMAP)LoadImage(NULL,"C:\\exam.bmp",IMAGE_ICON,32,32,LR_LOADFROMFILE);
    HWND E;
    MSG F;
    WNDCLASS G={0};
    G.hInstance=A;
    G.lpszClassName=title;
    G.lpfnWndProc=P;
    G.hbrBackground=GetSysColorBrush(COLOR_3DFACE);
    RegisterClass(&G);
    CreateWindow(G.lpszClassName,title,0xcf0000|WS_VISIBLE,50,150,600,450,0,0,A,0);
    while(GetMessage(&F,NULL,0,0)){
            TranslateMessage(&F);
            DispatchMessage(&F);
            }
            return F.wParam;
    }




Is This A Good Question/Topic? 0
  • +

Replies To: How can i show my button bitmap picture?

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,603
  • Joined: 19-February 09

Re: How can i show my button bitmap picture?

Posted 28 August 2011 - 10:26 AM

Hi, maybe you have found you can use IMAGE_BITMAP instead of IMAGE_ICON, it can be worth checking image was loaded.

hBmp = (HBITMAP) LoadImage(NULL, "C:\\exam.bmp", 
          IMAGE_BITMAP,32,32, LR_LOADFROMFILE);

if( hBmp == NULL)
{ MessageBox(NULL, TEXT("LoadImage failed!"),
      TEXT("Button LoadImage"), NULL);
}


Was This Post Helpful? 0
  • +
  • -

#3 Helegurbann  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-July 11

Re: How can i show my button bitmap picture?

Posted 28 August 2011 - 01:09 PM

Hi.I tried this.But it doesn't work.
Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,603
  • Joined: 19-February 09

Re: How can i show my button bitmap picture?

Posted 28 August 2011 - 03:00 PM

View PostHelegurbann, on 28 August 2011 - 11:09 PM, said:

Hi.I tried this.But it doesn't work.


What happens do you get any messages?

Here I've move LoadImage to WM_CREATE, to make it easier to show.
Removed hBmp from CreateWindow and used PostMessage.

    case WM_CREATE:
    {
      hBmp = (HBITMAP) LoadImage(NULL,"C:\\exam.bmp",
              IMAGE_BITMAP,32,32,LR_LOADFROMFILE);
      if(hBmp == NULL)
      {  MessageBox(NULL, "LoadImage failed!",
            "Button LoadImage", NULL);
      }

      button = CreateWindow("button","",
              WS_CHILD|WS_VISIBLE|BS_BITMAP,
              30,75,75,25,A,NULL,NULL,NULL);
      if(button == NULL)
      {  MessageBox(NULL, "CreateWindow failed!",
                    "Button LoadImage", NULL);
      }

      PostMessage( (HWND)   button,
            (UINT)   BM_SETIMAGE,
            (WPARAM) IMAGE_BITMAP,
            (LPARAM) hBmp );

      break;
    }




See if that will work.


You can also try free-ing the bitmap at close.

    case 0x2:
      PostQuitMessage(0);
      DeleteObject (hBmp);
      break;


Was This Post Helpful? 0
  • +
  • -

#5 Helegurbann  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-July 11

Re: How can i show my button bitmap picture?

Posted 28 August 2011 - 04:17 PM

I added libgdi32.a,magnified my button size and tried exam.ico ..But result:LoadImage failed.

This post has been edited by Helegurbann: 28 August 2011 - 04:24 PM

Was This Post Helpful? 0
  • +
  • -

#6 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,281
  • Joined: 28-March 11

Re: How can i show my button bitmap picture?

Posted 28 August 2011 - 05:40 PM

If LoadImage failed, try calling GetLastError after the LoadImage call and post the return value from it. Does "c:\exam.bmp" exist?
Was This Post Helpful? 1
  • +
  • -

#7 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,603
  • Joined: 19-February 09

Re: How can i show my button bitmap picture?

Posted 28 August 2011 - 05:57 PM

Yes, or the bitmap could be invalid.

In Paint, you can set the Image/Attributes to 100 by 100. Draw what you want. Then Image/Stretch/Skew to 32 by 32. Save as bitmap.


If the file is in same folder as the executable file, the filename is the only name needed.
Was This Post Helpful? 0
  • +
  • -

#8 Helegurbann  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-July 11

Re: How can i show my button bitmap picture?

Posted 28 August 2011 - 08:28 PM

I changed this:
case WM_CREATE:
{
  hBmp = (HBITMAP) LoadImage(NULL,"C:\\exam.jpg",
          IMAGE_BITMAP,100,100,LR_LOADFROMFILE);
  if(hBmp == NULL)
  {  MessageBox(NULL, "LoadImage failed!",
        "Button LoadImage", MB_OK);
        DWORD GetLastError(void);
  }

  button = CreateWindow("button","",
          WS_CHILD|WS_VISIBLE|BS_BITMAP,
          30,75,200,200,A,NULL,NULL,NULL);
  if(button == NULL)
  {  MessageBox(NULL, "CreateWindow failed!",
                "Button LoadImage", MB_OK);
  }

  PostMessage( (HWND)   button,
        (UINT)   BM_SETIMAGE,
        (WPARAM) IMAGE_BITMAP,
        (LPARAM) hBmp );

  break;


This post has been edited by Helegurbann: 28 August 2011 - 08:28 PM

Was This Post Helpful? 0
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,603
  • Joined: 19-February 09

Re: How can i show my button bitmap picture?

Posted 29 August 2011 - 12:45 PM

A jpg file is not a bitmap, you need a bmp file. You can save one from a graphics program.
Was This Post Helpful? 0
  • +
  • -

#10 Helegurbann  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-July 11

Re: How can i show my button bitmap picture?

Posted 29 August 2011 - 01:06 PM

bmp-jpg-jpeg files have alike binary values.I know this.Because i tried in a before time a jpg file to transfer a bmp file with C++ :)
Was This Post Helpful? 0
  • +
  • -

#11 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1342
  • View blog
  • Posts: 4,603
  • Joined: 19-February 09

Re: How can i show my button bitmap picture?

Posted 29 August 2011 - 01:27 PM

Is your program working? I might try that myself see what happens.
Was This Post Helpful? 0
  • +
  • -

#12 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,281
  • Joined: 28-March 11

Re: How can i show my button bitmap picture?

Posted 29 August 2011 - 02:31 PM

No, a jpeg is NOT the same as a bitmap. The headers are different, the compression is different. The binary is NOT the same. You cannot load a jpeg with LoadImage. Acutally, you can load jpgs with the OLE APIs or something like that.

Now, you keep posting code and saying you changed your code to this.. You are not telling us what the return value of GetLastError is OR if your code is working. As I do not have a C compiler I cannot test your code.

You do have a problem with your placement of GetLastError, you have it AFTER a call to MessageBox so as long as there is no error with MessageBox, the procedure will call SetLastError with a value of 0, If you want to get the error code for an API call like LoadImage, you have to put GetLastError RIGHT AFTER the API call you are checking. So in your case, put it right after
 if(hBmp == NULL)

GetLastError goes here BEFORE your call to MessageBox
Was This Post Helpful? 0
  • +
  • -

#13 Helegurbann  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-July 11

Re: How can i show my button bitmap picture?

Posted 29 August 2011 - 08:33 PM

Hi,i made this and this is not working again:
  if(hBmp == NULL)
  {  if(GetLastError()){
      SetLastError(0);  
 MessageBox(NULL, "LoadImage failed!",
                "Button LoadImage", MB_OK);
  }}


#define,I don't understand this cause:(

This post has been edited by Helegurbann: 29 August 2011 - 08:43 PM

Was This Post Helpful? 0
  • +
  • -

#14 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,281
  • Joined: 28-March 11

Re: How can i show my button bitmap picture?

Posted 30 August 2011 - 07:19 AM

What is not working? What is the return value of GetLastError?
Was This Post Helpful? 0
  • +
  • -

#15 Helegurbann  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 64
  • Joined: 23-July 11

Re: How can i show my button bitmap picture?

Posted 30 August 2011 - 12:05 PM

Maybe LoadImage?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2