TreeNode.FromHandle(TreeView, IntPtr) 方法

定义

返回带有指定句柄并分配给指定树视图控件的树节点。

public:
 static System::Windows::Forms::TreeNode ^ FromHandle(System::Windows::Forms::TreeView ^ tree, IntPtr handle);
public static System.Windows.Forms.TreeNode FromHandle (System.Windows.Forms.TreeView tree, IntPtr handle);
static member FromHandle : System.Windows.Forms.TreeView * nativeint -> System.Windows.Forms.TreeNode
Public Shared Function FromHandle (tree As TreeView, handle As IntPtr) As TreeNode

参数

tree
TreeView

包含树节点的 TreeView

handle
IntPtr

nativeint

树节点的句柄。

返回

TreeNode

TreeNode,表示分配给指定的 TreeView 控件并带有指定句柄的树节点。

示例

下面的代码示例获取 TreeNode 已折叠并使用其 Handle 属性创建副本。 原始副本 TreeNode 将从该 TreeNodeCollection集合中删除,并将副本添加到集合中。 此示例要求你具有FormTreeView控件。 控件 TreeView 应具有两个或多个根节点,每个节点至少有一个子节点。

private:
   void treeView1_AfterCollapse( Object^ /*sender*/, TreeViewEventArgs^ e )
   {
      // Create a copy of the e.Node from its Handle.
      TreeNode^ tn = TreeNode::FromHandle( e->Node->TreeView, e->Node->Handle );
      tn->Text = String::Concat( tn->Text, "Copy" );

      // Remove the e.Node so it can be replaced with tn.
      e->Node->Remove();

      // Add tn to the TreeNodeCollection.
      treeView1->Nodes->Add( tn );
   }
private void treeView1_AfterCollapse(object sender, TreeViewEventArgs e)
{
   // Create a copy of the e.Node from its Handle.
   TreeNode tn = TreeNode.FromHandle(e.Node.TreeView, e.Node.Handle);
   tn.Text += "Copy";
   // Remove the e.Node so it can be replaced with tn.
   e.Node.Remove();
   // Add tn to the TreeNodeCollection.
   treeView1.Nodes.Add(tn);
}
Private Sub treeView1_AfterCollapse(sender As Object, _
  e As TreeViewEventArgs) Handles treeView1.AfterCollapse
   ' Create a copy of the e.Node from its Handle.
   Dim tn As TreeNode = TreeNode.FromHandle(e.Node.TreeView, e.Node.Handle)
   tn.Text += "Copy"
   ' Remove the e.Node so it can be replaced with tn.
   e.Node.Remove()
   ' Add tn to the TreeNodeCollection.
   treeView1.Nodes.Add(tn)
End Sub

注解

将节点添加到 TreeView.Nodes 集合时,控件 TreeView 会在节点句柄可用之前发送NM_CUSTOMDRAW通知。 如果重写 WndProc 为响应此通知而提供自定义绘图的方法 TreeView ,则应始终在尝试访问节点之前检查此方法 null 的返回值。

适用于

另请参阅