TreeNode竟然没有HasChildren属性,ft!
今天写程序时遇到这个问题,找了msdn及参考这里:
http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B313134,花了2个小时才搞定这个问题,特此一记。
代码如下(vs2k5上调试通过):
using System.Runtime.InteropServices;
classTreeNodeHelper
{
TreeNodetreeNode=null;
publicTreeNodeHelper(TreeNodetr)
{
treeNode=tr;
}
/**////<summary>
///返回TreeNode是否有子结点
///</summary>
publicboolHasChildren
{
get
{
returnIsTreeNodeHasChildren(treeNode);
}
set
{
MakeTreeNodeHasChildren(treeNode,value);
}
}
publicconstUInt32TV_FIRST=4352;
publicconstUInt32TVSIL_NORMAL=0;
publicconstUInt32TVSIL_STATE=2;
publicconstUInt32TVM_SETIMAGELIST=TV_FIRST+9;
publicconstUInt32TVM_GETNEXTITEM=TV_FIRST+10;
publicconstUInt32TVIF_HANDLE=16;
publicconstUInt32TVIF_STATE=8;
publicconstUInt32TVIS_STATEIMAGEMASK=61440;
publicconstUInt32TVM_SETITEM=TV_FIRST+13;
publicconstUInt32TVM_GETITEM=TV_FIRST+12;
publicconstUInt32TVGN_ROOT=0;
publicconstintTVIF_CHILDREN=64;
//UseasequentialstructurelayouttodefineTVITEMfortheTreeView.
[StructLayout(LayoutKind.Sequential,Pack=8,CharSet=CharSet.Auto)]
publicstructTV_ITEM
{
publicuintmask;
publicIntPtrhItem;
publicuintstate;
publicuintstateMask;
publicIntPtrpszText;
publicintcchTextMax;
publicintiImage;
publicintiSelectedImage;
publicintcChildren;
publicIntPtrlParam;
}
//DeclaretwooverloadedSendMessagefunctions.The
//differenceisinthelastparameter:oneisByValandthe
//otherisByRef.
[DllImport("user32.dll")]
publicstaticexternUInt32SendMessage(IntPtrhWnd,UInt32Msg,
UInt32wParam,UInt32lParam);
[DllImport("User32",CharSet=CharSet.Auto)]
publicstaticexternIntPtrSendMessage(IntPtrhWnd,UInt32msg,
UInt32wParam,refTV_ITEMlParam);
/**////<summary>
///指定TreeNode是否有子结点(有子结点则TreeNode会显示+号,没有则不会显示)
///</summary>
///<paramname="tr"></param>
///<paramname="bHasChildren"></param>
publicstaticvoidMakeTreeNodeHasChildren(TreeNodetr,boolbHasChildren)
{
TV_ITEMtvItem=newTV_ITEM();
tvItem.mask=TVIF_CHILDREN|TVIF_HANDLE;
tvItem.hItem=tr.Handle;
tvItem.cChildren=bHasChildren?1:0;
SendMessage(tr.TreeView.Handle,TVM_SETITEM,0,reftvItem);
}
/**////<summary>
///返回树结点是否有子结点
///</summary>
///<paramname="?"></param>
///<returns></returns>
publicstaticboolIsTreeNodeHasChildren(TreeNodetr)
{
if(tr.Nodes.Count>0)
{
returntrue;
}
TV_ITEMtvItem=newTV_ITEM();
tvItem.mask=TVIF_CHILDREN|TVIF_HANDLE;
tvItem.hItem=tr.Handle;
SendMessage(tr.TreeView.Handle,TVM_GETITEM,0,reftvItem);
returntvItem.cChildren==1;
}
}
优质内容筛选与推荐>>
1、rabbitmq之window环境启动2、NET中的幕后英雄:MSCOREE.DLL (转载)3、Structured Streaming教程(2) —— 常用输入与输出4、SQLServer和java数据类型的对应关系5、要求
长按二维码向我转账
受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。