user4573047
user4573047

Reputation:

C++ Loading an Icon on to a Button

I'm trying to load an icon on to my button. I'm using Code::Blocks. I really don't care whether the image is a BITMAP (.bmp) or an ICON (.ico). Here's the code I have below for case WM_CREATE.

case WM_CREATE:
    HWND button;
    button = CreateWindow("BUTTON", "My Button", WS_VISIBLE | WS_CHILD,
                          225, 225, 100, 25, hwnd, NULL, NULL, NULL);
break;

Is it possible to add an icon to a button without it getting very complicated? Thanks for your time :)

Upvotes: 0

Views: 1948

Answers (1)

Cyclonecode
Cyclonecode

Reputation: 30001

You could send a BM_SETIMAGE message to your button like this:

case WM_CREATE:
  // load your bitmap from the executable, you should also be able
  // to load the bitmap / or icon from disk using `LoadImage`
  HBITMAP hBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE("<ResourceID>"));

  // notice the BS_BITMAP flag (you could also use `BS_ICON`)
  // perhaps you also would like to add BS_PUSHBUTTTON style
  HWND button = CreateWindow("BUTTON", 
                             "My Button", 
                             WS_VISIBLE | WS_CHILD | BS_BITMAP,
                             225, 225, 100, 25, 
                             hwnd, 
                             NULL, 
                             hInstance, 
                             NULL);
  // set the bitmap of your window, you could use `BM_SETICON` in
  // case you're using an icon rather than a bitmap
  SendMessage(button, BM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBitmap);
break;

To load a bitmap from a file on disk you could do something like this:

HBITMAP hBitmap = (HBITMAP)LoadImage(NULL, 
                                     "NameOfBitmap.bmp", 
                                     IMAGE_BITMAP,
                                     0, 0,
                                     LR_LOADFROMFILE); 

References
Button Messages
LoadBitmap()
LoadIcon()
LoadImage()
Button Styles

Upvotes: 1

Related Questions