Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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:

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 9-May-12 0:21am
Edited 9-May-12 0:28am
v2
Rate this: bad
good
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!
lv.mask = LVIF_PARAM | LVIF_IMAGE;
// To pass also text, use
lv.pszText = TEXT("1");
lv.mask = LVIF_PARAM | LVIF_IMAGE | LVIF_TEXT;
  Permalink  
Comments
JOHN 602 at 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 at 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 at 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 at 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 at 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 at 9-May-12 8:01am
   
Please pass this question over to Microsoft ;-)
I just cited from the MSDN ImageList_AddIcon description.
enhzflep at 9-May-12 8:10am
   
:D
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 at 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 at 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 at 9-May-12 9:02am
   
Thanx you very much. Your great experience saved me. Now all work good.
Rate this: bad
good
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.
  Permalink  
Rate this: bad
good
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);
  Permalink  
Comments
JOHN 602 at 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 at 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
0 Schatak 394
1 OriginalGriff 300
2 Abhijit Ghosh (Subho) 125
3 Andreas Gieriet 120
4 _Amy 115
0 OriginalGriff 7,137
1 Sergey Alexandrovich Kryukov 5,628
2 Maciej Los 3,529
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 9 May 2012
Copyright © CodeProject, 1999-2014
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