Sometimes we often need to implement tree multi-state selection. This article introduces the specific implementation of a three-state selection tree.
Step 1: generate a dialog box project.
Step 2: Add a tree control and set the required properties.
Step 3: Create a three-state selection box icon and initialize the tree in oninitdialog () to associate the tree control with the image list control consisting of the Selection box icon. For details, see Source code .
Step 4: Implement left-click message of the tree to implement three-state selection.
Void cmutitreedemodlg: onclicktree1 (nmhdr * pnmhdr, lresult * presult) {uint uflags = 0; htreeitem hpitem; cpoint point = getcurrentmessage ()-> pt; screentoclient (& Point ); point. offset (-8,-16); htreeitem hitem = m_tripletree.hittest (point, & uflags); If (uflags & tvht_onitem) {hpitem = m_tripletree.getparentitem (hitem); int m, n, M1, N1; m_tripletree.getitemimage (hitem, m, n); If (M! = 1) {m_tripletree.setitemimage (hitem, 1,1); m_tripletree.getitemimage (hitem, M1, N1); travelchild (hitem, M1); If (hpitem! = NULL) travelsiblingandparent (hitem, M1);} else if (M = 1) {items (hitem,); m_tripletree.getitemimage (hitem, M1, N1); travelchild (hitem, m1); If (hpitem! = NULL) travelsiblingandparent (hitem, M1);} // MessageBox ("XXXX");} * presult = 0 ;}
Among them, the most difficult thing to achieve is tree traversal. I use recursion
Travelchild (htreeitem hitem, int M1 );
Travelsiblingandparent (htreeitem hitem, int M1.
CodeAs follows:
// Recursive void cmutitreedemodlg: travelchild (htreeitem hitem, int m) {htreeitem hchilditem, hbrotheritem; hchilditem = values (hitem); values (hchilditem, M, M ); if (hchilditem! = NULL) travelchild (hchilditem, m); hbrotheritem = m_tripletree.getnextsiblingitem (hchilditem); m_tripletree.setitemimage (hbrotheritem, M, m); If (hbrotheritem! = NULL) travelchild (hbrotheritem, m);} void cmutitreedemodlg: travelsiblingandparent (htreeitem hitem, int m) {int M1, N1; htreeitem identifier, identifier, hparentitem; M1 = m; hnextsiblingitem = m_tripletree.getnextsiblingitem (hitem); While (hnextsiblingitem! = NULL) {m_tripletree.getitemimage (hnextsiblingitem, M1, N1); If (M1! = M) break; else {hnextsiblingitem = m_tripletree.getnextsiblingitem (hnextsiblingitem); If (hnextsiblingitem! = NULL) m_tripletree.getitemimage (hnextsiblingitem, M1, N1) ;}} hprevsiblingitem = m_tripletree.getprevsiblingitem (hitem); While (hprevsiblingitem! = NULL) {m_tripletree.getitemimage (hprevsiblingitem, M1, N1); If (M1! = M) break; else {hprevsiblingitem = m_tripletree.getprevsiblingitem (hprevsiblingitem); If (hprevsiblingitem! = NULL) m_tripletree.getitemimage (hprevsiblingitem, M1, N1) ;}}if (M1 = m) {hparentitem = m_tripletree.getparentitem (hitem); If (hparentitem! = NULL) {m_tripletree.setitemimage (hparentitem, M1, M1); travelsiblingandparent (hparentitem, M1) ;}} if (M1! = M) {hparentitem = m_tripletree.getparentitem (hitem); While (hparentitem! = NULL) {m_tripletree.setitemimage (hparentitem,); hparentitem = m_tripletree.getparentitem (hparentitem );}}}