确定是否取消选择树视图中所有节点的最安全方法?

Safest method to determine if all nodes in treeview are deselected?

我需要取消选择树视图控件中的所有选定节点。

正在搜索 MSDN documentation I found TreeView_SelectItem 宏。

文档指出:

If the hitem parameter is NULL, the control is set to have no selected item.

这解决了我问题的第一部分 -> 取消选择所有节点。

我的问题是关于我在 Return valueRemarks 部分为 [=16] 阅读的以下部分=]:

Return value

Returns the handle to the item if successful. For most cases, the message returns a NULL value to indicate an error. See the Remarks section for details.

Remarks

This message will return NULL if the item being retrieved is the root node of the tree. For example, if you use this message with the TVGN_PARENT flag on a first-level child of the tree view's root node, the message will return NULL.

在使用 TreeView_SelectItem 取消选择所有节点后,我可以使用哪个 macro/message 检查是否选择了任何项目?

我必须 100% 确定返回的结果不是错误的结果。

使用

HTREEITEM hItem = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_CARET, 0);

此参数组合中不能出现 "error"。

关于文档:从我的角度来看,询问根节点是否有父节点并不是错误。它只有 none,所以它 return 是 NULL。错误意味着这里 "The function return NULL because you are asking for something that can't exist..." 但即使你得到一个结果你也可以处理。

如果 window 存在并且系统尚未受到无效指针写入或类似操作的损害,我认为此函数没有错误可以防止 return 无效结果.

TreeView_SelectItem() 有自己的 return 值:

Return value

Returns TRUE if successful, or FALSE otherwise.

如果 TreeView_SelectItem(hwndTV, NULL) return 为真,您保证不会在那个时刻 select 编辑任何项目。如果您随后检查 TreeView 以查看是否有任何项目被 select 编辑并且碰巧找到了一个,这意味着有人被允许 select 在您没有 select 之后的项目编辑他们。因此,只要确保您的代码不允许新的 select 离子,直到您准备好它们。