惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Martin Fowler
Martin Fowler
大猫的无限游戏
大猫的无限游戏
J
Java Code Geeks
罗磊的独立博客
雷峰网
雷峰网
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
爱范儿
爱范儿
B
Blog RSS Feed
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
D
Docker
Recent Announcements
Recent Announcements
T
Tailwind CSS Blog
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Vercel News
Vercel News
小众软件
小众软件
人人都是产品经理
人人都是产品经理
云风的 BLOG
云风的 BLOG
IT之家
IT之家
Blog — PlanetScale
Blog — PlanetScale
I
InfoQ
S
SegmentFault 最新的问题

博客园 - 楚潇

有关sqlserver的锁 基于dotnet2.0的联通sgip1.2协议二级网关源码 在vs2005中用gridview显示表中的image字段 - 楚潇 - 博客园 谁没在变! vs2k5 中asp.net "Web Site Administration Tool "使用中遇到的问题 - 楚潇 美达飞凡16X DVD起死回生记 寄语八十年代的新一代 小胜凭智, 大胜靠德 《windows核心编程》 再读<心航> visual C# 2005 express beta2读配置文件的问题(ms的bug?) C#中判断socket是否已断开的方法 将对象转为byte[] 摘自古龙的句子 端午节到了 C#中的字符串格式化 .net开发手机短信 怎样才能提高.net的水平呢? Reflaction很嚣张的功能
.net winform下TreeNode在没有子结点时也显示+号的解决办法
楚潇 · 2006-04-26 · via 博客园 - 楚潇

TreeNode竟然没有HasChildren属性,ft!

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

代码如下(vs2k5上调试通过):
using System.Runtime.InteropServices;

    class TreeNodeHelper
    
{
        TreeNode treeNode 
= null;
        
public TreeNodeHelper(TreeNode tr)
        
{
            treeNode 
= tr;
        }


        
/// <summary>
        
/// 返回TreeNode是否有子结点
        
/// </summary>

        public bool HasChildren
        
{
            
get
            
{
                
return IsTreeNodeHasChildren(treeNode);
            }

            
set
            
{
                MakeTreeNodeHasChildren(treeNode, value);
            }

        }


        
public const UInt32 TV_FIRST = 4352;
        
public const UInt32 TVSIL_NORMAL = 0;
        
public const UInt32 TVSIL_STATE = 2;
        
public const UInt32 TVM_SETIMAGELIST = TV_FIRST + 9;
        
public const UInt32 TVM_GETNEXTITEM = TV_FIRST + 10;
        
public const UInt32 TVIF_HANDLE = 16;
        
public const UInt32 TVIF_STATE = 8;
        
public const UInt32 TVIS_STATEIMAGEMASK = 61440;
        
public const UInt32 TVM_SETITEM = TV_FIRST + 13;
        
public const UInt32 TVM_GETITEM = TV_FIRST + 12;
        
public const UInt32 TVGN_ROOT = 0;
        
public const int TVIF_CHILDREN = 64;

        
// Use a sequential structure layout to define TVITEM for the TreeView.
        [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Auto)]
        
public struct TV_ITEM
        
{
            
public uint mask;
            
public IntPtr hItem;
            
public uint state;
            
public uint stateMask;
            
public IntPtr pszText;
            
public int cchTextMax;
            
public int iImage;
            
public int iSelectedImage;
            
public int cChildren;
            
public IntPtr lParam;
        }


        
// Declare two overloaded SendMessage functions. The
        
// difference is in the last parameter: one is ByVal and the
        
// other is ByRef.
        [DllImport("user32.dll")]
        
public static extern UInt32 SendMessage(IntPtr hWnd, UInt32 Msg,
            UInt32 wParam, UInt32 lParam);

        [DllImport(
"User32", CharSet = CharSet.Auto)]
        
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 msg,
            UInt32 wParam, 
ref TV_ITEM lParam);

        
/// <summary>
        
/// 指定TreeNode是否有子结点(有子结点则TreeNode会显示+号,没有则不会显示)
        
/// </summary>
        
/// <param name="tr"></param>
        
/// <param name="bHasChildren"></param>

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


        
/// <summary>
        
/// 返回树结点是否有子结点
        
/// </summary>
        
/// <param name="?"></param>
        
/// <returns></returns>

        public static bool IsTreeNodeHasChildren(TreeNode tr)
        
{
            
if (tr.Nodes.Count > 0)
            
{
                
return true;
            }

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

    }