|
After adding or creating a tree control, you may want to fill it with the necessary items. Each node of the control is an
HTREEITEM object. To create a new node, call the CTreeCtrl::InsertItem() method. It comes in various versions. One of them is:
HTREEITEM InsertItem(LPCTSTR lpszItem,
HTREEITEM hParent = TVI_ROOT,
HTREEITEM hInsertAfter = TVI_LAST );
The easiest way to add an item consists of calling the
InsertItem() method with a null-terminated string as argument because this is the only required argument of this version. Here is an example:
BOOL CTree1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP,
CRect(10, 10, 240, 280), this, 0x1221);
TreeSoft->InsertItem("Office Production");
return TRUE; // return TRUE unless you set the focus to a control
}
In this case, the item would appear as the root. You can add as many nodes like that and each would appear as a root:
BOOL CTree1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP,
CRect(10, 10, 240, 280), this, 0x1221);
TreeSoft->InsertItem("Office Production");
TreeSoft->InsertItem("Company Management");
TreeSoft->InsertItem("Software Development");
TreeSoft->InsertItem("Human Interaction");
return TRUE; // return TRUE unless you set the focus to a control
}
When calling this version of the InsertItem() method, if you do not pass the second argument, the node is created as root. This is because the root item has an
HTREEITEM value of TVI_ROOT, which is passed as default. You can also pass the second argument as NULL, which would produce the same effect.
The InsertItem() method returns an HTREEITEM value. You can use this value as a parent to a leaf item. This is done by passing it as the second argument when creating a leaf. Here is an example:
BOOL CTree1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP |
TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT |
TVS_SINGLEEXPAND | TVS_SHOWSELALWAYS |
TVS_TRACKSELECT,
CRect(10, 10, 200, 240), this, 0x1221);
HTREEITEM hTree, hCompany;
hTree = TreeSoft->InsertItem("Software Production", TVI_ROOT);
hCompany = TreeSoft->InsertItem("Microsoft", hTree);
TreeSoft->InsertItem("Office", hCompany);
TreeSoft->InsertItem("Visual Studio", hCompany);
TreeSoft->InsertItem("Servers", hCompany);
hCompany = TreeSoft->InsertItem("Jasc", hTree);
TreeSoft->InsertItem("Paint Shop Pro", hCompany);
TreeSoft->InsertItem("Animation Shop", hCompany);
hCompany = TreeSoft->InsertItem("Lotus", hTree);
TreeSoft->InsertItem("Notes", hCompany);
TreeSoft->InsertItem("Smart Office", hCompany);
hCompany = TreeSoft->InsertItem("Macromedia", hTree);
TreeSoft->InsertItem("Flash", hCompany);
return TRUE; // return TRUE unless you set the focus to a control
}
When using the InsertItem() method as we have done so far, the items are added in the order of their appearance. Besides creating new nodes, the
InsertItem() method also allows you to control the order in which to insert the new item. The new leaf can be added as the first or the last child of a node. To do this, pass a third argument to this version of the
InsertItem() method and give it the TVI_FIRST to be the first child or
TVI_LAST to be the last child of the current parent node. Here is
an example:
BOOL CTree1Dlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
TreeSoft->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_TABSTOP |
TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT |
TVS_SINGLEEXPAND | TVS_SHOWSELALWAYS |
TVS_TRACKSELECT,
CRect(10, 10, 200, 240), this, 0x1221);
HTREEITEM hTree, hCompany;
hTree = TreeSoft->InsertItem("Software Production", TVI_ROOT);
hCompany = TreeSoft->InsertItem("Microsoft", hTree);
TreeSoft->InsertItem("Office", hCompany);
TreeSoft->InsertItem("Graphics Manipulation", hCompany, TVI_LAST);
TreeSoft->InsertItem("Project Management", hCompany);
TreeSoft->InsertItem("Software Develoment", hCompany);
TreeSoft->InsertItem("Operating Systems", hCompany, TVI_FIRST);
TreeSoft->InsertItem("Software Documentation", hCompany);
hCompany = TreeSoft->InsertItem("Jasc", hTree);
TreeSoft->InsertItem("Paint Shop Pro", hCompany);
TreeSoft->InsertItem("Animation Shop", hCompany);
hCompany = TreeSoft->InsertItem("Lotus", hTree);
TreeSoft->InsertItem("Notes", hCompany);
TreeSoft->InsertItem("Smart Office", hCompany);
hCompany = TreeSoft->InsertItem("Macromedia", hTree);
TreeSoft->InsertItem("Flash", hCompany);
return TRUE; // return TRUE unless you set the focus to a control
}
|