c++ - 使用 CTreeCtrl 检索 LPARAM 中的项目

标签 c++ mfc

我必须递归地向树控件中的项目添加子键列表。 我正在使用 InsertItem,如下所示。我附加了此元素的路径,以便在单击树控件时可以检索。我可以添加该值但无法检索它。

void CMyDlg::FillTreeWithRegistryKeysEx(CString sPath, HTREEITEM hItem)
{
    CString sRegKey = sPath.Left(sPath.Find(_T("\\")));
    sPath = sPath.Mid((sPath.Find(_T("\\")) + 1));
    sPath = CleanRegistryKey(sPath);

    int nKeyCount;
    CString  sSubKey = _T("");
    HKEY handle = GetHkey(sRegKey);
    HTREEITEM hReItem = NULL;
    HKEY phkey;

    std::vector<CString> sSubFolders;

    if (RegOpenKeyEx(handle, sPath, 0, KEY_ALL_ACCESS, &phkey) == ERROR_SUCCESS)
        sSubFolders = EnumRegistryKey(phkey);

    nKeyCount = sSubFolders.size();

    for (int nIndex = 0; nIndex < nKeyCount; nIndex++)
    {
        sSubKey = sPath + _T("\\") +sSubFolders.at(nIndex);
        hReItem = m_cTreeCtrl.InsertItem(TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM | TVIF_STATE, sSubFolders.at(nIndex), 
                                        icoClosedFolder, icoOpenFolder, 0, 0, (LPARAM)(LPCTSTR)sSubKey, hItem, TVI_LAST);

        FillTreeWithRegistryKeys(handle, sSubKey, hReItem);
    }
    RegCloseKey(phkey);
}


While retrieving the string is always blank.

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

    LPARAM lp = pNMTreeView->itemNew.lParam;
    CString sKey = (LPCTSTR)lp;
}


what is going wrong? 

最佳答案

传递给 InsertItem 的应用程序定义的 LPARAM 是一个字符串对象,一旦函数完成,它就会超出范围,更不用说它会被每个循环迭代替换为一个新字符串。我认为这行不通。

然而,TreeView 控件分配了它自己的文本拷贝,因此您可以安全地使用本地缓冲区填充 pszText 成员,就像您正在做的那样。

关于c++ - 使用 CTreeCtrl 检索 LPARAM 中的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39264221/

相关文章:

c++ - 如何在 OnInitDialog 函数中最小化 MFC 应用程序?

c++ - 在 MFC/Visual C++ 中覆盖 OnCancel 按钮

C++/MFC 如何从 PC 复制多个文件到 WinCE(移动设备)?

c++ - Boost Thread在创建为类的成员函数时不会分离

c++ - OpenCv2.2 C++ filter2D

c++ - 不能对 std::set<std::string, std::less<>> 使用用户提供的比较函数

c++ - move 赋值运算符 C++

mfc - MFC 项目中的虚假内存泄漏

mfc - C编辑,WM_​​PASTE

c++ - 带有复制运算符的右值