TreeControl子节点checkbox跟随父节点checkbox状态发生改变

方法一:
void CTreeTest_demoDlg::ChangeTreeItemAll(CTreeCtrl &tree, HTREEITEM hItem,BOOL bSelect) 
HTREEITEM x = tree.GetChildItem(hItem); 
if(NULL == x) return; 
do
tree.SetCheck( x, bSelect ); 
ChangeTreeItemAll(tree,x,bSelect);
}
while( ( x = tree.GetNextSiblingItem( x ) )!= NULL ); 
}

 void CTreeTest_demoDlg::OnNMClickTree(NMHDR *pNMHDR, LRESULT *pResult)
 {
  // TODO: 在此添加控件通知处理程序代码
 
  CPoint point; 
  UINT pflags; 
  GetCursorPos(&point); 
  m_ctrTree.ScreenToClient(&point); 
  HTREEITEM hitItem = m_ctrTree.HitTest( point, &pflags );
  if ( pflags & (TVHT_ONITEMSTATEICON )) 
 
 
  //if(m_bSelectDirect) // 选中父结点时他的直接孩子(儿子)结点同时被选中 
  //{ 
  // if ( m_ctrTree.GetCheck( hitItem ) )
  //
  // ChangeTreeItemDirect(m_ctrTree,hitItem,FALSE); 
  // }
  // else 
  //
  // ChangeTreeItemDirect(m_ctrTree,hitItem,TRUE); 
  // }
  //} 
//   if(m_bSelectAll) // 选中父结点时他的孩子结点以及所有后代(孙子)的节点被选中 
   
    if ( m_ctrTree.GetCheck( hitItem ) ) 
   
    ChangeTreeItemAll(m_ctrTree,hitItem,FALSE); 
   
    else 
    {
    ChangeTreeItemAll(m_ctrTree,hitItem,TRUE); 
   
    }
 
  *pResult = 0;
 }


方法二:
void CTreeTest_demoDlg::UpdateTreeCheck(const HTREEITEM hParent, const BOOL flag) 
if (m_ctrTree.ItemHasChildren(hParent)) 
HTREEITEM hNextItem; 
HTREEITEM hChildItem = m_ctrTree.GetChildItem(hParent); 
while (hChildItem != NULL) 
//递归获取当前节点下的子节点 
m_ctrTree.SetCheck(hChildItem,flag); 
UpdateTreeCheck(hChildItem,flag); 
hNextItem = m_ctrTree.GetNextItem(hChildItem, TVGN_NEXT);
hChildItem = hNextItem;
void CTreeTest_demoDlg::OnNMClickTree(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
DWORD dw = GetMessagePos();
CPoint pt(LOWORD(dw),HIWORD(dw)); //鼠标的屏幕坐标
CPoint ptClient; 
CRect rcPart;
m_ctrTree.GetWindowRect(rcPart);
ptClient.x = pt.x - rcPart.left;
ptClient.y = pt.y - rcPart.top; 
UINT uFlags;
HTREEITEM hItem = m_ctrTree.HitTest(ptClient, &uFlags); 
if ((hItem != NULL) && (uFlags & TVHT_ONITEMSTATEICON)) 
if (m_ctrTree.ItemHasChildren(hItem)) 
BOOL flag = m_ctrTree.GetCheck(hItem); //这时父节点还没有更新,需要取反 
UpdateTreeCheck(hItem,!flag); 
}
m_ctrTree.Select(hItem, TVGN_CARET);
*pResult = 0;
}

你可能感兴趣的:(TreeControl子节点checkbox跟随父节点checkbox状态发生改变)