给C#的treeview控件的部分节点添加checkbox
一、先初始化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 + ;
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 = ;
SendMessage(tvw.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
} [StructLayout(LayoutKind.Sequential, Pack = , 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的更多相关文章
- Treeview控件的Node节点延迟加载
		
Treeview控件是一个很常用的控件,用于展示资源或者组织结构的时候很方便,通常会在系统启动时进行资源的加载和节点目录的初始化,但在资源较多和层级较深的情况下,所有节点加载出来会耗费太多时间,影响体 ...
 - 树TreeView控件与DataTable交互添加节点(最高效的方法)
		
#region "读取树结点从Datatable" /// <summary> /// 读取树结点从Datatable" /// </summary&g ...
 - 循环访问 TreeView 控件的所有节点
		
创建测试每个节点的递归过程 . private void PrintRecursive(TreeNode treeNode) { // Print the node. System.Diagnosti ...
 - ASP.Net中的TreeView控件中对节点的上移和下移操作
		
Web中的TreeView中的没有PreNode和NextNode属性. 但它的集合属性中有一个IndexOf属性,从而能够找到它的前一个节点知后一个节点. TreeView中要么只有一个根节点:要么 ...
 - TreeView控件使用
		
treeView1.SelectedNode = treeView1.Nodes[0]; //选中当前treeview控件的根节点为当前节点添加子节点: TreeNode tmp; tmp = n ...
 - [转] C#2010 在TreeView控件下显示路径下所有文件和文件夹
		
原文 张丹-小桥流水,C#2010 在TreeView控件下显示路径下所有文件和文件夹 C#2010学习过程中有所收获,便总结下来,希望能给和我一样在学习遇到困难的同学提供参考. 本文主要介绍两个自定 ...
 - 基于Treeview控件遍历本地磁盘
		
一.前言 Treeview控件常用于遍历本地文件信息,通常与Datagridview与ImageList搭配.ImageList控件用于提供小图片给TreeView控件,DatagridView通常显 ...
 - Win32中TreeView控件的使用方法,类似于资源管理器中文件树形显示方式
		
首先是头文件,内容如下: #include <tchar.h> #include "..\CommonFiles\CmnHdr.h" #include <Wind ...
 - asp  TreeView控件的使用
		
相对于之前发过一个TreeView控件的使用方法 本次利用js操作,页面无刷新,性能提高 Css编码可能时我的模板页样式被继承下来,导致页面变乱,不需要的可以去掉 前台 <style> . ...
 
随机推荐
- Web应用程序系统的多用户权限控制设计及实现-首页模块【5】
			
首页模块就是展示不同权限的最终结果了,在阅读这章之前若有些不明白,可看看后续的单独的权限模块,用户模块,目录模块后从整体上再看首页模块. 阅读该模块需要一定或者是比较熟练的js知识,EasyUI Ta ...
 - 关于C语言中单双引号的问题
			
代码 #include<stdio.h> int main() { if ( "{" =='{' ) printf("True\n"); else ...
 - 解决springmvc中文件下载功能中使用javax.servlet.ServletOutputStream out = response.getOutputStream();后运行出异常但结果正确的问题
			
问题描述: 在springmvc中实现文件下载功能一般都会使用javax.servlet.ServletOutputStream out = response.getOutputStream();封装 ...
 - SSL协议详解
			
背景介绍 最近在看<密码学与网络安全>相关的书籍,这篇文章主要详细介绍一下著名的网络安全协议SSL. 在开始SSl介绍之前,先给大家介绍几个密码学的概念和相关的知识. 1.密 ...
 - 烂泥:KVM利用LVM快照快速部署虚拟机
			
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 上一篇文章介绍了有关KVM利用LVM快照备份和恢复虚拟机的功能,这篇文章我们来介绍,如何利用LVM快照功能为KVM快速部署虚拟机. 部署虚拟机需要以下几 ...
 - 百度地图秘钥ak的获取
			
今天打开网站的时候出现了这个问题“百度未授权使用地图API, 可能是因为您提供的密钥不是有效的百度开放平台密钥或此密钥未对本应用的百度地图JavasoriptAPI授权.....”经过研究终于知道什么 ...
 - How to use python remove the '^M' when copy words from Windows to Linux
			
今天帮同事用Python写了一个小工具,实现了在linux下批量文件名和去掉windows 文件到linux过程中产生^M的脚本,代码如下: !/opt/exptools/bin/python imp ...
 - Android中手机号、车牌号正则表达式
			
手机号 手机号的号段说明转载自:国内手机号码的正则表达式|蜗牛的积累 手机名称有GSM:表示只支持中国联通或者中国移动2G号段(130.131.132.134.135.136.137.138.139. ...
 - SQL 分页查询的四种方法
			
方法一 假设现在有这样的一张表: CREATE TABLE test ( id int primary key not null identity, names ) ) 然后向里面插入大约100条数据 ...
 - Cinder 调试 - cinder service 状态为 down
			
1. 问题 我们经常会发现某个cinder service 的状态为 down.比如下面例子中 controller 上的 cinder-scheduler 和 block1 节点上 cinder-v ...