1

I'm building a GUI with some simple dialogs using raw Win32 API with pure C (no MCF). In one of those dialog I'd like to display a button with an icon (a small folder) instead of a text.

I prepared a .ico file with the proper size (16x16 pixel) and I proceeded as follow:

  • I've defined the icon resource in resource header file:

     #define ICON_FOLDER 901
    
  • I've put the icon, called folder.ico, in the same folder of the resource script and I've loaded icon resource in it:

    ICON_FOLDER ICON "folder.ico"
    
  • I've defined my button in the corresponding dialog resource specifying the BS_ICON style (MODEL_SEARCH is the resource ID defined in the resource header too):

    CONTROL "", MODEL_SEARCH, "button", BS_PUSHBUTTON | BS_ICON | WS_TABSTOP | WS_VISIBLE | WS_CHILD,   300, 8, 18, 18
    
  • In the GUI code, when the dialog containing the button is built, I've tried to load the icon at the beginning of the dialog procedure and then tried to set the icon in the case WM_INITDIALOG using respectively the following two loines of code:

    HICON folderico=LoadIcon(NULL,MAKEINTRESOURCE(ICON_FOLDER));
    

    and

    SendMessage(GetDlgItem(hwnd,MODEL_SEARCH),BM_SETIMAGE, (WPARAM)IMAGE_ICON,(LPARAM)folderico);
    

It doesn't work, the button is displayed but it doesn't show the icon.

I tried to do some changes and for example if I use those two last line of code to set one of default icons, like for example the IDI_APPLICATION one, the icon corresponding to IDI_APPLICATION resource is properly shown.

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
  • Does your icon's mask contain non-transparent pixels? Also, what happens if you copy the application icon over the *folder.ico* file? – IInspectable Mar 12 '15 at 13:33
  • Yes, my icon contains some non-trasparent pixels, but it should be not a problem, I used a similar icon as main window one and it's properly displayed... what do you mean for "copying the application icon", do you mean the executable? In this case nothing happens, anyway specifying the icon resource in the resource file it should be necessary the presence of the .ico file in the same directory of the resource script only at the resource file compiling time... – Filippo Micheletti Mar 12 '15 at 13:48
  • ps: I also tried to use another custom icon, the one that I use as th emain application icon (and which I know to be properly load and displayed), but nothing changes... – Filippo Micheletti Mar 12 '15 at 13:49
  • There's no obvious sign of you having performed any error checking. Of course, it doesn't help that you didn't show your code. Had you shown your code we would know and not need to guess. – David Heffernan Mar 12 '15 at 14:25

1 Answers1

2

You should be passing GetModuleHandle(NULL) into the first parameter of LoadIcon. You only want to use NULL for the first argument if you are loading standard Windows icons.

Trevor Balcom
  • 3,766
  • 2
  • 32
  • 51
  • 1
    Better would be to pass the actual module handle so that the code could run in a DLL if that were ever needed – David Heffernan Mar 12 '15 at 14:25
  • It works, thank you! Just for further information one can use the LoadImage function intead of the LoadIcon one, so that icon size can be trimmed at loading time. It's also possible to compress load and set in only one row, as follow: SendMessage(GetDlgItem(hwnd,MODEL_SEARCH),BM_SETIMAGE, (WPARAM)IMAGE_ICON,(LPARAM)LoadImage(GetModuleHandle(NULL),MAKEINTRESOURCE(ICON_FOLDER),IMAGE_ICON,14,14,0)); – Filippo Micheletti Mar 12 '15 at 14:27
  • @DavidHeffernan That is technically true. To the poster, the solution I provided works if you are making an EXE. In an EXE the hInstance passed in to WinMain() is the same as the one returned by GetModuleHandle(NULL). If this code was in a DLL you would want to keep track of the hInstance from DllMain() and supply it as the first argument to LoadIcon instead. – Trevor Balcom Mar 12 '15 at 14:29
  • Yes, I know how `GetModuleHandle` works. If using the MS compiler you can use `__ImageBase` http://stackoverflow.com/questions/557081/how-do-i-get-the-hmodule-for-the-currently-executing-code – David Heffernan Mar 12 '15 at 14:32
  • @FilippoMicheletti Please do take this as a sign that you need to check for errors. Had you done so you'd have realised that `LoadIcon` was failing. This is your opportunity to learn from experience that failing to check for errors causes pain. – David Heffernan Mar 12 '15 at 14:33