我已使用已接受的解决方案from this question从我的WM_INITDIALOG
处理程序中的树视图节点中删除复选框。
加载后,树具有正确的外观。选择节点并单击复选框所在的位置后,没有任何反应(复选框不会出现),这是正确的行为。
但是,如果我选择节点并按 空格键,复选框会自动添加到节点中。
以下是解释问题的WM_INITDIALOG
处理程序:
case WM_INITDIALOG:
{
// get treeview handle
HWND TreeView = GetDlgItem( hDlg, IDC_TREE1 );
/************ enable checkboxes **************/
DWORD dwStyle = GetWindowLong( TreeView , GWL_STYLE);
dwStyle |= TVS_CHECKBOXES;
SetWindowLongPtr( TreeView , GWL_STYLE, dwStyle );
/************ add items and subitems **********/
// add root item
TVINSERTSTRUCT tvis = {0};
tvis.item.mask = TVIF_TEXT;
tvis.item.pszText = L"This is root item";
tvis.hInsertAfter = TVI_LAST;
tvis.hParent = TVI_ROOT;
HTREEITEM hRootItem = reinterpret_cast<HTREEITEM>( SendMessage( TreeView ,
TVM_INSERTITEM, 0, reinterpret_cast<LPARAM>( &tvis ) ) );
// and here is an example of removing a checkbox
TVITEM tvi;
tvi.hItem = hRootItem ;
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = 0;
TreeView_SetItem( TreeView, &tvi );
// add firts subitem for the hTreeItem
memset( &tvis, 0, sizeof(TVINSERTSTRUCT) );
tvis.item.mask = TVIF_TEXT;
tvis.item.pszText = L"This is first subitem";
tvis.hInsertAfter = TVI_LAST;
tvis.hParent = hRootItem;
HTREEITEM hTreeSubItem1 = reinterpret_cast<HTREEITEM>( SendMessage( TreeView ,
TVM_INSERTITEM, 0, reinterpret_cast<LPARAM>( &tvis ) ) );
// now we insert second subitem for hRootItem
memset( &tvis, 0, sizeof(TVINSERTSTRUCT) );
tvis.item.mask = TVIF_TEXT | TVIF_STATE; // added extra flag
tvis.item.pszText = L"This is second subitem";
tvis.hInsertAfter = TVI_LAST;
tvis.hParent = hRootItem;
HTREEITEM hTreeSubItem2 = reinterpret_cast<HTREEITEM>( SendMessage( TreeView ,
TVM_INSERTITEM, 0, reinterpret_cast<LPARAM>( &tvis ) ) );
}
return (INT_PTR)TRUE;
Here引自MSDN:
版本5.80。即使没有图像与项目相关联,也会显示一个复选框。
也许这是我问题的原因?
我尝试过处理TVN_KEYDOWN
并设置项状态或再次移除复选框,但没有成功。
我已经将树 Jonathan Potter 建议的方式继承了树,并且它有效:
LRESULT CALLBACK TreeProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam,
UINT_PTR uIdSubclass, DWORD_PTR dwRefData )
{
switch (message)
{
case WM_KEYDOWN:
{
// reject spacebar if tree node doesn't have checkbox
if( wParam == VK_SPACE )
{
HTREEITEM ht = TreeView_GetSelection( hwnd );
TVITEM tvItem;
// Prepare to receive the desired information.
tvItem.mask = TVIF_HANDLE | TVIF_STATE;
tvItem.hItem = (HTREEITEM)ht;
tvItem.stateMask = TVIS_STATEIMAGEMASK;
// Request the information.
TreeView_GetItem( hwnd, &tvItem );
// reject if it's not checked, or pass default value otherwise
switch( tvItem.state >> 12 )
{
case 0:
MessageBeep(0);
return FALSE;
break;
case 1:
case 2:
default:
return ::DefSubclassProc( hwnd, message, wParam, lParam );
break;
}
}
}
break;
case WM_NCDESTROY:
::RemoveWindowSubclass( hwnd, TreeProc, 0 );
break;
}
return ::DefSubclassProc( hwnd, message, wParam, lParam);
}
如何从树节点 正确删除复选框,以便它再也不会出现?
谢谢。
最好的问候。
答案 0 :(得分:2)
对控件进行子类化,通过WM_KEYDOWN
拦截空格键,如果焦点位于您不希望拥有复选框的项目上,则不会传递消息。