CTreeCtrl节点递归查找

//只有一个根目录的情况采用这种方式,如果有多个RootItem,需要把while提出来
HTREEITEM FindItem(HTREEITEM hRootItem, GUID &guid)
{
    HTREEITEM hItem = NULL;
    while (hRootItem)
    {
        FileListItem *pItem = (FileListItem*)GetItemData(hRootItem);
        if (pItem->FileInfo.guidFile == guid)
            return hRootItem;       

if ( ItemHasChildren(hRootItem) )
        {
            HTREEITEM   hChildItem = GetChildItem(hRootItem);
            if (hChildItem)
            {
                hItem = TreeVisit(hChildItem, guid); //递归遍历孩子节点     
                if (hItem)
                    break;
            }
        }
        hRootItem = GetNextItem(hRootItem, TVGN_NEXT);
    }
    return hItem;
}

HTREEITEM CTreeFind(CTreeCtrl& tree, HTREEITEM hRootItem, unsigned int ulNode)   
{       
    DWORD ulData;
    HTREEITEM hTreeRes;

    hTreeRes = NULL;
    ulData = tree.GetItemData(hRootItem);
    if (ulData == ulNode)
   {
        hTreeRes = hRootItem;
   }
   else if (tree.ItemHasChildren(hRootItem))       
   {   
        HTREEITEM hChildItem = tree.GetChildItem(hRootItem);       
       while(hChildItem!=NULL)       
      {   
            hTreeRes = CTreeFind(tree, hChildItem, ulNode);           
               if (hTreeRes != NULL)
              {
                  break;
              }
            hChildItem = tree.GetNextItem(hChildItem, TVGN_NEXT);       
        }
    }

    return hTreeRes;
}

HTREEITEM CTreeTestDlg::GetTreeItem(unsigned int ulNode)
{

    HTREEITEM pRes = NULL;
    HTREEITEM pRoot = NULL;

    pRoot = m_treeMain.GetRootItem();
    while (pRoot != NULL)
   {
        pRes = CTreeFind(m_treeMain, pRoot, ulNode);
        if (pRes != NULL)
       {
           break;
       }
       pRoot = m_treeMain.GetNextItem(pRoot, TVGN_NEXT);
    }

    return pRes;
}

你可能感兴趣的:(tree,null)