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

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 楚潇

有关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;
        }

    }