温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-07-25 18:02:14 来源:网络 阅读:2414 作者:lqtc0722 栏目:编程语言
public partial class Form1Form
{
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);
}
}
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI