.net winform下TreeNode在没有子结点时也显示+号的解决办法

TreeNode竟然没有HasChildren属性,ft!

今天写程序时遇到这个问题,找了msdn及参考这里: http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B313134,花了2个小时才搞定这个问题,特此一记。

代码如下(vs2k5上调试通过):
using System.Runtime.InteropServices;
None.gif      class  TreeNodeHelper
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        TreeNode treeNode 
= null;
InBlock.gif        
public TreeNodeHelper(TreeNode tr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            treeNode 
= tr;
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回TreeNode是否有子结点
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public bool HasChildren
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return IsTreeNodeHasChildren(treeNode);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                MakeTreeNodeHasChildren(treeNode, value);
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public const UInt32 TV_FIRST = 4352;
InBlock.gif        
public const UInt32 TVSIL_NORMAL = 0;
InBlock.gif        
public const UInt32 TVSIL_STATE = 2;
InBlock.gif        
public const UInt32 TVM_SETIMAGELIST = TV_FIRST + 9;
InBlock.gif        
public const UInt32 TVM_GETNEXTITEM = TV_FIRST + 10;
InBlock.gif        
public const UInt32 TVIF_HANDLE = 16;
InBlock.gif        
public const UInt32 TVIF_STATE = 8;
InBlock.gif        
public const UInt32 TVIS_STATEIMAGEMASK = 61440;
InBlock.gif        
public const UInt32 TVM_SETITEM = TV_FIRST + 13;
InBlock.gif        
public const UInt32 TVM_GETITEM = TV_FIRST + 12;
InBlock.gif        
public const UInt32 TVGN_ROOT = 0;
InBlock.gif        
public const int TVIF_CHILDREN = 64;
InBlock.gif
InBlock.gif        
// Use a sequential structure layout to define TVITEM for the TreeView.
InBlock.gif
        [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
InBlock.gif        
public struct TV_ITEM
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public uint mask;
InBlock.gif            
public IntPtr hItem;
InBlock.gif            
public uint state;
InBlock.gif            
public uint stateMask;
InBlock.gif            
public IntPtr pszText;
InBlock.gif            
public int cchTextMax;
InBlock.gif            
public int iImage;
InBlock.gif            
public int iSelectedImage;
InBlock.gif            
public int cChildren;
InBlock.gif            
public IntPtr lParam;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// Declare two overloaded SendMessage functions. The
InBlock.gif        
// difference is in the last parameter: one is ByVal and the
InBlock.gif        
// other is ByRef.
InBlock.gif
        [DllImport("user32.dll")]
InBlock.gif        
public static extern UInt32 SendMessage(IntPtr hWnd, UInt32 Msg,
InBlock.gif            UInt32 wParam, UInt32 lParam);
InBlock.gif
InBlock.gif        [DllImport(
"User32", CharSet = CharSet.Auto)]
InBlock.gif        
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg,
InBlock.gif            UInt32 wParam, 
ref TV_ITEM lParam);
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 指定TreeNode是否有子结点(有子结点则TreeNode会显示+号,没有则不会显示)
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="tr"></param>
ExpandedSubBlockEnd.gif        
/// <param name="bHasChildren"></param>

InBlock.gif        public static void MakeTreeNodeHasChildren(TreeNode tr, bool bHasChildren)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TV_ITEM tvItem 
= new TV_ITEM();
InBlock.gif            tvItem.mask 
= TVIF_CHILDREN | TVIF_HANDLE;
InBlock.gif            tvItem.hItem 
= tr.Handle;
InBlock.gif            tvItem.cChildren 
= bHasChildren ? 1 : 0;
InBlock.gif            SendMessage(tr.TreeView.Handle, TVM_SETITEM, 
0ref tvItem);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 返回树结点是否有子结点
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="?"></param>
ExpandedSubBlockEnd.gif        
/// <returns></returns>

InBlock.gif        public static bool IsTreeNodeHasChildren(TreeNode tr)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (tr.Nodes.Count > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            TV_ITEM tvItem 
= new TV_ITEM();
InBlock.gif            tvItem.mask 
= TVIF_CHILDREN | TVIF_HANDLE;
InBlock.gif            tvItem.hItem 
= tr.Handle;
InBlock.gif            SendMessage(tr.TreeView.Handle, TVM_GETITEM, 
0ref tvItem);
InBlock.gif            
return tvItem.cChildren == 1;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

转载于:https://www.cnblogs.com/JLL/archive/2006/04/26/385591.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值