setting/getting lParam value using CTreeCtrl

976 views Asked by At

I'm using CTreeCtrl to display some data. With each entry in the tree, I have some associated data which I keep in a struct. I save this data with the item by putting the pointer to the struct in the lParam value in each entry in the tree.

This is my add entries code to the tree:

void CClassView::AddElementToTree(Element* _pElement, HTREEITEM _hRoot)
{
    HTREEITEM hBranch;

    TVINSERTSTRUCT tvInsert;
    ZeroMemory(&tvInsert, sizeof(tvInsert));
    tvInsert.hParent = _hRoot;
    tvInsert.hInsertAfter = NULL;
    tvInsert.item.mask = TVIF_TEXT;

    WCHAR szText[64] = {'\0'};
    tvInsert.item.pszText = szText;

    for(std::vector<Element*>::iterator i = _pElement->pChildren.begin(); i != _pElement->pChildren.end(); ++i)
    {
        wcscpy_s(szText, (*i)->GetName().c_str());
        tvInsert.item.lParam = (LPARAM)(*i);

        hBranch = m_wndClassView.InsertItem(&tvInsert);
        AddElementToTree(*i, hBranch);
    }
}

Essentially this function recursively add an element to the tree, with its children. _pElement I pass externally. This is a member variable of my class so I know it is not destroyed unless the program ends.

When the user selects an entry in the tree view, I handle the selchanged message:

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

    Element* pElement = (Element*)pNMTreeView->itemNew.lParam;

    *pResult = 0;
}

pElement is always NULL. I debugged the program and it seems lParam is also zero.

Am I doing anything wrong? I know that the memory of my struct has not been deallocated. Is it something wrong I'm doing adding the entry to the tree?

Any help would be appreciated.

1

There are 1 answers

1
xMRi On BEST ANSWER

TVIF_PARAM must be set in the mask!