How Do I Find Indent or Depth of an HTREEITEM?

Larry 60 Reputation points
2026-08-03T02:07:22.99+00:00

Hello Folks and AI assistants:

I send TVM_GETINDENT to learn how much each tree item is indented relative to its parent.

I want to know how much a particular treeview item is indented.

For this I need to know the depth a particular HTREEITEM is in the tree.

Is there a way to derive an item's depth from the HTREEITEM , or is this a value the application needs to keep as it traverses the tree?

Thanks
Larry

Developer technologies | C++
Developer technologies | C++

A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.

0 comments No comments

Answer accepted by question author
RLWA32 52,786 Reputation points
2026-08-03T07:10:23.3066667+00:00

@Larry

Although the Tree-View control does not natively track the indentation of an item you can store this information by using the lParam member of the item's TVITEM or TVITEMEX structure when you insert the item.

Consequently, retrieving the indentation of an item would not require traversing the tree each time the information is needed. Of course, it would be your responsibility to update this static information if an item was relocated.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

Answer accepted by question author
Taki Ly (WICLOUD CORPORATION) 3,700 Reputation points Microsoft External Staff Moderator
2026-08-03T03:40:42.2066667+00:00

Hello @Larry ,

It appears that the Tree-View control does not intrinsically store or expose the current depth of a specific HTREEITEM. Because the control tracks items via relationship links rather than absolute depth, this suggests that the application will need to calculate this value dynamically or track it during traversal.

It might be beneficial to calculate the depth on-demand by walking up the tree structure. You can achieve this by repeatedly calling the TreeView_GetParent macro (which sends the TVM_GETNEXTITEM message with the TVGN_PARENT flag) and counting the steps until it returns NULL.

Below is a quick example of a function that could help:

int GetItemDepth(HWND hwndTV, HTREEITEM hItem) {
    int depth = 0;
    HTREEITEM hParent = hItem;

    // Walk up the parent links until we reach the root
    while ((hParent = TreeView_GetParent(hwndTV, hParent)) != NULL) {
        depth++;
    }
    return depth;
}

Once you find the depth, multiplying it by the result of TVM_GETINDENT should give you the total relative indentation.

I hope this information is helpful. Please let me know if exploring this approach works well for your scenario. If you found my response helpful or informative, I would greatly appreciate it if you could follow this guide for your confirmation.

Thank you.

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.