gpt4 book ai didi

c - 帮助我理解这段 C 代码

转载 作者:太空狗 更新时间:2023-10-29 15:27:55 26 4
gpt4 key购买 nike

INT GetTree (HWND hWnd, HTREEITEM hItem, HKEY *pRoot, TCHAR *pszKey, 
INT nMax) {
TV_ITEM tvi;
TCHAR szName[256];
HTREEITEM hParent;
HWND hwndTV = GetDlgItem (hWnd, ID_TREEV);

memset (&tvi, 0, sizeof (tvi));

hParent = TreeView_GetParent (hwndTV, hItem);
if (hParent) {
// Get the parent of the parent of the...
GetTree (hWnd, hParent, pRoot, pszKey, nMax);

// Get the name of the item.
tvi.mask = TVIF_TEXT;
tvi.hItem = hItem;
tvi.pszText = szName;
tvi.cchTextMax = dim(szName);
TreeView_GetItem (hwndTV, &tvi); //send the TVM_GETITEM message?

lstrcat (pszKey, TEXT ("\\"));
lstrcat (pszKey, szName);
} else {
*pszKey = TEXT ('\0');
szName[0] = TEXT ('\0');
// Get the name of the item.
tvi.mask = TVIF_TEXT | TVIF_PARAM;
tvi.hItem = hItem;
tvi.pszText = szName;
tvi.cchTextMax = dim(szName);
if (TreeView_GetItem (hwndTV, &tvi))
//*pRoot = (HTREEITEM)tvi.lParam; //original
hItem = (HTREEITEM)tvi.lParam;
else {
INT rc = GetLastError();
}
}
return 0;
}

以注释“获取项目的名称”开头的代码块对我来说没有意义。如果您正在获取 ListView 项目,为什么代码会设置正在检索的项目的参数?如果您已经拥有这些值,则无需检索它们。

其次,靠近注释“原始”的是原始代码行,在嵌入式 visual c++ 4.0 下编译时会出现警告,但如果将完全相同的代码复制到 visual studio 2008 中,它将无法编译。由于我没有编写任何这段代码,并且正在努力学习,因此原作者可能在这一行上犯了错误吗? *pRoot 应该指向 HKEY 类型,但他正在转换为 HTREEITEM 类型,由于数据类型不匹配,这种类型永远不会工作?

最佳答案

The block of code that begins with the comment "Get the name of the item" does not make sense to me. If you are getting the listview item why does the code set the parameters of the item being retrieved, because if you already had the values there would be no need to retrieve them.

在那条评论之后,第一行是向 TreeView_GetItem(顺便说一下,它实际上是伪装的 SendMessage)指定我们要检索项目的文本和关联的 lParam。下一行指定我们需要有关信息的项目的句柄。

下一行指定必须保存检索到的文本的位置,即在函数开始时分配的 szName 缓冲区中;函数调用前的最后一行指定了此类缓冲区的大小,以避免缓冲区溢出。

我建议您查看 TreeView_GetItemTVITEM 的文档,以更好地理解发生了什么。

Secondly near the comment "original" is the original line of code which will compile with a varning under embedded visual c++, but if you copy the exact same code into visual studio 2008 it will not compile. Since I did not write any of this code and am trying to learn is it possible the original author made a mistake on this line, since the *pRoot should point to and HKEY type yet he is casting to an HTREEITEM type which should never work since the data types don't match?

不清楚代码在那里试图做什么;乍一看,我会说在与 TreeView 根节点中的每个项目关联的 lParam 中存储了一个注册表项的句柄,过程以这种方式检索它。尽管如此,如果是那样的话,(HTREEITEM) Actor 就完全没有意义了;可能这是一个错误,被编译器原谅了,因为它把所有的句柄都当作普通的 void *;如果我的假设是正确的,你应该保留原来的行,只需将 (HTREEITEM) 替换为 (HKEY)。

关于c - 帮助我理解这段 C 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2860240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com