我必须递归地向树控件中的项目添加子键列表。 我正在使用 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/