4 个解决方案
#1
HTREEITEM root;
HTREEITEM child;
HTREEITEM childChild;
int tempId = 0;
root = m_TreeCtrl.GetRootItem();
//第一层:层
while(root != NULL)
{
child = m_TreeCtrl.GetChildItem(root);
//第二层:屏幕
while (child != NULL)
{
childChild = m_TreeCtrl.GetChildItem(child);
//第二层:信号层
while (childChild != NULL)
{
tempId = m_TreeCtrl.GetItemData(childChild);
if(tempId == id)
{
m_TreeCtrl.SelectItem(childChild);
m_TreeCtrl.Expand(childChild, TVM_EXPAND);
return;
}
childChild = m_TreeCtrl.GetNextItem(childChild, TVGN_NEXT);
}
child = m_TreeCtrl.GetNextItem(child, TVGN_NEXT);
}
root = m_TreeCtrl.GetNextItem(root, TVGN_NEXT);
}
循环遍历树形控件的子项,但上面的例子是取子项的GetItemData,你可以获取子项的文字来进行判断
#2
BOOL CViewTree::ExpandItem(HTREEITEM hTargetItem, CString strFindName)
{
if(hTargetItem != NULL)
{
CString strName = this->GetItemText(hTargetItem);
if(strName == strFindName)
{
SelectItem(hTargetItem);
Expand(hTargetItem, TVM_EXPAND);
}
else
{
HTREEITEM hChildItem = GetChildItem(hTargetItem);
while (hChildItem != NULL)
{
if(ExpandItem(hChildItem, strFindName) == TRUE)
{
return TRUE;
}
hChildItem = GetNextItem(hChildItem, TVGN_NEXT);
}
}
}
return FALSE;
}
#3
类似楼上的递归的方式比较可取
#4
就是调用CTreeCtrl::GetNextItem遍历树节点,找到节点以后调用CTreeCtrl::EnsureVisible()设置可见,调用CTreeCtrl::Expand()展开。
#1
HTREEITEM root;
HTREEITEM child;
HTREEITEM childChild;
int tempId = 0;
root = m_TreeCtrl.GetRootItem();
//第一层:层
while(root != NULL)
{
child = m_TreeCtrl.GetChildItem(root);
//第二层:屏幕
while (child != NULL)
{
childChild = m_TreeCtrl.GetChildItem(child);
//第二层:信号层
while (childChild != NULL)
{
tempId = m_TreeCtrl.GetItemData(childChild);
if(tempId == id)
{
m_TreeCtrl.SelectItem(childChild);
m_TreeCtrl.Expand(childChild, TVM_EXPAND);
return;
}
childChild = m_TreeCtrl.GetNextItem(childChild, TVGN_NEXT);
}
child = m_TreeCtrl.GetNextItem(child, TVGN_NEXT);
}
root = m_TreeCtrl.GetNextItem(root, TVGN_NEXT);
}
循环遍历树形控件的子项,但上面的例子是取子项的GetItemData,你可以获取子项的文字来进行判断
#2
BOOL CViewTree::ExpandItem(HTREEITEM hTargetItem, CString strFindName)
{
if(hTargetItem != NULL)
{
CString strName = this->GetItemText(hTargetItem);
if(strName == strFindName)
{
SelectItem(hTargetItem);
Expand(hTargetItem, TVM_EXPAND);
}
else
{
HTREEITEM hChildItem = GetChildItem(hTargetItem);
while (hChildItem != NULL)
{
if(ExpandItem(hChildItem, strFindName) == TRUE)
{
return TRUE;
}
hChildItem = GetNextItem(hChildItem, TVGN_NEXT);
}
}
}
return FALSE;
}
#3
类似楼上的递归的方式比较可取
#4
就是调用CTreeCtrl::GetNextItem遍历树节点,找到节点以后调用CTreeCtrl::EnsureVisible()设置可见,调用CTreeCtrl::Expand()展开。