MFC 实现CTreeCtrl单选
void CDepartmenManager::SetUncheck(HTREEITEM hTree) { if (!hTree){ return; } m_DePartmentView.SetCheck(hTree, 0); HTREEITEM hChild = m_DePartmentView.GetChildItem(hTree); if (hChild){ SetUncheck(hChild); } else{ return; } HTREEITEM hBrothor = m_DePartmentView.GetNextSiblingItem(hChild); while (hBrothor){ if (hBrothor){ SetUncheck(hBrothor); } else{ return; } hBrothor = m_DePartmentView.GetNextSiblingItem(hBrothor); } } void CDepartmenManager::OnNMClickTree1(NMHDR *pNMHDR, LRESULT *pResult) { CPoint point; GetCursorPos(&point);//获得鼠标点击的位置 m_DePartmentView.ScreenToClient(&point);//转化为客户坐标 SetUncheck(hRoot); UINT uFlags; CurrentItem = m_DePartmentView.HitTest(point, &uFlags);//获得当前点击节点的ITEM if (uFlags & TVHT_ONITEM){ BOOL bState = m_DePartmentView.GetCheck(CurrentItem); m_DePartmentView.SetCheck(CurrentItem, bState); } *pResult = 0; }
CTreeCtrl m_DePartmentView;
hRoot为根节点,
CurrentItem是临时节点