Reputation: 57267
I'm (still) trying to figure out how to change state images of a treeview from the default +/- to an image of my choice. I'm making progress but I've hit a wall.
I'm just trying to change the image when a branch is expanded. When I use the below code, sometimes it changes and sometimes not. It only acts as expected when I bounce back and forth a few times, not the first time.
I assume I'm missing a flag or haven't set something correctly? Any tips?
case WM_NOTIFY:
if (wParam == IDC_TVWFILELIST) {
LPNMTREEVIEW nmtv = (LPNMTREEVIEW) lParam;
if (nmtv->hdr.code == TVN_ITEMEXPANDED) {
TVITEM item = nmtv->itemNew;
item.state = INDEXTOSTATEIMAGEMASK(4);
TreeView_SetItem(pnl_tree.GetTreeview(), &item);
}
}
break;
Upvotes: 1
Views: 2004
Reputation: 57267
OK, of course when I post on SO I figure it out myself soon after! Darn. Guess it helps to talk it through. Anyway, for the next guy.
I've had this same problem at other places learning the winapi, especially with treeviews - if I neglect setting a certain member of a struct, I get strange behavior.
In this case, turns out that item.stateMask = TVIS_STATEIMAGEMASK;
needed to be re-set.
Apparently it doesn't persist across TreeView_SetItem() (which very concisely states "The TreeView_SetItem macro sets some or all of a tree-view item's attributes").
Working code, for me, with a bonus of expand/collapse handler (now I'm cooking with gas):
if (wParam == IDC_TVWFILELIST) {
LPNMTREEVIEW nmtv = (LPNMTREEVIEW) lParam;
if (nmtv->hdr.code == TVN_ITEMEXPANDED) {
TVITEM item = nmtv->itemNew;
item.stateMask = TVIS_STATEIMAGEMASK;
if (nmtv->action == TVE_COLLAPSE) {
item.state = INDEXTOSTATEIMAGEMASK(4);
}
else if (nmtv->action == TVE_EXPAND) {
item.state = INDEXTOSTATEIMAGEMASK(5);
}
TreeView_SetItem(pnl_tree.GetTreeview(), &item);
}
}
Some other links for future treeview image state newcomers, that don't apply to this problem specifically, but helped me slowly figure this thing out:
Upvotes: 2