Click here to Skip to main content
13,089,400 members (62,851 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi guys
I have a problem with inserting icons in my listview.
When I push the button I'd like to insert some icons in the listview.
I have noticed when I run my code some place is captured(where a possible icon must be)?BUT icons aren't displayed.
Please help me with that !!!

Here is my simple code:

//Resource file:
#define IDI_ICON1       111
#define IDI_ICON2       112
#define IDI_ICON3       113
#define IDI_ICON4       114
#define IDI_ICON5       115
#define IDI_ICON6       116
//There is my listview:
    (HMENU) 500,
lvc.fmt  = LVCFMT_LEFT;
lvc.iSubItem = 0;       = 280;
lvc.pszText  = TEXT("Name");
ListView_InsertColumn(hFileListView, 0, &lvc);
//Data Modified
lvc.iSubItem = 1;       = 150;
lvc.pszText  = TEXT("Data Modified");
ListView_InsertColumn(hFileListView, 1, &lvc);
lvc.iSubItem = 2;       = 100;
lvc.pszText  = TEXT("Type");
ListView_InsertColumn(hFileListView, 2, &lvc);
lvc.iSubItem = 3;       = 100;
lvc.pszText  = TEXT("Size");
ListView_InsertColumn(hFileListView, 3, &lvc);
typedef struct tagAPPLINFO
    char szAppName[40];
    char szIconName[20];
APPLINFO rgApplInfo[]=
    {"Folder",            "book1.ico"},
    {"Exe",               "exec.ico"},
    {"Music",             "music.ico"},
    {"Text",		"text.ico"},
    {"Undefined",		"unkn.ico"},
    {"Picture",		"picture.ico"},
    {"Dll",		"Dll.ico"}  
VOID InsertInFileList(HWND hWnd,HWND hFileListView,LVITEM lv,HIMAGELIST himl,HINSTANCE hInst){
    HICON hIcon;
    GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);
    for(int i = IDI_ICON1; i <= IDI_ICON6; i++)
        hIcon = LoadIcon(hInst,MAKEINTRESOURCE(i));
        ImageList_AddIcon(himl, hIcon);
    ListView_SetImageList(hFileListView, himl, LVSIL_SMALL);
    for(int i=0; i<6; i++)
        lv.iSubItem = 0;
        lv.cchTextMax = 40;
        lv.lParam = (LPARAM)&rgApplInfo[1];
        lv.iImage = i;
        ListView_InsertItem(hFileListView, &lv);
        ListView_SetItemText(hFileListView, 0, 0, TEXT("1"));
Posted 9-May-12 0:21am
Updated 9-May-12 0:28am
Jochen Arndt194.8K
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The LVITEM mask member is not set when passing it to ListView_InsertItem:
lv.iItem=0; // this will insert at top!
lv.iSubItem = 0;
lv.cchTextMax = 40;
lv.lParam = (LPARAM)&rgApplInfo[1];
lv.iImage = i;
// This is missing!
// To pass also text, use
lv.pszText = TEXT("1");
JOHN 602 9-May-12 6:57am
I have added your code,but icons are still concealed. What I do wrong ???
Maybe the problem in icons path/
I placed icons in project folder.
Jochen Arndt 9-May-12 7:06am
At first you should check the return values of all functions: ImageList_AddIcon() and ListView_InsertItem() return -1 upon errors, ImageList_Create() and LoadIcon() return NULL upon errros. If you are using a debug build, use the ASSERT() and VERIFY() macros (e.g. ASSERT(hIcon != NULL) and VERIFY(ImageList_AddIcon(himl, hIcon) >= 0)).
enhzflep 9-May-12 7:41am
Not a solution to your problem as such, but a couple of things come to mind.
1) You should DeleteObject hIcon after you've added it to the image list
2) Doesn't your imagelist_create specify an image list of size 1, that may not grow larger than 2?
3) Is it your intention to always use a pointer to rgApplInfo[1]? i.e {"Exe", "exec.ico"} as the lParam for all of your listView items?
Jochen Arndt 9-May-12 7:48am
Your first point is not necessary: The system automatically frees icons loaded with LoadIcon() when no longer used. But the others should be noted.
enhzflep 9-May-12 7:53am
When you say, "when no longer used" is that actually a euphemism for 'when the program exits?'
In much the same way that you don't have to free any memory allocated with malloc or delete, since the system frees them both when no longer needed?
(i.e when the program exits)
Jochen Arndt 9-May-12 8:01am
Please pass this question over to Microsoft ;-)
I just cited from the MSDN ImageList_AddIcon description.
enhzflep 9-May-12 8:10am
Mmm mm. Nothing like (msdn) documentation that fails to clarify that which it documents....
Interesting that there is a distinction made between icons brought into memory via LoadIcon & CreateIcon (with the help saying you should delete an icon after adding to an imagelist if it's a result of CreateIcon, though saying that one that's been loaded will be deleted by the system)
Guess I'll just stick to DeleteObject right after so I don't have to remember about this inconsistency.

Many thanks for the insight.
Simon. :)
JOHN 602 9-May-12 7:19am
You were right about return value of LoadIcon(). It returns NULL.
I'm not an expert in Win32 and I'm sorry that I spent your time.
But where is the mistake. Icons are in project folder.Maybe smth wrong with path/
Jochen Arndt 9-May-12 7:28am
You must import the icons (add them to your executable). With Visual Studio, open the resource tab, click right, select 'Add resource', select 'Icon' and choose 'Import' (may depend on the VS version). However, the mask member var must be also set to specify the valid LVITEM members.
JOHN 602 9-May-12 9:02am
Thanx you very much. Your great experience saved me. Now all work good.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

I am not sure how much difference it will make but it is recommended to use LoadImage()[^] rather than LoadIcon(). You should also check all return values from system calls to see if any have not succeeded.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

You might want to expand the size of your imagelist to 6 or 10 to start with
GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_COLOR32, 6, 1);

And DestroyIcon

for(int i = IDI_ICON1; i <= IDI_ICON6; i++)
        hIcon = LoadIcon(hInst,MAKEINTRESOURCE(i));
        ImageList_AddIcon(himl, hIcon);
        DestroyIcon( hIcon );

Does you icons support multiple sizes? 16x16, 32x32, 256x256, in the same icon file?
ListView_SetView( hFileListView, LV_VIEW_DETAILS );

Make sure you set item
ListView_SetItem( hFileListView, &lv);
ListView_SetItem( hFileListView, (const LV_ITEM *)&lv);
JOHN 602 13-May-12 5:42am
jkirkerx all is OK. I've improved my code.But the loaded icons looks somehow wrong(washed pictures). Seems like I've done all steps correctly(load icons in Resource-file:Add->Source->Import->then I selected all the icons and inserted them). I can't get it where is the mistake.Have any ideas ?
jkirkerx 13-May-12 18:38pm
I had that problem at first. I had to go back and make new icons, so a single icon file contained multiple values such as 16x16, 24x24, and so forth, including 16, 24 and 32 colors.

I loaded some system icons like you did, and the icons are multiple resolution. I know for detail view, it's 16x16, and for icon view, I think it's 24x24 or larger.

Here you made a small 16x16 icon list, and choose ILC_MASK, I think it was ILC_COLOR24 or ILC_COLOR32 or something for more vibrant icons. The Mask I think was for overlaying a icon over an icon, like the shortcut arrow over the main icon.

GetSystemMetrics(SM_CXSMICON), GetSystemMetrics(SM_CYSMICON), ILC_MASK, 1, 1);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web01 | 2.8.170813.1 | Last Updated 9 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100