2 qq 28254507 qq_28254507 于 2016.02.17 16:11 提问

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

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

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

4个回答

caozhy
caozhy   Ds   Rxr 2016.02.18 08: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);
}

caozhy
caozhy   Ds   Rxr 2016.02.18 08:34

图片说明

caozhy
caozhy   Ds   Rxr 2016.02.18 08:35

另外有个支持拖动的代码:
http://www.vckbase.com/index.php/wv/193

oyljerry
oyljerry   Ds   Rxr 2016.02.17 16:38

没有这样的函数,只能删除CTreeCtrl的所有节点,然后按照这个变化重新构建生成新的节点

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!