MFC中的CTreeCtrl是常用的控件,在项目中常常会遍历整个树节点并查找定位节点,以下是代码:
HTREEITEM TravTreeItem(HTREEITEM hTreeItem, A* p)
{
HTREEITEM hTreeChild = m_wndTreeCtrl.GetChildItem(hTreeItem);
while (hTreeChild != NULL)
{
DWORD_PTR dwItemData = 0;
if (hTreeChild != NULL) dwItemData = m_wndTreeCtrl.GetItemData(hTreeChild);
A* p1 = (A*)dwItemData;
if ((p1 != NULL) && (p1 == p))
{
return hTreeChild;
}
if (m_wndTreeCtrl.ItemHasChildren(hTreeChild))
{
HTREEITEM hItem = TravTreeItem(hTreeChild, p);
if (hItem != nullptr)
{
return hItem;
}
}
hTreeChild = m_wndTreeCtrl.GetNextSiblingItem(hTreeChild);
}
return NULL;
}
bool _IsPosition(A* p)
{
HTREEITEM hCurItem = m_wndTreeCtrl.GetRootItem();
HTREEITEM hRootItem = m_wndTreeCtrl.GetRootItem();
if (hRootItem == nullptr) return false;
while (hRootItem != NULL)
{
if (m_wndTreeCtrl.ItemHasChildren(hRootItem))
{
hCurItem = TravTreeItem(hRootItem, p);
}
if (hCurItem != nullptr)
{
break;
}
hRootItem = m_wndTreeCtrl.GetNextItem(hRootItem, TVGN_NEXT);
}
m_wndTreeCtrl.SelectItem(hCurItem);
}