Click here to Skip to main content
15,881,424 members
Articles / Desktop Programming / MFC
Tip/Trick

CTreeObject - A Memory CTreeCtrl like Object

Rate me:
Please Sign up or sign in to vote.
4.90/5 (5 votes)
8 Oct 2017CPOL1 min read 14.6K   194   3   8
A memory CTreeCtrl like object, but resident in memory only

Introduction

Recently, I needed to use in an application a data structure that is very similar to CTreeCtrl. But, as CTreeCtrl is a CWnd object, I cannot use this one, because I didn't want to have a CWnd object. And because I didn't find something that fit my needs, I have built one myself. And I decided to share it here ...

Structure

This objects are derived from CObject, but this was not necessary (only because you might need to implement the serialization inside). There are implemented almost any methods that are needed to use this object like a CTreeCtrl object:

C++
typedef struct CTreeItem* HTREEOBJECTITEM;

class CTreeObject : virtual public CObject
{
.....
public:
 CTreeObject();
 virtual ~CTreeObject();
 HTREEOBJECTITEM InsertItem(CString sText, HTREEOBJECTITEM hParent = NULL);
 void SetItemData(HTREEOBJECTITEM hItem, DWORD dwItemData);
 DWORD GetItemData(HTREEOBJECTITEM hItem);
 void DeleteItem(HTREEOBJECTITEM hItem);
 void DeleteAllItems();

 POSITION GetRootHeadPosition() {return m_PtrItem.GetHeadPosition();}
 POSITION GetRootTailPosition() {return m_PtrItem.GetTailPosition();}
 HTREEOBJECTITEM GetNextSiblingItem(POSITION& pos, HTREEOBJECTITEM hParentItem = NULL);
 HTREEOBJECTITEM GetPrevSiblingItem(POSITION& pos, HTREEOBJECTITEM hParentItem = NULL);
 POSITION GetChildItem(HTREEOBJECTITEM hItem);
 HTREEOBJECTITEM GetParentItem(HTREEOBJECTITEM hItem);
 CString GetItemText(HTREEOBJECTITEM hItem) const;
 int GetChildCount(HTREEOBJECTITEM hItem) const;
 int GetRootCount() const {return m_PtrItem.GetCount();}
 BOOL IsValidItem(HTREEOBJECTITEM hItem) const;
};

Using the Code

Using this object is very simple, and similar to the model, CTreeCtrl:

C++
HTREEOBJECTITEM hRoot0 = m_Tree.InsertItem(_T("hRoot0"));

HTREEOBJECTITEM hhParent0 = m_Tree.InsertItem(_T("hParent0"), hRoot0);

m_Tree.InsertItem(_T("hItem0"), hhParent0);
m_Tree.InsertItem(_T("hItem1"), hhParent0);

And listing them:

C++
POSITION pos = m_Tree.GetRootHeadPosition();
while(NULL != pos)
{
 HTREEOBJECTITEM hRoot = m_Tree.GetNextSiblingItem(pos);
 POSITION posParent = m_Tree.GetChildItem(hRoot);
 while(NULL != posParent)
 {
  HTREEOBJECTITEM hParent = m_Tree.GetNextSiblingItem(posParent, hRoot);
  TRACE(_T("Item %s\n"), m_Tree.GetItemText(hParent));
 }
}

To check an item if it exists or is valid, I have IsValidItem(HTREEOBJECTITEM hItem) method.

I attach with this article a sample project that illustrates how to use this class ...

So, anywhere you need to have a tree data structure (XML, DICOM structure, data groups, etc.) you can freely use this object ... enjoy it !

Future Development

The next step in developing this class is to add serialization, and to templatize it.

History

  • 2017-10-08 - Initial release

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
SuggestionAlternative, more generic, implementation Pin
McChubby0078-Oct-17 10:18
McChubby0078-Oct-17 10:18 
GeneralRe: Alternative, more generic, implementation Pin
_Flaviu8-Oct-17 19:35
_Flaviu8-Oct-17 19:35 
GeneralRe: Alternative, more generic, implementation Pin
McChubby00710-Oct-17 6:08
McChubby00710-Oct-17 6:08 
GeneralRe: Alternative, more generic, implementation Pin
yafan10-Oct-17 4:05
yafan10-Oct-17 4:05 
GeneralRe: Alternative, more generic, implementation Pin
McChubby00710-Oct-17 5:56
McChubby00710-Oct-17 5:56 
Questionhave you consider to post this as a tip? Pin
Nelek8-Oct-17 8:58
protectorNelek8-Oct-17 8:58 
AnswerRe: have you consider to post this as a tip? Pin
_Flaviu8-Oct-17 19:40
_Flaviu8-Oct-17 19:40 
GeneralRe: have you consider to post this as a tip? Pin
Nelek9-Oct-17 8:48
protectorNelek9-Oct-17 8:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.