学习:Tree Control

Posted zpchcbd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了学习:Tree Control相关的知识,希望对你有一定的参考价值。

/*
D:Visual_Studio_reposMFC7*/

/*插入*/
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;
}

技术图片

以上是关于学习:Tree Control的主要内容,如果未能解决你的问题,请参考以下文章

VC6.0中的Tree Control控件,如何给有的节点设checkbox,而有的节点不设?具体代码如何实现?

Nginx——Nginx启动报错Job for nginx.service failed because the control process exited with error code(代码片段

树控件(Tree Control),标签控件(tab control)

Control-Tree

ABAP Tree Control的使用

Create Hierarchical Tree To Control Records In Oracle Forms