树形控件点击分组全选,取消一个,分组选择变暗 1.添加树形控件 给树形控件添加成员变量CTreeCtrl m_TripleTree; 2.在头文件中添加变量 HTREEITEM m_hTreeItem; 3.再头文件中定义 CImageList m_imList; 4.在初始化中添加 BOOL CTreeDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here HICON hIcon[3]; //图标 m_imList.Create(16,16,0,3,3); // hIcon[0]=AfxGetApp()->LoadIcon(IDI_ICON1); //加载图标 hIcon[1]=AfxGetApp()->LoadIcon(IDI_ICON2); hIcon[2]=AfxGetApp()->LoadIcon(IDI_ICON3); for(int n=0;n<3;n++) m_imList.Add(hIcon[n]); m_TripleTree.SetImageList(&m_imList,TVSIL_NORMAL); TV_INSERTSTRUCT tvinsert; tvinsert.hParent=NULL; tvinsert.hInsertAfter=TVI_LAST; tvinsert.item.mask=TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT; tvinsert.item.hItem=NULL; tvinsert.item.state=0; tvinsert.item.stateMask=0; tvinsert.item.cchTextMax=6; tvinsert.item.iSelectedImage=0; tvinsert.item.cChildren=0; tvinsert.item.lParam=0; tvinsert.item.pszText="未分组"; // 树根 tvinsert.item.iImage=0; HTREEITEM hCObject=m_TripleTree.InsertItem(&tvinsert); //second level tvinsert.hParent=hCObject;//hCObject tvinsert.item.iImage=0; tvinsert.item.pszText="张三"; m_TripleTree.InsertItem(&tvinsert); //HTREEITEM hCCmdTarget=m_TripleTree.InsertItem(&tvinsert); tvinsert.hParent=hCObject;//hCObject tvinsert.item.iImage=0; tvinsert.item.pszText="张三"; m_TripleTree.InsertItem(&tvinsert); tvinsert.hParent=hCObject;//hCObject tvinsert.item.iImage=0; tvinsert.item.pszText="张三"; m_TripleTree.InsertItem(&tvinsert); tvinsert.hParent=hCObject;//hCObject tvinsert.item.pszText="朋友"; tvinsert.item.iImage=0; m_TripleTree.InsertItem(&tvinsert); tvinsert.hParent=hCObject;//hCObject tvinsert.item.iImage=0; tvinsert.item.pszText="好友"; m_TripleTree.InsertItem(&tvinsert); tvinsert.item.pszText="同学"; tvinsert.item.iImage=0; m_TripleTree.InsertItem(&tvinsert); ///////////////////////////////////////////////////////////////////////////////////////// tvinsert.hParent=NULL; tvinsert.item.pszText="未分组2"; // 树根 tvinsert.item.iImage=0; HTREEITEM hCObject2=m_TripleTree.InsertItem(&tvinsert); //second level tvinsert.hParent=hCObject2;//hCObject tvinsert.item.iImage=0; tvinsert.item.pszText="张三"; m_TripleTree.InsertItem(&tvinsert); tvinsert.hParent=hCObject2;//hCObject tvinsert.item.iImage=0; tvinsert.item.pszText="王五"; m_TripleTree.InsertItem(&tvinsert); /////////////////////////// 5.导入相应ICO图片(3个) 6.给树形控件添加单击事件,并添加如下代码: 如: void CTreeDlg::OnClickTree1(NMHDR* pNMHDR, LRESULT* pResult) { // TODO: Add your control notification handler code here 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; } 7.添加TravelChild(HTREEITEM hItem, int m)成员函数 如: void CTreeDlg::TravelChild(HTREEITEM hItem, int m) { HTREEITEM hChildItem,hBrotherItem; hChildItem=m_TripleTree.GetChildItem(hItem); while(hChildItem!=NULL) { m_TripleTree.SetItemImage(hChildItem,m,m); hChildItem=m_TripleTree.GetNextSiblingItem(hChildItem); } } 8.添加TravelSiblingAndParent(HTREEITEM hItem, int m)成员函数 如: void CTreeDlg::TravelSiblingAndParent(HTREEITEM hItem, int m) { int m1,n1; int mark=0; HTREEITEM hNextSiblingItem,hPrevSiblingItem,hParentItem; m1=m; hNextSiblingItem=m_TripleTree.GetNextSiblingItem(hItem);//第一个 while(hNextSiblingItem!=NULL) { m_TripleTree.GetItemImage(hNextSiblingItem,m1,n1); if(m1!=m) { mark=1; 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) { mark=1; break; } else { hPrevSiblingItem=m_TripleTree.GetPrevSiblingItem(hPrevSiblingItem); if(hPrevSiblingItem!=NULL) m_TripleTree.GetItemImage(hPrevSiblingItem,m1,n1); } } if(m1!=m ) //有一个未被选中,j吧父节点去了 { hParentItem=m_TripleTree.GetParentItem(hItem); while(hParentItem!=NULL) { m_TripleTree.SetItemImage(hParentItem,2,2);// hParentItem=m_TripleTree.GetParentItem(hParentItem); } } if(m1==m&& mark==0)//权被选中 { hParentItem=m_TripleTree.GetParentItem(hItem); while(hParentItem!=NULL) { m_TripleTree.SetItemImage(hParentItem,m1,m1); hParentItem=m_TripleTree.GetParentItem(hParentItem); } } } 运行: