简体   繁体   中英

Setting button icon using C with raw Win32

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM