//本树只有一个父节点,最深子节点为2级,拥有复选框
//遍历树控件(只有一个父节点,命名为“所有模板”)
void CCheckOverLapDlg::TraverseTree(CTreeCtrl* pTreeCtrl)
{
CString strParent = _T("所有模板");
//主函数入口
TraverseTreeBranch(pTreeCtrl, pTreeCtrl->GetRootItem(), TVI_ROOT, strParent, 1);
}
void CCheckOverLapDlg::TraverseTreeBranch(CTreeCtrl* pTreeCtrl, HTREEITEM hItem, HTREEITEM hParentItem, CString strParent, int iGrade)
{
// Do stuff with hItem here
//判断该节点是否处于选中状态
BOOL bSelected = m_wndTreeCtrl.GetCheck(hItem);
//获取当前节点的文本信息
CString strCurrent = m_wndTreeCtrl.GetItemText(hItem);
//获取其子节点
HTREEITEM hItemTmp = pTreeCtrl->GetChildItem(hItem);
//如果没有子节点了(即最深层节点),并且有文本信息,且处于选中状态
if (hItemTmp == NULL && strCurrent != (_T("")) && strParent != _T("所有模板") && bSelected)
{
// Do stuff with hItem here,此处根据你的需求来修改
//获取当前节点与父节点的文本信息,存起来,
m_mapSelectedItem.insert(pair<CString, CString>(strParent, strCurrent));
//从该树控制中删除此节点
m_wndTreeCtrl.DeleteItem(hItem);
}
while (hItemTmp != NULL)
{
//一级子节点
if (iGrade == 1)
TraverseTreeBranch(pTreeCtrl, hItemTmp, TVI_ROOT, strCurrent, 2);
//二级子节点
else if (iGrade == 2)
TraverseTreeBranch(pTreeCtrl, hItemTmp, TVI_ROOT, strCurrent, 3);
//获取当前节点的子节点
hItemTmp = m_wndTreeCtrl.GetNextSiblingItem(hItemTmp);
}
}
标签:控件,pTreeCtrl,遍历,strCurrent,strParent,MFC,hItemTmp,hItem,节点
From: https://www.cnblogs.com/lmxwiki/p/18340306