Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


Always property to True or create the control with the TVS_SHOWSELALWAYS
style.

Besides selecting an item, when a node has children, to expand it, the user can double-
click the node or click its button if available. Simply selecting the node does not expand
it. If you want the selected node to automatically expand without the user having to
double-click or click its button, set its Single Expand property to True or create it with
the TVS_SINGLEEXPAND style.

When the items of a tree control display or when the nodes expand, they may span
beyond the allocated rectangle of the control. When this happens, a vertical and/or a
horizontal scroll bars may automatically display. This is because, by default, the Scroll
property is set to True. If you do not want any scroll bar, set the Scroll property to False
or create the control with the TVS_NOSCROLL style.

Another operation the user can perform on a node consists of changing its text. If you
want to allow this, set the control’s Edit Labels to True or add the TVS_EDITLABELS
style to it.

A user can be allowed to add items to the list by drag-n-drop operations. If you want to
prevent this, set the Disable Drag Drop property to True or create the control with the
TVS_DISABLEDRAGDROP style.

Practical Learning: Configuring a Tree Control



  1. On the dialog box, click the tree control to select it. On the Properties window set the
    Has Buttons, the Has Lines and the Lines At Root properties to True

  2. Also set the Client Edge and the Modal Frame properties to True

  3. Add a Control Variable to the tree object and name it m_CarTree

  4. Save All


21.1.3..Tree Controls Methods.......................................................................


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. 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 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 |
Free download pdf