DrawTreeNodeEventArgs.Bounds Propiedad

Definición

Obtiene el tamaño y la ubicación del control TreeNode que se va a dibujar.Gets the size and location of the TreeNode to draw.

public:
 property System::Drawing::Rectangle Bounds { System::Drawing::Rectangle get(); };
public System.Drawing.Rectangle Bounds { get; }
member this.Bounds : System.Drawing.Rectangle
Public ReadOnly Property Bounds As Rectangle

Valor de propiedad

Estructura Rectangle que representa los límites del control TreeNode que se va a dibujar.A Rectangle that represents the bounds of 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

La ubicación especificada por esta propiedad es relativa a la esquina superior izquierda del TreeView control.The location specified by this property is relative to the upper-left corner of the TreeView control.

Cuando la TreeView.DrawMode propiedad está establecida en OwnerDrawText, el área especificada por la Bounds propiedad es la región en la que se puede hacer clic en un nodo para seleccionarla.When the TreeView.DrawMode property is set to OwnerDrawText, the area specified by the Bounds property is the region in which a node can be clicked to select it. Si dibuja fuera de esta región de prueba de posicionamiento, debe proporcionar su propio código para seleccionar un nodo cuando se haga clic en su área visible.If you draw outside this hit test region, you should provide your own code for selecting a node when its visible area is clicked. También puede que desee proporcionar su propio código de prueba de posicionamiento cuando TreeView.DrawMode la propiedad está establecida OwnerDrawAllen.You may also want to provide your own hit test code when the TreeView.DrawMode property is set to OwnerDrawAll. En este modo, la Bounds propiedad engloba todo el nodo en todo el ancho TreeView del control, en lugar de la región de la prueba de posicionamiento.In this mode, the Bounds property encompasses the whole node across the entire width of the TreeView control, rather than the just hit test region.

Se aplica a

Consulte también: