首页  编辑  

TreeView添加动态扩展的节点

Tags: /C#/界面处理/ListView_TreeView/   Date Created:

using System . Runtime . InteropServices;

public struct TVITEM

{

    public uint mask;

    public IntPtr hItem;

    public uint state;

    public uint stateMask;

    public string pszText;

    public int cchTextMax;

    public int iImage;

    public int iSelectedImage;

    public int cChildren;

    public int lParam;

}

[ DllImport ( "User32.DLL" )]

public static extern int SendMessage( IntPtr hWnd, uint Msg, int wParam, ref TVITEM iParam);

public const int TVIF_CHILDREN = 0x0040 ;

public const int TV_FIRST = 0x1100 ;

public const int TVM_SETITEM = TV_FIRST + 63 ;

private void button1_Click( object sender , EventArgs e)

{

    foreach ( TreeNode vTreeNodeL1 in treeView1 . Nodes)

   {

        foreach ( TreeNode vTreeNodeL2 in vTreeNodeL1 . Nodes)

       {

            TVITEM vTVITEM = new TVITEM ();

           vTVITEM . cChildren = 1 ;

           vTVITEM . hItem = vTreeNodeL2 . Handle;

           vTVITEM . mask = TVIF_CHILDREN;

           SendMessage(treeView1 . Handle, TVM_SETITEM, 0 , ref vTVITEM);

       }

   }

}

private void treeView1_BeforeExpand( object sender , TreeViewCancelEventArgs e)

{

    if ((e . Node . Level == 1 ) && (e . Node . Nodes . Count <= 0 ))

       e . Node . Nodes . Add( "Zswang );

}