问题描述
- 求助!MFC CTreeCtrl如何整体移动某个项到另一个项内
-
即整体移动某项到另一项作为其子项。
(被移动项还有自己的子项的),有没有啥办法?
类里好像也没啥相关的函数这里不需要实现拖拽,就类似于删除某项后把其放入“回收站”项
解决方案
递归复制再删除
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);
}
解决方案三:
另外有个支持拖动的代码:
http://www.vckbase.com/index.php/wv/193
解决方案四:
没有这样的函数,只能删除CTreeCtrl的所有节点,然后按照这个变化重新构建生成新的节点
时间: 2024-12-30 06:33:02