C#TreeView控件控制某些节点的checkbox不显示

来源:互联网 发布:小论文数据大多造假 编辑:程序博客网 时间:2024/05/22 06:40

private void form_Load(object sender, EventArgs e)

{

            this.tvCheck.CheckBoxes = true;
            this.tvCheck.ShowLines = true;
            this.tvCheck.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;
            this.tvCheck.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(tvCheck_DrawNode);

}

private void tvCheck_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {

             if (e.Node.Text == "aaaaaa")   //需要隐藏checkbox的节点名称
                {
                    HideCheckBox(this.tvCheck, e.Node);
                }

             e.DrawDefault = true;
        }

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;
        private void HideCheckBox(TreeView tvw, TreeNode node)
        {
            TVITEM tvi = new TVITEM();
            tvi.hItem = node.Handle;
            tvi.mask = TVIF_STATE;
            tvi.stateMask = TVIS_STATEIMAGEMASK;
            tvi.state = 0;
            SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
        }

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

 

阅读全文
1 0
原创粉丝点击