ProgJ
2016-02-17 08:11
采纳率: 88.1%
浏览 2.1k

求助!MFC CTreeCtrl如何整体移动某个项到另一个项内

即整体移动某项到另一项作为其子项。
(被移动项还有自己的子项的),有没有啥办法?
类里好像也没啥相关的函数

这里不需要实现拖拽,就类似于删除某项后把其放入“回收站”项

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • threenewbee 2016-02-18 00:32
    已采纳

    递归复制再删除

    HTREEITEM hR;
    
    void CMtapp1Dlg::OnButton1() 
    {
        // TODO: Add your control notification handler code here
        HTREEITEM hRoot = m_tree.InsertItem("My Computer");
        HTREEITEM hC = m_tree.InsertItem("C:\\", hRoot);
        HTREEITEM hD1 = m_tree.InsertItem("My Documents", hC);
        HTREEITEM hCf1 = m_tree.InsertItem("doc1.doc", hC);
        HTREEITEM hCf2 = m_tree.InsertItem("contract.txt", hC);
        HTREEITEM hCf3 = m_tree.InsertItem("data.bin", hC);
        HTREEITEM hCf4 = m_tree.InsertItem("simple.c", hC);
        HTREEITEM hCf5 = m_tree.InsertItem("testpaper.docx", hD1);
        HTREEITEM hD = m_tree.InsertItem("D:\\", hRoot);
        HTREEITEM hDf1 = m_tree.InsertItem("backup.img", hD);
        HTREEITEM hDf2 = m_tree.InsertItem("pic1.bmp", hD);
        hR = m_tree.InsertItem("Recycle Bins", hRoot);
        m_btn1.EnableWindow(FALSE);
    }
    
    void CMtapp1Dlg::MoveNode(HTREEITEM hP, HTREEITEM hS)
    {
        HTREEITEM h = m_tree.GetChildItem(hS);
        while (h != NULL)
        {
            HTREEITEM hc = m_tree.InsertItem(m_tree.GetItemText(h), hP);
            MoveNode(hc, h);
            h = m_tree.GetNextItem(h, TVGN_NEXT);
        }
    }
    
    void CMtapp1Dlg::OnButton2() 
    {
        // TODO: Add your control notification handler code here
        HTREEITEM hsel = m_tree.GetSelectedItem();
        if (hsel != NULL && hsel != hR)
        {
            HTREEITEM h = m_tree.InsertItem(m_tree.GetItemText(hsel), hR);
            MoveNode(h, hsel);
            m_tree.DeleteItem(hsel);
        }
        m_tree.Expand(hR, TVE_EXPAND);
    }
    
    
    评论
    解决 无用
    打赏 举报
  • 查看更多回答(3条)

相关推荐 更多相似问题