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

推荐订阅源

Google DeepMind News
Google DeepMind News
Stack Overflow Blog
Stack Overflow Blog
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
T
The Blog of Author Tim Ferriss
博客园 - 叶小钗
N
Netflix TechBlog - Medium
腾讯CDC
C
Check Point Blog
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
GbyAI
GbyAI
S
SegmentFault 最新的问题
F
Fortinet All Blogs
美团技术团队
U
Unit 42
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
F
Full Disclosure
Recorded Future
Recorded Future
D
DataBreaches.Net
博客园 - 【当耐特】
Martin Fowler
Martin Fowler
J
Java Code Geeks
I
InfoQ
Y
Y Combinator Blog
A
About on SuperTechFans
AI
AI
爱范儿
爱范儿
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Forbes - Security
Forbes - Security
W
WeLiveSecurity
M
MIT News - Artificial intelligence
雷峰网
雷峰网
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Simon Willison's Weblog
Simon Willison's Weblog
Schneier on Security
Schneier on Security
The GitHub Blog
The GitHub Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
aimingoo的专栏
aimingoo的专栏
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
GRAHAM CLULEY
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
D
Docker
Recent Commits to openclaw:main
Recent Commits to openclaw:main
量子位
V2EX - 技术
V2EX - 技术
Project Zero
Project Zero

博客园 - 楚潇

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

    }