get lparam value form the htreeitem c++

1.4k views Asked by At

I created a dialog with a tree control which fetches data on to a list control when clicked on any particular node of the treecontrol. This is how i tried inserting nodes.

CString *sCommonAppkey = new CString(_szApp + sIsPath);

HTREEITEM hrCommon = m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM, _T("Common"), icoPlanit, icoPlanit, 0, 0, (LPARAM)(LPCTSTR)sCommonAppkey, NULL, NULL);

When clicked on a node it is being directed to the event handler "OnTvnSelchangedExample" and the data is fetched from the path specified in "lparam" parameter in the insertitem method of HTREEITEM.

void **CExample**::OnTvnSelchangedExample(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMTREEVIEW pNMTreeView = reinterpret_cast<LPNMTREEVIEW>(pNMHDR);

LPARAM lp = pNMTreeView->itemNew.lParam;

    CString *sTempKey = (CString *)lp;
    CString path = sTempKey->GetBuffer();
}

I can access the lparam value only in the event handler.

Now i want to implement search functionality for the entire tree's data.

so i need the lparam value of all Tree handles sequentially by iterating through it, so that i can search for the specific text in the tree. So without clicking on any node of the tree, is there any possibility to get the lparam value of Tree handle(HTREEITEM)

2

There are 2 answers

1
Johan Jönsson On BEST ANSWER

You can iterate through the tree from the root with TreeView_GetChild, there the handle is the tree handle. To get the handle, call TreeView_GetItem.

TVITEMEX item;
item.mask = TVIF_PARAM;
item.hItem = hrCommon;

TreeView_GetItem(handle_, &item);
CString* text = (CString*)item.lParam;
0
Andrew Komiagin On

The tree traversal is easy to implement using recursion:

void CMyTreeCtrl::Iterate(HTREEITEM hItem)
{
    if (hItem)
    {
        // Use the tree node corresponding to hItem
        // .....
        // End of using hItem
        hItem = GetNextItem(hItem, TVGN_CHILD);
        while (hItem)
        {
            Iterate(hItem);
            hItem = GetNextItem(hItem, TVGN_NEXT);
        }
    }
    else
    {
        HTREEITEM hItem = GetNextItem(NULL, TVGN_ROOT);
        while (hItem)
        {
            Iterate(hItem);
            hItem = GetNextItem(hItem, TVGN_NEXT);
        }
    }
}

If you want to get the item data you need to simply call GetItemData(hItem). It returns DWORD_PTR. So in your case you need to cast it to CString*. That's it.

IMPORTANT: In this example CMyTreeCtrl is derived from CTreeCtrl.