工控编程吧
标题: 上位机MFC 如何遍历树控件 [打印本页]
作者: qq263946146 时间: 2019-7-29 14:48
标题: 上位机MFC 如何遍历树控件
GetChildItem,GetNextItem两函数结果就可以实现。
- void CGkbc8Dlg::Search(HTREEITEM hParent, int nOffset)
- {
- CString strText = _T("");
- HTREEITEM hChild = NULL;
- //获得该项的子项
- hChild = m_ctrlTree.GetChildItem(hParent);
- while (hChild != NULL)
- {
- //获得子项的文本
- strText = m_ctrlTree.GetItemText(hChild);
- //根据偏移量在字符串中插入空格
- for (int n = 0; n < nOffset; n++)
- {
- strText.Insert(0, _T(" "));
- }
- //在列表框中添加文本
- m_ctrlList.AddString(strText);
- //子项是否有子项
- if (m_ctrlTree.ItemHasChildren(hChild))
- {
- //遍历下级子项
- Search(hChild, nOffset + 1);
- }
- //获得子项的兄弟项
- hChild = m_ctrlTree.GetNextItem(hChild, TVGN_NEXT);
- }
- }
复制代码
(, 下载次数: 2)