-
Treeview - Win32 API
I have a treeview control with an imagelist added but the images only appear next to the items when they're selected and then disappear when deselected. I need the icons to be there all of the time.
Code:
TVITEM tvi;
TVINSERTSTRUCTURE tvs;
HTREEITEM hti;
HBITMAP hbmp;
HIMAGELIST himg;
int checknone, checksub, checked;
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, WC_TREEVIEW, "", WS_CHILD|TVS_HASLINES|TVS_SHOWSELALWAYS, CW_USEDEFAULT, CW_USEDEFAULT,
500, 300, phWnd, NULL, gInstance, NULL);
himg = ImageList_Create(10, 10, FALSE, 3, 0);
hbmp = LoadBitmap(gInstance, MAKEINTRESOURCE(NV_BITMAP_LB));
checknone = ImageList_Add(himg, hbmp, NULL);
DeleteObject(hbmp);
hbmp = LoadBitmap(gInstance, MAKEINTRESOURCE(NV_BITMAP_LB2));
checksub = ImageList_Add(himg, hbmp, NULL);
DeleteObject(hbmp);
hbmp = LoadBitmap(gInstance, MAKEINTRESOURCE(NV_BITMAP_LB3));
checked = ImageList_Add(himg, hbmp, NULL);
DeleteObject(hbmp);
TreeView_SetImageList(hwnd, himg, TVSIL_NORMAL);
tvi.mask = TVIF_TEXT|TVIF_IMAGE;
tvi.pszText = "Test";
tvi.cchTextMax = 5;
tvs.item = tvi;
tvs.hParent = TVI_ROOT;
tvi.iImage = checknone;
tvi.iSelectedImage = checknone;
hti = (HTREEITEM)SendMessage(hwnd, TVM_INSERTITEM, 0, (LPARAM)&tvs);
tvs.hParent = hti;
SendMessage(hwnd, TVM_INSERTITEM, 0, (LPARAM)&tvs);
SendMessage(hwnd, TVM_INSERTITEM, 0, (LPARAM)&tvs);
-
I use similar code without problems.
Try
In the CreateWindow you have used the child style but not set an ID for the control. (HMENU param is cast to an int ID if the WS_CHILD style is used)
Try some error checking
// Fail if not all of the images were added.
if (ImageList_GetImageCount(himg)<NUM_BMPS)
return FALSE;
I also use
tvi.hInsertAfter=TVI_LAST;