Tree-shaped control partitioning:
Root node: All nodes that have been shrunk
Node: able to expand
Leaf: Cannot unfold
Note: Nodes and leaves are not absolute divisions, and when a new subordinate is added to a leaf, the leaf becomes a node
The tree controls are added:
node struct: htreeitem, declaring the node variable with the struct body
To add an item to a control:
htreeitem InsertItem (lpctstr lpszitem, //Item name int nImage, // Index of the image of an item in the tree view Control image list int nselectedimage, //tree view Control the index of the selected image of an item in the image list Htreeitem hparent = tvi_ ROOT, //Insert The handle of the item to be inserted before the parent's handle//new item );
Note: Before adding an item, set the root node before initializing the
Tree control Mouse click:
Determine the mouse position
Htreeitem Cmytreedlg::getselecttree () { CPoint pt; GetCursorPos (&pt); // get the current mouse position M_mytree.screentoclient (&PT); // Convert screen coordinates to client area coordinates Htreeitem Tree_item = M_mytree.hittest (PT); // call HitTest to find the tree node corresponding to the click return Tree_item;//return mouse position node}
Get an item of text
CString GetItemText (Htreeitem hItem) const;
Gets the specified relationship item
Htreeitem GetNextItem (Htreeitem hItem, UINT nCode) const;
nCode
The flag can be one of the following values: TVGN_CARET
retrieves the currently selected item . TVGN_CHILD
retrieves the first subproject parameter by the specified item hItem
. Retrieves the item that is the TVGN_DROPHILITE
target of a drag-and-drop operation . TVGN_FIRSTVISIBLE
retrieves the first visible item . TVGN_LASTVISIBLE
retrieves the last expanded item in the tree. This does not retrieve the last item visible in the tree view window . TVGN_NEXT
retrieves the next sibling . TVGN_NEXTVISIBLE
retrieves the next visible item that follows the specified item . TVGN_PARENT
retrieves the parent of the specified item . TVGN_PREVIOUS
retrieves the preceding sibling . TVGN_PREVIOUSVISIBLE
retrieves the first visible item that is located in the specified item. TVGN_ROOT
retrieves the first child item of a root item that is part of a specified item.
Gets the parent of the specified item
Htreeitem GetParentItem (Htreeitem hItem) const;
MFC Controls: Tree controls (CTreeCtrl)