/*
D:\Visual_Studio_repos\MFC\7\
*/
/*插入*/
void CMy7Dlg::OnBnClickedButton1()
{
/*
逻辑顺序:
1、利用GetSelectedItem函数获取被选中的节点
2、if判断是否有节点选中,如果没有的话创建一个HTREEITEM结构体作为节点进行插入
3、插入的顺序为 hInsertAfter = TVI_LAST,节点的名称为EDIT控件的名称
4、进行插入InsertItem 返回一个新节点的句柄
5、插入成功后,默认选中插入的节点
*/
CString mString;
GetDlgItemText(IDC_EDIT1,mString);
HTREEITEM hTree = m_tree_1.GetSelectedItem();
if (hTree == NULL) {
hTree = TVI_ROOT;
}
TVINSERTSTRUCT tv = { 0 };
tv.hParent = hTree;
tv.hInsertAfter = TVI_LAST;
tv.item.pszText = mString.GetBuffer(); //获取Cstring的字符串的指针
tv.item.mask = TVIF_TEXT;
HTREEITEM NewTree = m_tree_1.InsertItem(&tv); // 插入节点
m_tree_1.SelectItem(NewTree); //插入后 再进行默认选中
}
/*修改*/
void CMy7Dlg::OnBnClickedButton2()
{
CString mString;
CString TreeName;
GetDlgItemText(IDC_EDIT1, mString);
HTREEITEM hTree = m_tree_1.GetSelectedItem();
m_tree_1.SetItemText(hTree, mString.GetBuffer());
}
void CMy7Dlg::OnBnClickedButton3()
{
HTREEITEM hTree = m_tree_1.GetSelectedItem();
m_tree_1.DeleteItem(hTree);
}
/*实时EDIT节点名*/
void CMy7Dlg::OnTvnSelchangedTree1(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);
CString mString;
HTREEITEM hTree = m_tree_1.GetSelectedItem();
mString = m_tree_1.GetItemText(hTree);
SetDlgItemText(IDC_EDIT1, mString);
*pResult = 0;
}
![](https://img2018.cnblogs.com/blog/1586953/202002/1586953-20200208113535273-1269066287.png)