189 8069 5689

C#WinformTreeViewCheckBox部分显示(代码)

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);
///
///Hides the checkbox for the specified node on a TreeView control. 
///
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#WinformTreeViewCheckBox部分显示(代码)
标题路径:http://cdxtjz.com/article/gcjscs.html

其他资讯