gpt4 book ai didi

c++ - 如何使用 TVS_CHECKBOXES 样式删除特定 TreeView 项上的复选框

转载 作者:行者123 更新时间:2023-11-28 02:15:27 24 4
gpt4 key购买 nike

我找不到一种方法来禁用我的 TreeView 控件中特定项目的复选框(实际上我只需要启用特定项目的复选框)。

我已阅读 this , thisthis回答无济于事。

在创建 TreeView 项(不需要复选框)时,我尝试将标志设置为:

tvinsert.item.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM; // attributes
tvinsert.item.stateMask = TVIS_STATEIMAGEMASK;
tvinsert.item.state = INDEXTOSTATEIMAGEMASK(0);

应该隐藏一个项目的复选框但是MSDN documentation

Version 5.80. Displays a check box even if no image is associated with the item.

我正在创建 TreeView 窗口控件

g_WindowHandleTreeView = CreateWindow(
WC_TREEVIEW,
"", //caption not required
TVS_TRACKSELECT | WM_NOTIFY | WS_CHILD | TVS_HASLINES | TVS_LINESATROOT | WS_VISIBLE/* | TVS_CHECKBOXES*/,
CW_USEDEFAULT,
CW_USEDEFAULT,
300,
550,
g_WindowHandlePannelStructure,
NULL,
(HINSTANCE)GetWindowLong(g_WindowHandlePannelStructure, GWL_HINSTANCE),
NULL);

DWORD dwStyle = GetWindowLong(g_WindowHandleTreeView, GWL_STYLE);
dwStyle |= TVS_CHECKBOXES;
SetWindowLongPtr(g_WindowHandleTreeView, GWL_STYLE, dwStyle);

然后用

创建 TreeView 项目
// Clear the treeview
TreeView_DeleteAllItems(hwnd);

// Tree items
std::vector<HTREEITEM> root_sub;
std::vector<HTREEITEM> mesh_items;
std::vector<HTREEITEM> mesh_items_sub;

TV_INSERTSTRUCT tvinsert = { 0 }; // struct to config the tree control
tvinsert.hParent = TVI_ROOT; // top most level Item
tvinsert.hInsertAfter = TVI_LAST; // root level item attribute.
tvinsert.item.mask = TVIF_TEXT | TVIF_PARAM; // attributes
tvinsert.item.stateMask = TVIS_STATEIMAGEMASK;
tvinsert.item.state = INDEXTOSTATEIMAGEMASK(0);
// ^^^ here trying to disable the checkbox but only prior to Version 5.80. ?

// Create root item
std::string rootTxt = "Model";
tvinsert.item.pszText = (LPSTR)rootTxt.c_str();
tvinsert.item.lParam = ID_MESH_ALL;
HTREEITEM Root = (HTREEITEM)SendMessage(hwnd, TVM_INSERTITEM, 0, (LPARAM)&tvinsert);

// Create path item
std::string pathTxt = std::string("Path : ") + pModel->objPath;
tvinsert.hParent = Root;
tvinsert.item.pszText = (LPSTR)pathTxt.c_str();
tvinsert.item.lParam = 0;
root_sub.push_back((HTREEITEM)SendMessage(hwnd, TVM_INSERTITEM, 0, (LPARAM)&tvinsert));

// More items....................

// Now attempting to change flags to ENABLE+CHECK the checkbox (which are always enabled anyways...)
tvinsert.item.state = INDEXTOSTATEIMAGEMASK(2);

// Create mesh header
std::string meshTxt = std::string("Mesh #") + std::to_string(mesh_items.size() + 1) + std::string(" - ") + std::to_string(mesh.v.size()) + std::string(" vertices");
tvinsert.hInsertAfter = mesh_root;
tvinsert.hParent = mesh_root;
tvinsert.item.pszText = (LPSTR)meshTxt.c_str();
tvinsert.item.lParam = ID_MESH_0 + mesh_items.size();
mesh_items.push_back((HTREEITEM)SendMessage(hwnd, TVM_INSERTITEM, 0, (LPARAM)&tvinsert));

// Disable flags
tvinsert.item.state = INDEXTOSTATEIMAGEMASK(0);

// ...

那么反过来呢?除了为它提供不同的 Windows 进程之外,我不明白对我的 TreeView 控件进行子类化意味着什么。

预期的行为是只在选择的 TreeView 项目旁边显示一个复选框。我目前有一个用于所有项目的复选框。

感谢您的见解。

最佳答案

这里是如何创建一个带有复选框的 TreeView 控件,以及如何在选定节点上删除复选框。

首先创建一个没有 TVS_CHECKBOXES 复选框样式的窗口控件。例如:

g_WindowHandleTreeView = CreateWindow(
WC_TREEVIEW,
"",
TVS_TRACKSELECT | WS_CHILD | TVS_HASLINES | TVS_LINESATROOT | WS_VISIBLE | TVS_HASBUTTONS,
CW_USEDEFAULT,
CW_USEDEFAULT,
300,
550,
g_WindowHandlePannelStructure, // is the parent window control
NULL,
(HINSTANCE)GetWindowLong(g_WindowHandlePannelStructure, GWL_HINSTANCE),
NULL);

然后添加复选框样式:

DWORD dwStyle = GetWindowLong(g_WindowHandleTreeView, GWL_STYLE);
dwStyle |= TVS_CHECKBOXES;
SetWindowLongPtr(g_WindowHandleTreeView, GWL_STYLE, dwStyle);

现在用插入结构为 TreeView 准备一个项目,例如:

TV_INSERTSTRUCT tvinsert = { 0 }; // struct to config the tree control
tvinsert.hParent = TVI_ROOT; // root item
tvinsert.hInsertAfter = TVI_LAST; // last current position
tvinsert.item.mask = TVIF_TEXT | TVIF_PARAM | TVIF_STATE; // attributes
tvinsert.item.stateMask = TVIS_STATEIMAGEMASK;
tvinsert.item.state = 0;
tvinsert.item.pszText = (LPSTR)"Root node";
tvinsert.item.lParam = SOME_ID; // ID for the node

并使用 SendMessage(...) 调用插入节点:

HTREEITEM Root = (HTREEITEM)SendMessage(hwnd, TVM_INSERTITEM, 0, (LPARAM)&tvinsert);

此时节点显示一个复选框(即使 item.state 设置为 0)所以剩下要做的就是删除它:

TVITEM tvi;
tvi.hItem = Root; // The item to be "set"/modified
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0; // setting state to 0 again
TreeView_SetItem(hwnd, &tvi);

就是这样。

关于c++ - 如何使用 TVS_CHECKBOXES 样式删除特定 TreeView 项上的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34161879/

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