|
// 根据lpszText查找所给定根的所有项
HTREEITEM CMyTreeCtrl::FindItem(HTREEITEM hRoot, LPSTR lpszText)
{
//如果hRoot = NULL,指定hRoot为Tree的根
if (hRoot == NULL)
hRoot = GetRootItem();
//如果hRoot = NULL,返回NULL
if (hRoot == NULL)
return NULL;
//判断hRoot是否符合条件,如果符合,返回hRoot
if (GetItemText(hRoot).Compare(lpszText) == 0)
return hRoot;
//如果hRoot没有子节点,返回NULL
if (ItemHasChildren(hRoot) == FALSE)
return NULL;
//递归查找hRoot的所有子节点
HTREEITEM hRes = NULL;
HTREEITEM hItem = GetChildItem(hRoot);
while (hItem)
{
hRes = FindItem(hItem, lpszText); //查以hItem为根的枝
if (hRes != NULL) //如果在以hItem为根的枝里找到,返回结果
return hRes;
else //否则,查找与hItem同级的下一个枝
hItem = GetNextSiblingItem(hItem);
} // end of while(hItem != NULL, has next item)
return NULL;
}
// 根据dwData查找所给定根的所有项
HTREEITEM CMyTreeCtrl::FindItem(HTREEITEM hRoot, DWORD dwData)
{
//如果hRoot = NULL,指定hRoot为Tree的根
if (hRoot == NULL)
hRoot = GetRootItem();
//如果hRoot = NULL,返回NULL
if (hRoot == NULL)
return NULL;
//判断hRoot是否符合条件,如果符合,返回hRoot
if (GetItemData(hRoot) == dwData)
return hRoot;
//如果hRoot没有子节点,返回NULL
if (ItemHasChildren(hRoot) == FALSE)
return NULL;
//递归查找hRoot的所有子节点
HTREEITEM hRes = NULL;
HTREEITEM hItem = GetChildItem(hRoot);
while (hItem)
{
hRes = FindItem(hItem, dwData); //查以hItem为根的枝
if (hRes != NULL) //如果在以hItem为根的枝里找到,返回结果
return hRes;
else //否则,查找与hItem同级的下一个枝
hItem = GetNextSiblingItem(hItem);
} // end of while(hItem != NULL, has next item)
return NULL;
}
|