C# Winform TreeView CheckBox 部分显示(代码)

public partial class  Form1 Form
{
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;
 
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#,checkbox,WinForm,treeview,部分显示)