0

I would like to draw my program's icon on an "owner-drawn" button. This could be either an icon in the resource file, or the generic Windows icon. But, even after endless searching, I have not been able to find the code for this. I have come across bits and pieces of answers. But, no complete explanations.

Sorry that I have no code to post. I am totally lost on this one. Either standard Win API or GDI+ will work for me.

anachronon
  • 17
  • 5
  • Does this answer your question? [How to draw ICON in GDI+?](https://stackoverflow.com/questions/4286152/how-to-draw-icon-in-gdi) – Ken White Nov 03 '20 at 01:24
  • Please post some code so we understand the framework you are working in. – Raymond Chen Nov 03 '20 at 13:56
  • No, unfortunately, that link just led me down a rabbit hole, and I never found the bottom. I followed link after link, with no answers. There must be an easy way to do this. – anachronon Nov 04 '20 at 00:22

1 Answers1

0

When you create the button, add BS_ICON style, then get the handle to your icon, you can use LoadImage.

Finally send BM_SETIMAGE message with the handle to your icon.

Here is the code sample:

HWND button1 = CreateWindow(TEXT("Button"), TEXT("OK"), WS_VISIBLE | WS_CHILD | BS_PUSHBUTTON | BS_ICON,
    50, 50, 50, 50, hwnd, (HMENU)1001, hInstance, NULL);
HICON hIcon = (HICON)LoadImage( // returns a HANDLE so we have to cast to HICON
    NULL,             // hInstance must be NULL when loading from a file
    L"iconname.ico",   // the icon file name
    IMAGE_ICON,       // specifies that the file is an icon
    0,                // width of the image (we'll specify default later on)
    0,                // height of the image
    LR_LOADFROMFILE |  // we want to load a file (as opposed to a resource)
    LR_DEFAULTSIZE |   // default metrics based on the type (IMAGE_ICON, 32x32)
    LR_SHARED         // let the system release the handle when it's no longer used
);
SendMessage(button1, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
Zeus
  • 3,703
  • 3
  • 7
  • 20
  • Thanks Zhu Song. We had an ice storm here. So, the clean-up hasn't given me much chance to code. I will try to get to it, in a couple days. – anachronon Nov 06 '20 at 00:30