工控编程吧

标题: 上位机MFC 如何遍历树控件 [打印本页]

作者: qq263946146    时间: 2019-7-29 14:48
标题: 上位机MFC 如何遍历树控件
GetChildItem,GetNextItem两函数结果就可以实现。
  1. void CGkbc8Dlg::Search(HTREEITEM hParent, int nOffset)
  2. {
  3.         CString strText = _T("");

  4.         HTREEITEM hChild = NULL;

  5.         //获得该项的子项
  6.         hChild = m_ctrlTree.GetChildItem(hParent);

  7.         while (hChild != NULL)
  8.         {
  9.                 //获得子项的文本
  10.                 strText = m_ctrlTree.GetItemText(hChild);

  11.                 //根据偏移量在字符串中插入空格
  12.                 for (int n = 0; n < nOffset; n++)
  13.                 {
  14.                         strText.Insert(0, _T("   "));
  15.                 }

  16.                 //在列表框中添加文本
  17.                 m_ctrlList.AddString(strText);

  18.                 //子项是否有子项
  19.                 if (m_ctrlTree.ItemHasChildren(hChild))
  20.                 {
  21.                         //遍历下级子项
  22.                         Search(hChild, nOffset + 1);
  23.                 }

  24.                 //获得子项的兄弟项
  25.                 hChild = m_ctrlTree.GetNextItem(hChild, TVGN_NEXT);
  26.         }
  27. }
复制代码
(, 下载次数: 2)