Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
//-----------------------------------
//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:

hFileListView=CreateWindow(WC_LISTVIEW,
    NULL,
    WS_CHILD | WS_VISIBLE | LVS_REPORT|WS_HSCROLL|WS_BORDER|WS_VSCROLL |
    ES_AUTOHSCROLL | ES_AUTOVSCROLL,
    150,
    70,
    630,
    220,
    hWnd,
    (HMENU) 500,
    hInst,
    NULL);
lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH  | LVCF_FMT;
lvc.fmt  = LVCFMT_LEFT;
//Name
lvc.iSubItem = 0;
lvc.cx       = 280;
lvc.pszText  = TEXT("Name");
ListView_InsertColumn(hFileListView, 0, &lvc);
//Data Modified
lvc.iSubItem = 1;
lvc.cx       = 150;
lvc.pszText  = TEXT("Data Modified");
ListView_InsertColumn(hFileListView, 1, &lvc);
//Type
lvc.iSubItem = 2;
lvc.cx       = 100;
lvc.pszText  = TEXT("Type");
ListView_InsertColumn(hFileListView, 2, &lvc);
//Size
lvc.iSubItem = 3;
lvc.cx       = 100;
lvc.pszText  = TEXT("Size");
ListView_InsertColumn(hFileListView, 3, &lvc);

//----------------------------------------------------------------
typedef struct tagAPPLINFO
{
    char szAppName[40];
    char szIconName[20];
}APPLINFO;

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;
    himl=ImageList_Create(
    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.iItem=0;
        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
Updated 9-May-12 0:28am
v2

The LVITEM mask member is not set when passing it to ListView_InsertItem:
C++
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!
lv.mask = LVIF_PARAM | LVIF_IMAGE;
// To pass also text, use
lv.pszText = TEXT("1");
lv.mask = LVIF_PARAM | LVIF_IMAGE | LVIF_TEXT;
 
Share this answer
 
Comments
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)
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.
 
Share this answer
 
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);
 
Share this answer
 
Comments
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900