C# Winform TreeView CheckBox 部分显示(代码)
原创
©著作权归作者所有:来自51CTO博客作者lqtc0722的原创作品,请联系作者获取转载授权,否则将追究法律责任
public partial class Form1: Form
{
private const intTVIF_STATE = 0x8;
private const intTVIS_STATEIMAGEMASK = 0xF000;
private const intTV_FIRST = 0x1100;
private const intTVM_SETITEM = TV_FIRST + 63;
public Form1()
{
InitializeComponent();
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);
for(int i = 0; i < 10;++i)
{
this.treeView1.Nodes.Add(string.Format("First level {0}", i));
for(int j = 0; j < 5;j++)
{
this.treeView1.Nodes[i].Nodes.Add(string.Format("Second level {0}", j));
for(int k = 0; k < 5;k++)
{
this.treeView1.Nodes[i].Nodes[j].Nodes.Add(string.Format("Thirdlevel {0}", k));
}
}
}
this.treeView1.ExpandAll();
}
Private void treeView_DrawNode(object sender, DrawTreeNodeEventArgse)
{
if(e.Node.Level == 1)
HideCheckBox(this.treeView1,e.Node);
e.DrawDefault = true;
}
[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);
///<summary>
///Hides the checkbox for the specified node on a TreeView control.
///</summary>
Private void HideCheckBox(TreeView tvw, TreeNodenode)
{
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);
}
}
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C# treeview控件部分节点添加checkbox
eeview 二、实现委托事件 三、隐藏的实现
ide 初始化 javascript 委托事件