当空格键被按下时,删除的复选框将重新出现在treeview节点中 [英] Removed checkbox reappears in treeview node when spacebar is pressed

查看:370
本文介绍了当空格键被按下时,删除的复选框将重新出现在treeview节点中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用接受的解决方案 从我的 WM_INITDIALOG 处理程序中的 treeview节点中删除

I have used the accepted solution from this question to remove checkbox from a treeview node in my WM_INITDIALOG handler.

载入后,有正确的外观。在我选择节点并点击复选框的位置后,没有任何反应(复选框不会出现),这是正确的行为。

Upon loading, tree has a proper look. After I select the node and click on the place where checkbox would be, nothing happens ( checkbox does not appear ) which is the correct behavior.

但是,如果我选择节点并按 空格键添加到节点。

However, if I select the node and press spacebar the checkbox is automatically added to the node.

以下是说明问题的 WM_INITDIALOG 处理程序:

Here is the WM_INITDIALOG handler that illustrates the problem:

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;  

这里是从MSDN有趣的报价:

Here is interesting quote from MSDN:

版本5.80 。

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

也许这是我的问题的原因?

Perhaps this is the cause of my problem?

我已尝试处理 TVN_KEYDOWN 并设置项目状态或再次移除复选框,但未成功。

I ave tried handling TVN_KEYDOWN and set items state or again removing the checkbox but had no success.

我已经成功建立了乔治·波特它工作:

I have subclassed the tree, the way member Jonathan Potter suggested, and it worked:

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);
}



编辑结束



< h2> QUESTION:

如何从树节点 中正确移除

END OF EDIT

QUESTION:

How can I properly remove checkbox from a tree node so it never appears again ?

谢谢。

最好的问候。

推荐答案

子类控制,通过 WM_KEYDOWN 拦截空格键,不传递消息通过,如果重点是一个项目,你不想有一个复选框。

Sub-class the control, intercept the space key via WM_KEYDOWN, and don't pass the message through if the focus is on an item that you don't want to have a checkbox.

这篇关于当空格键被按下时,删除的复选框将重新出现在treeview节点中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆