工控编程吧

标题: 上位机MFC如何展开和选择树项 [打印本页]

作者: qq263946146    时间: 2019-7-29 14:24
标题: 上位机MFC如何展开和选择树项
这个功能相对简单,控件自己有提供相应函数。Expand,Select。
  1. //在树控件中添加连线和按钮
  2.         m_ctrlTree.ModifyStyle(0, TVS_HASLINES | TVS_LINESATROOT | TVS_HASBUTTONS);

  3.         CString strText = _T("");

  4.         //在树控件中插入项
  5.         HTREEITEM hRoot = m_ctrlTree.InsertItem(_T("Root"));

  6.         for (int i = 0; i < 4; i++)
  7.         {
  8.                 strText.Format(_T("Item %d"), i);

  9.                 //在树控件中插入项
  10.                 HTREEITEM hParent = m_ctrlTree.InsertItem(strText, hRoot);

  11.                 for(int j = 0; j < 5; j++)
  12.                 {
  13.                         strText.Format(_T("SubItem %d %d"), i, j);

  14.                         //在树控件中插入项
  15.                         m_ctrlTree.InsertItem(strText, hParent);
  16.                 }

  17.                 //展开树项
  18.                 m_ctrlTree.Expand(hParent, TVE_EXPAND);       
  19.         }

  20.         //展开树项
  21.         m_ctrlTree.Expand(hRoot, TVE_EXPAND);

  22.         //选择树项
  23.         m_ctrlTree.Select(hRoot, TVGN_CARET);
复制代码
(, 下载次数: 0)