Introduction

Don't you find you have written the same segment of
code many times to parse a subtree because
the action you wanted to launch on the items was different. In order to avoid such
code, I've written CTreeViewIterator class. This class contains a method
called ApplyFunction to launch a function on each item/node of a subtree.

Implementation

The class I've written is dedicated to Explorer like applications (those with
a CTreeView class on the left side). In that section, we're going
to see how to use
CTreeViewIterator in that general
case. In the section Adaptation, we'll see how to modify it to use it in other
cases.

When you generate your application using the wizard, Visual C++ doesn't give you
the capability to choose the CTreeView class implementation. Therefore, you
have to have a CLeftView in your application. In the LeftView.cpp file, add the
following line in the include section.

#include "TreeViewIterator.h"

You're now able to use the CTreeViewIterator class in that module.

You must define the function that will receive the item/node as parameter. This
function will be external to your CLeftView class even if defined in the same
module. If you don't define it in this module, do not forget to create a definition
to set it visible from this module. If for any reason,
you do want to set it as a method of your CLeftView class, please read the Adaptation section. The signature of this function is predefined. It
must be like this:

The goal here, is to store the subtree as a list in a string. So,
the only thing the external function has to do is getting the name and
concatenating it to the full list. To do so, it will use an attribute created
in the CLeftView called sFullList (type CString). Since we got a handle on the
CLeftView as first parameter, there's no
problem till the attribute is public (if not, we should have use public methods
like get & set). Using this method, you may update the
tree item/node or the CLeftView the way you want.

Now you have the function to apply to each item/node, you have to call
it. To do so, you'll have to implement first the iterator and then, to call
the ApplyFunction method with the right parameters.

The method OnSelChanged is launched when the user has select a
new item/node in the treeview. Here, we have catched it to add our function call. As you can see, we first add the iterator implementation with:

CTreeViewIterator *ptrTree = (CTreeViewIterator *) tTree;

In fact, we may consider the iterator on the CTreeCtrl as a shell
with a special method. Therefore, within ApplyFunction method, we will see the content of the CTreeCtrl to parse.

We now have to get the parameters:

The first one, the handle on the CLeftView is easy to get, it's this
variable.

The second is the handle on the item/node. Since we
are in the method that catch the new selection, we gonna extract the item
address within the parameter pNMHDR using two lines:

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Share

About the Author

After years passed in services companies, I now work for Euromaster, an international group dedicated to vehicules maintenance. I also develop for my own usage. I both use Java and C++ languages and now, I integrate XML documents and XSL syntax in my applications even if SQL Databases like Oracle remain my main data sources

Do you mean you want to know the level of the selected item?
For example,

Item1
Item11
Item12
Item121
Item13
Item131
Item132
Item2
Item21

If Item131 is selected, level should be 3.

To do so, use the function GetParentItem. This function give you a handle on the parent of the item you pass as parameter. If the returned handle is null, it means that you reached the root. So, using a while syntax, you may count the times you may climb up in the tree from your current position (the selected item) and then, the counting result will be your level.

When you create your item using the InsertItem method of your CTreeCtrl, you get a HTREEITEM variable returned by the method. It points to the item you have just created. On this item, 2 datas may be updated. The first one is the text displayed in the tree. To update it, use the SetItemText method. The second one is the Data. It's a long value you don't see on the screen but it's stored in the HTREEITEM. To update it, use the SetItemData method. The get methods exist for both text and data. So, those are the steps you are to do:

- Create the item using InsertItem
- Set text if mandatory (you first set the text in the InsertItem method) using SetItemText
- Set data using SetItemData (that's the way to store an ID)
- When the user selects a tree item, the OnSelchanged event is thrown. Then, get the selected item ((NM_TREEVIEW *) pNMHDR->itemNew.hItem)
- With the selected item, you may get the ID using GetItemData since you stored it during item creation phase.