TreeControl

xiaoxiao2021-02-27  410

这个树控件类似于文件夹的结构,有一个跟节点,后面有子节点和叶节点,删除一个子节点后面的也节点也会被删除

节点的增添

void CTreeCtrlDemoDlg::OnBnClickedBtnAdd() { CString strInput; GetDlgItemTextW(IDC_EDIT_INPUT, strInput); //获取节点的选中状态,没有选中就为跟节点,否则为子节点 HTREEITEM hItem = m_tree.GetSelectedItem(); if (!hItem) hItem = TVI_ROOT; //对节点信息进行设置 TVINSERTSTRUCTW ts = { 0 }; ts.hParent = hItem; ts.item.pszText = (LPWSTR)(LPCTSTR)strInput; ts.item.mask = TVIF_TEXT; ts.hInsertAfter = TVI_LAST; //插入节点,并将插入的节点设为选中状态 HTREEITEM hNewItem = m_tree.InsertItem(&ts); m_tree.SelectItem(hNewItem); m_tree.EnsureVisible(hNewItem); }

删除

void CTreeCtrlDemoDlg::OnBnClickedBtnDel() { HTREEITEM hItem = m_tree.GetSelectedItem(); if (!hItem) return; HTREEITEM hParent = m_tree.GetParentItem(hItem); m_tree.DeleteItem(hItem); m_tree.SelectItem(hParent); }

修改

void CTreeCtrlDemoDlg::OnBnClickedBtnMod() { HTREEITEM hItem = m_tree.GetSelectedItem(); if (hItem) { CString strInput; GetDlgItemTextW(IDC_EDIT_INPUT, strInput); m_tree.SetItemText(hItem, strInput); } }
转载请注明原文地址: https://www.6miu.com/read-3738.html

最新回复(0)