Sign in to follow this  

VC++,how to expnding certain node in tree control

Recommended Posts

Original post by Mike nl
Uh, TVM_EXPAND? All you need is the handle to the item, which you may or may not have, but that depends on what exactly you're doing. You can always enumerate nodes with TVM_GETNEXTITEM.

Tank you,Mike. But I have a problem how do I know the value hItem?
I just know the path (in TreeControl) I want to expand (such as A/B/C).
I do not know how to get the hItem of "A/B/C"

Share this post

Link to post
Share on other sites
Like I said, use TVM_GETNEXTITEM to enumerate the children (flag = TVGN_CHILD) of a node.

This might work. I haven't tested or even tried to compile it, but this might give you an idea.

HTREEITEM GetTreeItemByPath(HWND hTree, HTREEITEM hNode, string path)
// Strip the first segment off the path
string::size_type pos = path.find_first_of("\\/");
const string name = path.substr(0, pos);
if (pos == string::npos) {
} else {
path.erase(0, pos + 1);

// Only the first 260 chars matter
TCHAR buffer[260];
TVITEM item;
item.mask = TVIF_TEXT;
item.pszText = buffer;
item.cchTextMax = 260;

// Enumerate the children of the node
item.hItem = TreeView_GetChild(hTree, hNode);
while (item.hItem != NULL)
// Get the item's text
TreeView_GetItem(hTree, &item);

// Compare it
if (name == buffer) {
// Match, find the rest of the path
HREEITEM hItem = GetTreeItemByPath(hTree, item.hItem, path);
// If duplicates are not possible, you can just return hItem immediately instead of continuing the search.
if (hItem != NULL) {
return hItem;

item.hItem = TreeView_GetNextSibling(hTree, item.hItem);
return NULL;

HTREEITEM GetTreeItemByPath(HWND hTree, const string& path)
return GetTreeItemByPath(hTree, TreeView_GetRoot(hTree), path)

[Edited by - Mike nl on August 20, 2008 5:09:19 AM]

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this