DrawTreeNodeEventArgs.Node Propiedad

Definición

Obtiene el TreeNode que se va a dibujar.Gets the TreeNode to draw.

public:
 property System::Windows::Forms::TreeNode ^ Node { System::Windows::Forms::TreeNode ^ get(); };
public System.Windows.Forms.TreeNode Node { get; }
member this.Node : System.Windows.Forms.TreeNode
Public ReadOnly Property Node As TreeNode

Valor de propiedad

TreeNode que se va a dibujar.The TreeNode to draw.

Ejemplos

En el ejemplo de código siguiente se muestra cómo TreeView personalizar un control mediante el dibujo del propietario.The following code example demonstrates how to customize a TreeView control using owner drawing. El TreeView control en el ejemplo muestra etiquetas de nodo opcionales junto con las etiquetas de nodo estándar.The TreeView control in the example displays optional node tags alongside the standard node labels. Las etiquetas de nodo se especifican mediante la TreeNode.Tag propiedad.Node tags are specified using the TreeNode.Tag property. El TreeView control también utiliza colores personalizados, incluido un color de resaltado personalizado.The TreeView control also uses custom colors, including a custom highlight color.

Puede personalizar la mayoría de los TreeView colores estableciendo las propiedades de color, pero el color de resaltado de la selección no está disponible como una propiedad.You can customize most of the TreeView colors by setting color properties, but the selection highlight color is not available as a property. Además, el rectángulo de resaltado de selección predeterminado solo se extiende alrededor de una etiqueta de nodo.Additionally, the default selection highlight rectangle extends only around a node label. El dibujo del propietario debe utilizarse para dibujar las etiquetas de nodo y para dibujar un rectángulo de resaltado personalizado lo suficientemente grande como para incluir una etiqueta de nodo.Owner drawing must be used to draw the node tags and to draw a customized highlight rectangle large enough to include a node tag.

En el ejemplo, un controlador para el TreeView.DrawNode evento dibuja las etiquetas del nodo y el resaltado de la selección personalizada de forma manual.In the example, a handler for the TreeView.DrawNode event draws the node tags and the custom selection highlight manually. Los nodos no seleccionados no necesitan personalización.Unselected nodes do not need customization. Para ello, la DrawDefault propiedad se establece en true para que el sistema operativo lo dibuje.For these, the DrawDefault property is set to true so that they will be drawn by the operating system.

Para obtener el ejemplo completo, vea DrawTreeNodeEventArgs el tema de referencia de información general.For the complete example, see the DrawTreeNodeEventArgs overview reference topic.

   // Draws a node.
private:
   void myTreeView_DrawNode( Object^ sender, DrawTreeNodeEventArgs^ e )
   {
      // Draw the background and node text for a selected node.
      if ( (e->State & TreeNodeStates::Selected) != (TreeNodeStates)0 )
      {
         // Draw the background of the selected node. The NodeBounds
         // method makes the highlight rectangle large enough to
         // include the text of a node tag, if one is present.
         e->Graphics->FillRectangle( Brushes::Green, NodeBounds( e->Node ) );

         // Retrieve the node font. If the node font has not been set,
         // use the TreeView font.
         System::Drawing::Font^ nodeFont = e->Node->NodeFont;
         if ( nodeFont == nullptr )
                  nodeFont = (dynamic_cast<TreeView^>(sender))->Font;

         // Draw the node text.
         e->Graphics->DrawString( e->Node->Text, nodeFont, Brushes::White, Rectangle::Inflate( e->Bounds, 2, 0 ) );
      }
      // Use the default background and node text.
      else
      {
         e->DrawDefault = true;
      }

      // If a node tag is present, draw its string representation 
      // to the right of the label text.
      if ( e->Node->Tag != nullptr )
      {
         e->Graphics->DrawString( e->Node->Tag->ToString(), tagFont, Brushes::Yellow, (float)e->Bounds.Right + 2, (float)e->Bounds.Top );
      }

      
      // If the node has focus, draw the focus rectangle large, making
      // it large enough to include the text of the node tag, if present.
      if ( (e->State & TreeNodeStates::Focused) != (TreeNodeStates)0 )
      {
         Pen^ focusPen = gcnew Pen( Color::Black );
         try
         {
            focusPen->DashStyle = System::Drawing::Drawing2D::DashStyle::Dot;
            Rectangle focusBounds = NodeBounds( e->Node );
            focusBounds.Size = System::Drawing::Size( focusBounds.Width - 1, focusBounds.Height - 1 );
            e->Graphics->DrawRectangle( focusPen, focusBounds );
         }
         finally
         {
            if ( focusPen )
               delete safe_cast<IDisposable^>(focusPen);
         }

      }
   }
// Draws a node.
private void myTreeView_DrawNode(
    object sender, DrawTreeNodeEventArgs e)
{
    // Draw the background and node text for a selected node.
    if ((e.State & TreeNodeStates.Selected) != 0)
    {
        // Draw the background of the selected node. The NodeBounds
        // method makes the highlight rectangle large enough to
        // include the text of a node tag, if one is present.
        e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node));

        // Retrieve the node font. If the node font has not been set,
        // use the TreeView font.
        Font nodeFont = e.Node.NodeFont;
        if (nodeFont == null) nodeFont = ((TreeView)sender).Font;

        // Draw the node text.
        e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White,
            Rectangle.Inflate(e.Bounds, 2, 0));
    }

    // Use the default background and node text.
    else 
    {
        e.DrawDefault = true;
    }

    // If a node tag is present, draw its string representation 
    // to the right of the label text.
    if (e.Node.Tag != null)
    {
        e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont,
            Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top);
    }

    // If the node has focus, draw the focus rectangle large, making
    // it large enough to include the text of the node tag, if present.
    if ((e.State & TreeNodeStates.Focused) != 0)
    {
        using (Pen focusPen = new Pen(Color.Black))
        {
            focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            Rectangle focusBounds = NodeBounds(e.Node);
            focusBounds.Size = new Size(focusBounds.Width - 1, 
            focusBounds.Height - 1);
            e.Graphics.DrawRectangle(focusPen, focusBounds);
        }
    }
}
' Draws a node.
Private Sub myTreeView_DrawNode(ByVal sender As Object, _
    ByVal e As DrawTreeNodeEventArgs) Handles myTreeView.DrawNode

    ' Draw the background and node text for a selected node.
    If (e.State And TreeNodeStates.Selected) <> 0 Then

        ' Draw the background of the selected node. The NodeBounds
        ' method makes the highlight rectangle large enough to
        ' include the text of a node tag, if one is present.
        e.Graphics.FillRectangle(Brushes.Green, NodeBounds(e.Node))

        ' Retrieve the node font. If the node font has not been set,
        ' use the TreeView font.
        Dim nodeFont As Font = e.Node.NodeFont
        If nodeFont Is Nothing Then
            nodeFont = CType(sender, TreeView).Font
        End If

        ' Draw the node text.
        e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, _
            e.Bounds.Left - 2, e.Bounds.Top)

    ' Use the default background and node text.
    Else
        e.DrawDefault = True
    End If

    ' If a node tag is present, draw its string representation 
    ' to the right of the label text.
    If (e.Node.Tag IsNot Nothing) Then
        e.Graphics.DrawString(e.Node.Tag.ToString(), tagFont, _
            Brushes.Yellow, e.Bounds.Right + 2, e.Bounds.Top)
    End If

    ' If the node has focus, draw the focus rectangle large, making
    ' it large enough to include the text of the node tag, if present.
    If (e.State And TreeNodeStates.Focused) <> 0 Then
        Dim focusPen As New Pen(Color.Black)
        Try
            focusPen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot
            Dim focusBounds As Rectangle = NodeBounds(e.Node)
            focusBounds.Size = New Size(focusBounds.Width - 1, _
                focusBounds.Height - 1)
            e.Graphics.DrawRectangle(focusPen, focusBounds)
        Finally
            focusPen.Dispose()
        End Try
    End If

End Sub

Comentarios

Utilice esta propiedad para tener acceso TreeNode al objeto que se va a dibujar.Use this property to access the TreeNode object to draw. Esto resulta útil cuando la State propiedad no proporciona información adecuada para satisfacer sus necesidades.This is useful when the State property does not provide adequate information to meet your needs. La State propiedad proporciona solo información de estado básica que puede usar, por ejemplo, para determinar si un nodo está seleccionado, comprobado o enfocado.The State property provides only basic state information that you can use, for example, to determine whether a node is selected, checked, or focused. Por Node otro lado, la propiedad permite tener acceso a todos los miembros TreeNode del objeto.The Node property, on the other hand, allows you to access all members of the TreeNode object. Debe tener acceso directamente al nodo, por ejemplo, cuando desee determinar su estado de expansión.You must access the node directly, for example, when you want to determine its expansion state.

Se aplica a

Consulte también: