首页 > Windows开发 > 详细

给C#的treeview控件的部分节点添加checkbox

时间:2015-03-27 00:48:10      阅读:1095      评论:0      收藏:0      [点我收藏+]

一、先初始化treeview

this.treeView1.CheckBoxes = true;
this.treeView1.ShowLines = false;
this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;
this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView_DrawNode);
二、实现委托事件

private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
//隐藏节点前的checkbox
if (e.Node.ImageIndex==6)
HideCheckBox(this.treeView1, 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);
将上面的实用工具类TreeViewUtility中,以方便使用,具体如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace SituationProcess//名称空间根据需要自行修改
{
    /// <summary>
    /// 给treeview控件的部分节点添加checkbox
    /// </summary>
    class TreeViewUtility
    {
        TreeView tvTreeView;

        public TreeViewUtility(TreeView treeView)
        {
            this.tvTreeView = treeView;
        }

        public void InitializeTreeView()
        {
            this.tvTreeView.CheckBoxes = true;
            this.tvTreeView.ShowLines = true;
            this.tvTreeView.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;
            this.tvTreeView.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(tvTreeView_DrawNode);
        }

        private void tvTreeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            //隐藏节点前的checkbox
            if (e.Node.Text == "数据集集合")//隐藏文本名称为“数据集集合”的TreeView控件节点
                HideCheckBox(this.tvTreeView, 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);
    }
}

 

给C#的treeview控件的部分节点添加checkbox

原文:http://www.cnblogs.com/rainbow70626/p/4370367.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!