给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> . ...
随机推荐
- Python基础(2)--对象类型
Python使用对象模型来存储数据.构造任何类型的值都是一个对象 所有的Python对象都拥有三个特性:身份.类型.值 身份: 每一个对象都有一个唯一的身份来标志自己,任何对象的身份可以使用内建函数i ...
- NSArray,NSMutableArray的三种排序
int main(int argc, const char * argv[]) { @autoreleasepool { //字符串进行排序 NSArray *arr=@[@"b" ...
- Mac OS X 10.10 Yosemite下面解决XAMPP无法开启mysql的问题
首次使用XAMPP但是却遇到一个MySQL无法启动的缘故,不知道怎么解决,经过百度 找到解决方案: 在xampp安装目录下找到xamp这个文件(默认路径是:/Applications/XAMPP/xa ...
- 响应式Web设计 – 布局
写在前面 去年上半年,我开始着手推动项目中响应式设计的落地.以官网优化需求为契机,主动去做了响应式的页面设计,也说服了产品.设计和开发的相关同事一起把它上线落实,但不幸的是,由于各种方面的原因,比如, ...
- mybatis3.3 + struts2.3.24 + mysql5.1.22开发环境搭建及相关说明
一.新建Web工程,并在lib目录下添加jar包 主要jar包:struts2相关包,mybatis3.3相关包,mysql-connector-java-5.1.22-bin.jar, gson-2 ...
- cocos2d-x图层以及显示对象的基本使用
LogoNode: #ifndef LogoNode_hpp #define LogoNode_hpp #include <stdio.h> #include "cocos2d. ...
- which,whereis, locate, find
which 在PATH环境变量中的路径中查找目标文件,所以用来查找都是可执行文件,Linux下的各种命令本质上就是一个可执行的文件,所以我们安装新的软件之后通常都会有相应的命令将其打开,就是因为安装的 ...
- hyper-v无线网络上外网
这个通过无线网络上外网也是找了很多文章,大部分写的都不详细,没有办法成功,原理就是创建一个虚拟网卡,然后把创建的虚拟网卡和无线网卡桥接,虚拟机中使用创建的虚拟网卡,这里创建的虚拟网卡指的是用hyper ...
- Linux系统升级更新openssh 7.3p1
放在最前面:鉴于网上爬虫猖獗,博客被盗时有发生,这里需要来个链接,大家请认准来自博客园的Scoter:http://www.cnblogs.com/scoter2008,本文将持续更新 最近绿盟给扫描 ...
- Unity3d内置浏览器
uWebKit是一个Unity3d插件,个人认为比较强大,值得收藏啊 有图有真相: 安装以及破解说明: 1.导入资源包 2.将破解目录里的Editor复制到工程项目的Assets目录下进行覆盖 3.打 ...