TreeView Remove CheckBox by some Nodes

后端 未结 3 1144
被撕碎了的回忆
被撕碎了的回忆 2020-11-27 05:16

I want remove CheckBoxes where the Node.Type is 5 or 6. I use this code:

private void TvOne_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
    int type =         


        
3条回答
  •  庸人自扰
    2020-11-27 05:59

    Using TreeViewExtensions.

    Usage sample:

    private void MyForm_Load(object sender, EventArgs e)
    {
         this.treeview1.DrawMode = TreeViewDrawMode.OwnerDrawText;
         this.treeview1.DrawNode += new DrawTreeNodeEventHandler(arbolDependencias_DrawNode);
    }
    
    void treeview1_DrawNode(object sender, DrawTreeNodeEventArgs e)
    {
        if (e.Node.Level == 1) e.Node.HideCheckBox();
        e.DrawDefault = true;
    }
    

    Here is the answer's code as an Extension method, using this you can do:

    public static class TreeViewExtensions
    {
        private const int TVIF_STATE = 0x8;
        private const int TVIS_STATEIMAGEMASK = 0xF000;
        private const int TV_FIRST = 0x1100;
        private const int TVM_SETITEM = TV_FIRST + 63;
    
        [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
        private struct TVITEM
        {
            public int mask;
            public IntPtr hItem;
            public int state;
            public int stateMask;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string lpszText;
            public int cchTextMax;
            public int iImage;
            public int iSelectedImage;
            public int cChildren;
            public IntPtr lParam;
        }
    
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam,
                                                 ref TVITEM lParam);
    
        /// 
        /// Hides the checkbox for the specified node on a TreeView control.
        /// 
        public static void HideCheckBox(this TreeNode node)
        {
            TVITEM tvi = new TVITEM();
            tvi.hItem = node.Handle;
            tvi.mask = TVIF_STATE;
            tvi.stateMask = TVIS_STATEIMAGEMASK;
            tvi.state = 0;
            SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
        }
    }
    

提交回复
热议问题