customized checkbox in child node and no checkbox in parent node in a treeview
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
namespace WindowsApplication2
{
public partial class Form2 : Form
{
public const int TVIF_STATE = 0x8;
public const int TVIS_STATEIMAGEMASK = 0xF000;
public const int TV_FIRST = 0x1100;
public const int TVM_SETITEM = TV_FIRST + 63;
[StructLayout(LayoutKind.Sequential)]
public 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")]
public static extern int SendMessage (IntPtr hwnd, int msg, IntPtr wParam, ref TVITEM lParam);
private void TreeNode_SetStateImageIndex(TreeNode node, int index)
{
TVITEM tvi = new TVITEM();
tvi.hItem = node.Handle;
tvi.mask = TVIF_STATE;
tvi.stateMask = TVIS_STATEIMAGEMASK;
tvi.state = index << 12;
SendMessage(node.TreeView.Handle, TVM_SETITEM, IntPtr.Zero, ref tvi);
}
public Form2()
{
InitializeComponent();
this.treeView1.CheckBoxes = true;
this.treeView1.ShowLines = true;
this.treeView1.ShowPlusMinus = true;
this.treeView1.ShowNodeToolTips = true;
this.treeView1.ShowRootLines = true;
treeView1.ItemHeight = 25;
this.treeView1.DrawMode = System.Windows.Forms.TreeViewDrawMode.OwnerDrawAll;
this.treeView1.DrawNode += new System.Windows.Forms.DrawTreeNodeEventHandler(this.treeView_DrawNode);
for (int i = 0; i < 3; i++)
{
this.treeView1.Nodes.Add(string.Format("First level {0}", i));
for (int j = 0; j < 2; j++)
{
this.treeView1.Nodes[i].Nodes.Add(string.Format("Second level {0}", j));
for (int k = 0; k < 2; k++)
{
this.treeView1.Nodes[i].Nodes[j].Nodes.Add(string.Format("Third level {0}", k));
}
}
}
}
private void treeView_DrawNode(object sender, DrawTreeNodeEventArgs e)
{
Color backColor, foreColor;
if ((e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected)
{
backColor = SystemColors.Highlight;
foreColor = SystemColors.HighlightText;
}
else if ((e.State & TreeNodeStates.Hot) == TreeNodeStates.Hot)
{
backColor = SystemColors.HotTrack;
foreColor = SystemColors.HighlightText;
}
else
{
backColor = e.Node.BackColor;
foreColor = e.Node.ForeColor;
}
using (SolidBrush brush = new SolidBrush(backColor))
{
e.Graphics.FillRectangle(brush, e.Node.Bounds);
//e.Graphics.FillPath(brush, GraphicsPath p);
}
TextRenderer.DrawText(e.Graphics, e.Node.Text, this.treeView1.Font, e.Node.Bounds, foreColor, backColor);
if ((e.State & TreeNodeStates.Focused) == TreeNodeStates.Focused)
{
ControlPaint.DrawFocusRectangle(e.Graphics, e.Node.Bounds, foreColor, backColor);
}
if (HasParent(e.Node) == false)
{
TreeNode_SetStateImageIndex(e.Node, 0);
}
e.DrawDefault = true;
}
private static bool HasChild(TreeNode node)
{
int nodeCount = 0;
foreach (TreeNode x in node.Nodes)
nodeCount++;
if (nodeCount != 0)
return true;
else
return false;
}
private static bool HasParent(TreeNode node)
{
if (node.Parent == null)
return false;
else
return true;
}
}
}

浙公网安备 33010602011771号