Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 21: Tree and List Controls Visual C++ and MFC Fundamentals


TVS_HASLINES | TVS_HASBUTTONS | TVS_LINESATROOT |
TVS_SINGLEEXPAND | TVS_SHOWSELALWAYS |
TVS_TRACKSELECT,
CRect(10, 10, 200, 100), 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 CControlsDlg::OnInitDialog()
{
CDialog::OnInitDialog();

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, 100), this, 0x1221);

TreeSoft->InsertItem("Office Production");//, TVI_ROOT);
TreeSoft->InsertItem("Company Management");
TreeSoft->InsertItem("Software Development");
TreeSoft->InsertItem("Human Interaction");

return TRUE; // return TRUE unless you set the focus to a control
}

Figure 60: A Tree List With All Items As Roots...................................................................


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 CControlsDlg::OnInitDialog()
{
CDialog::OnInitDialog();
Free download pdf