分类: C/C++
2008-08-01 16:54:45
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) { m_TripleTree.SetItemImage(hItem,0,0); m_TripleTree.GetItemImage(hItem,m1,n1); TravelChild(hItem, m1); if(hPItem!=NULL) TravelSiblingAndParent(hItem,m1); } // MessageBox("xxxx"); } *pResult = 0; }这其中,最难实现的是树的遍历,我用递归
//递归 void CMutiTreeDemoDlg::TravelChild(HTREEITEM hItem,int m) { HTREEITEM hChildItem,hBrotherItem; hChildItem=m_TripleTree.GetChildItem(hItem); m_TripleTree.SetItemImage(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 hNextSiblingItem,hPrevSiblingItem,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,2,2); hParentItem=m_TripleTree.GetParentItem(hParentItem); } } }具体细节请阅读本文提供的源代码。 下载本文示例代码