Hi ,


i am working with CTreeCtrl,
Here i need to add and remove IMAGE
on the tree item at runtime. ie add an image
only on the particular tree item . How can i do this.
i have tried something like this .

HTREEITEM itemSelected = mtreeCtrl.GetSelectedItem();
mtreeCtrl.SetImageList(&mImageList, LVSIL_NORMAL);

the problem here is Image gets added to all the items
in the tree view . i just want to know how to associate
an image to particular tree item.


Thanks in Advance.

Dani AI

Generated

Nice catch by — the tree control uses a single image list as a pool, and each node stores which index from that pool it should display. To change the picture for one node, set that node's image indexes (the per-item iImage / iSelectedImage) either when inserting the item or later by updating the item's TVITEM. Replacing or reassigning the control image list does not by itself change per-node indexes.

Example: create the node with explicit image indexes so only that node shows a given image.

// create a node with image index 1 (normal) and 2 (selected)
HTREEITEM h = mTreeCtrl.InsertItem(_T("Node text"), 1, 2, hParent, TVI_LAST);

To change an existing node's image at runtime, update the TVITEM mask and set iImage/iSelectedImage, then call the tree's SetItem:

TVITEM tvi = {0};
tvi.mask = TVIF_IMAGE | TVIF_SELECTEDIMAGE;
tvi.hItem = hItem;
tvi.iImage = newImageIndex;
tvi.iSelectedImage = newSelIndex;
mTreeCtrl.SetItem(&tvi);

Practical tips and pitfalls:

  • Keep the CImageList alive for the control lifetime (make it a member), otherwise the control will reference freed data.
  • If you want some nodes to appear without icons, add a transparent/blank entry to the image list and use that index for those nodes.
  • When using alpha icons, create the image list with ILC_COLOR32 and call SetBkColor(CLR_NONE) so transparency is preserved.
  • If you replace the whole image list at runtime, update item indexes afterwards (old indexes map to the new list differently).
  • State images (checkboxes) use the state-image mechanism and require correct indexing (use INDEXTOSTATEIMAGEMASK when setting TVITEM.state).

These notes build on 's approach and cover the common runtime and visual gotchas so per-item images behave as expected.

got it guyz its solved .......
mtreectrl.setitemimage();
:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.