简体   繁体   English

将项目添加到CTreectrl C ++

[英]Adding item to CTreectrl C++

I have a requirement of converting a project from VB to C++. 我需要将项目从VB转换为C ++。

There is Tree control in VB. VB中有树控件。 For adding items to Tree control "Add" Method(Nodes Collection) has been used, which contains a parameter called "key" and the same key can be retrieved on click of that particular item. 为了将项目添加到树控件中,已使用“添加”方法(节点集合),该方法包含一个称为“键”的参数,单击该特定项目即可检索相同的键。

Is there any such provision in CTreeCtrl InsertItem function using TVITEM or TVITEMEX structure where we can add a key to each item of the tree control and get it back when clicked on it? 使用TVITEM或TVITEMEX结构的CTreeCtrl InsertItem函数中是否有任何此类规定,我们可以在其中向树控件的每个项目添加一个键,并在单击它时将其取回?

To create your root item: 创建根项目:

TV_INSERTSTRUCT tvInsertStruct;
tvInsertStruct.hParent=NULL;
tvInsertStruct.hInsertAfter=TVI_LAST;
tvInsertStruct.item.pszText=_T("ROOT");
tvInsertStruct.item.mask=TVIF_TEXT;

const HTREEITEM hRootItem= m_tree.InsertItem(&tvInsertStruct);

To insert sub-items hanging on the root: 要插入根目录上悬挂的子项:

for(int i=0; i<SomeCollection.GetCount(); i++)
{
    const CElement* pElement= SomeCollection.GetAt(i);
    ASSERT(pElement);

    CString Name = pElement->GetName();

    tvInsertStruct.hParent = hRootItem;
    tvInsertStruct.hInsertAfter = TVI_LAST;
    const LPTSTR cutJobNameTemp = Name.GetBuffer(0);
    tvInsertStruct.item.pszText = cutJobNameTemp;
    tvInsertStruct.item.mask = TVIF_TEXT;

    HTREEITEM hItem = m_tree.InsertItem(&tvInsertStruct);       
    ASSERT(hItem);      
    tree.SetItemData(hItem, (DWORD_PTR)pElement);
}

The code line that answers your question is SetItemData : with it you can directly associate a tree node handle with a memory address.111 回答您问题的代码行是SetItemData :您可以直接将树节点句柄与内存地址相关联SetItemData

To see all nodes open just add: 要查看所有打开的节点,只需添加:

ExpandTreeCtrl(m_tree);

NOTE: I know the following is not the cleanest approach to handle the selection of an item on the tree, so I replaced it with a more proper way that also handles keyboard 注意: 我知道以下不是处理树上项目选择的最干净方法,因此我以更正确的方式替换了它,该方式也可以处理键盘

To get an entry point for your app to respond to Clicks on the tree, you can add in its parent dialog (or control)'s message map 若要获取应用程序响应树上单击的入口点,可以在其父对话框(或控件)的消息图中添加

 ON_NOTIFY(NM_CLICK, IDC_TREE, OnNMClickTree) 

and implement its handling function 并实现其处理功能

 void CMyDialog::OnNMClickTree(NMHDR *pNMHDR, LRESULT *pResult) { UINT flags; CPoint point; GetCursorPos(&point); *pResult= 0; CTreeCtrl* pTree= dynamic_cast <CTreeCtrl*> (this->GetDlgItem(pNMHDR->idFrom)); if(pTree) { pTree->ScreenToClient(&point); HTREEITEM hItem = pTree->HitTest(point, &flags); if( (flags & TVHT_ONITEMINDENT) || (flags & TVHT_ONITEMBUTTON) ) //do nothing when clicking on the [+]expand / [-]collapse of the tree return; if(!hItem) return; // If you want to get item text: CString sText= pTree->GetItemText(hItem); //To get your element: CElement* pElement = (CElement*)pTree->GetItemData(hItem); } } 

To get an entry point for your app to respond to the change of the currently selected item on the tree, you can add in its parent dialog (or control)'s message map 若要获取应用程序以响应树上当前所选项目的更改的入口点,可以在其父对话框(或控件)的消息图中添加

void CMyDialog::OnTreeCtrlSelChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
    NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*) pNMHDR;
    HTREEITEM hItem = pNMTreeView->itemNew.hItem;

    if(!hItem)
        return;

    // If you want to get item text:
    CString sText= m_tree.GetItemText(hItem);

    //To get your element:
    CElement* pElement = (CElement*)m_tree.GetItemData(hItem);
}

and implement its handling function 并实现其处理功能

 void CMyDialog::OnTreeCtrlSelChanged(NMHDR* pNMHDR, LRESULT* pResult) { NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*) pNMHDR; HTREEITEM hItem = pNMTreeView->itemNew.hItem; if(!hItem) return; // If you want to get item text: CString sText= m_tree.GetItemText(hItem); //To get your element: CElement* pElement = (CElement*)m_tree.GetItemData(hItem); } 

The line that is now dereferencing to access the CElement data associated with the tree node is GetItemData . 现在取消引用以访问与树节点关联的CElement数据的行是GetItemData Then do what you intend with the pointer you got. 然后使用您得到的指针执行您打算做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM