Winform中TreeView中部分节点前面的CheckBox隐藏

该文章介绍了一个在WinForm应用中如何实现在TreeView控件中隐藏特定节点的CheckBox的方法。通过设置DrawMode为OwnerDrawAll并自定义DrawNode事件,配合使用TVITEM结构和SendMessage函数来修改节点的状态,从而达到隐藏CheckBox的效果。
摘要由CSDN通过智能技术生成

WinForm中TreeView中部分节点前面的CheckBox隐藏

在Winform中需要想实现TreeView树中部分节点前面的CheckBox隐藏,Winform并没有提供相应的属性来隐藏节点的CheckBox,需要用户重绘节点。
项目截图:

一下就是一个全代码
在这里插入图片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeViewDemoWindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.treeView1.ExpandAll();

                this.treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;
                this.treeView1.DrawNode += new DrawTreeNodeEventHandler(tvCheck_DrawNode);

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void tvCheck_DrawNode(object sender, DrawTreeNodeEventArgs e)
        {
            try
            {
                if (e.Node.Text == "节点7" || e.Node.Text == "节点6")
                {
                    HideCheckBox(this.treeView1, e.Node);
                }
                e.DrawDefault = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            try
            {
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        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)
        {
            try
            {
                TVITEM tvi = new TVITEM();
                tvi.hltem = node.Handle;
                tvi.mask = TVIF_STATE;
                tvi.stateMask = TVIS_STATEIMAGEMASK;
                tvi.state = 0;
                SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
        private struct TVITEM 
        {
            public int mask;
            public IntPtr hltem;
            public int state;
            public int stateMask;
            [MarshalAs(UnmanagedType.LPTStr)]
            public string IpszText;
            public int cchTextMax;
            public int iImage;
            public int iSelectedImange;
            public int cChildren;
            public IntPtr IParam;
        }
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, ref TVITEM Iparam);


    }
}

项目地址:https://download.csdn.net/download/xwwwill/87789489

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值