I want to create an HIMAGELIST for the list view. It actually needs to consist of file icons.
Here’s the code I have:
HIMAGELIST imageList = ImageList_Create(GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
ILC_MASK, 1, 1);
HICON ico = reinterpret_cast<HICON>(LoadImage(0,IDI_APPLICATION,
IMAGE_ICON,0,0,LR_SHARED));
ImageList_AddIcon(imageList, ico);
ListView_SetImageList(listView, imageList, LVSIL_SMALL);
The list view with three elements now has three application items.
However when I try to add another icon (IDI_HAND in this case), I still get the same 3 icons.
Another problem I have is that I can’t fetch the actual file icons I need:
SHFILEINFO sfi;
SHGetFileInfo (L"C:\\test.txt", NULL, &sfi, sizeof (sfi), SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
ImageList_AddIcon(imageList, sfi.hIcon);
This results in empty icons, not the txt icons I want.
I’ve been struggling with this for ever, and I greatly appreciate your help here.
UPDATE
I’m using sample code I found on the Internet to fill the list view (obviously that’s not what I want):
UINT columnMask = LVCF_TEXT|LVCF_FMT|LVCF_SUBITEM|LVCF_WIDTH;
LVCOLUMN lc[] = {
{ columnMask, 0, 150, L"Text...",0, 0,0,0 },
{ columnMask, LVCFMT_CENTER, 70, L"Number",0, 1,0,0 },
{ columnMask, 0, 100, L"Whatever",0, 2,0,0 },
};
ListView_InsertColumn(listView, 0, &lc[0]);
ListView_InsertColumn(listView, 1, &lc[1]);
ListView_InsertColumn(listView, 2, &lc[2]);
UINT itemMask = LVIF_TEXT;
LVITEM li[] = {
{ itemMask, 0,0, 0,0, L"...for the first item!",0, 0,0,0,0,0,0 },
{ itemMask, 0,1, 0,0, L"1",0, 0,0,0,0,0,0 },
{ itemMask, 0,2, 0,0, L"14 bucks",0, 0,0,0,0,0,0 },
{ itemMask, 1,0, 0,0, L"...for the second item!",0, 0,0,0,0,0,0 },
{ itemMask, 1,1, 0,0, L"24",0, 0,0,0,0,0,0 },
{ itemMask, 1,2, 0,0, L"2 suns",0, 0,0,0,0,0,0 },
{ itemMask, 2,0, 0,0, L"...for the second item!",0, 0,0,0,0,0,0 },
{ itemMask, 2,1, 0,0, L"24",0, 0,0,0,0,0,0 },
{ itemMask, 2,2, 0,0, L"2 suns",0, 0,0,0,0,0,0 },
{ itemMask, 3,0, 0,0, L"...for the second item!",0, 0,0,0,0,0,0 },
{ itemMask, 3,1, 0,0, L"24",0, 0,0,0,0,0,0 },
{ itemMask, 3,2, 0,0, L"2 suns",0, 0,0,0,0,0,0 },
};
// setting an icon like this doesn't work
li[0].iImage = sfi.iIcon;
ListView_InsertItem(listView, &li[0]);
ListView_SetItem(listView, &li[1]);
ListView_SetItem(listView, &li[2]);
ListView_InsertItem(listView, &li[3]);
ListView_SetItem(listView, &li[4]);
ListView_SetItem(listView, &li[5]);
ListView_InsertItem(listView, &li[6]);
ListView_SetItem(listView, &li[7]);
ListView_SetItem(listView, &li[8]);
The
iImagemember ofLVICONis the index of the icon in the imagelist you passed toListView_SetImageList. You are passing the index of the icon in the system imagelist, but the imagelist you passed toListView_SetImageListis your private imagelist. EitherListView_SetImageListto associate the imagelist with your private imagelist, and set theiImageto the index in your private imagelist (the return value fromImageList_AddIcon)ListView_SetImageListto associate the imagelist with the system imagelist, and set theiImageto the index in the system imagelist.