Actualización: noviembre 2007
Define constantes que representan los posibles estados de TreeNode.
Esta enumeración tiene un atributo FlagsAttribute que permite una combinación bit a bit de los valores de miembro.
Espacio de nombres: System.Windows.FormsEnsamblado: System.Windows.Forms (en System.Windows.Forms.dll)
<FlagsAttribute> _
Public Enumeration TreeNodeStates
Dim instance As TreeNodeStates
[FlagsAttribute] public enum TreeNodeStates
[FlagsAttribute] public enum class TreeNodeStates
/** @attribute FlagsAttribute */ public enum TreeNodeStates
public enum TreeNodeStates
| Nombre de miembro | Descripción | |
|---|---|---|
| Checked | El nodo está activado. | |
| Default | El nodo está en su estado predeterminado. | |
| Focused | El nodo tiene foco. | |
| Grayed | El nodo está deshabilitado. | |
| Hot | El nodo está caliente. Este estado aparece cuando la propiedad TreeView.HotTracking se establece en true y el puntero del mouse está encima del nodo. | |
| Indeterminate | Nodo en un estado indeterminado. | |
| Marked | El nodo está marcado. | |
| Selected | El nodo está seleccionado. | |
| ShowKeyboardCues | El nodo debe indicar un método abreviado de teclado. |
La propiedad State de la clase DrawTreeNodeEventArgs utiliza esta enumeración. Para obtener más información, vea el evento TreeView.DrawNode.
En el ejemplo siguiente se muestra cómo se personaliza un control TreeView mediante el dibujo del propietario. El control TreeView del ejemplo muestra etiquetas de nodo opcionales junto a las etiquetas de nodo normales. Las etiquetas de nodo se especifican utilizando la propiedad TreeNode.Tag. El control TreeView también utiliza colores personalizados, incluido un color de resaltado personalizado.
Puede personalizar la mayoría de los colores de TreeView estableciendo las propiedades de los colores, pero el color de resaltado de una selección no está disponible como propiedad. Además, el rectángulo de resalte de la selección predeterminado sólo se extiende alrededor de una etiqueta de nodo. Debe utilizarse la técnica de dibujo del propietario para dibujar las etiquetas de nodo así como un rectángulo de resalte personalizado suficientemente grande para incluir una etiqueta de nodo.
En el ejemplo, un controlador del evento TreeView.DrawNode dibuja los nodos no seleccionados llamando a los métodos de la clase DrawTreeNodeEventArgs. Estos métodos proporcionan un aspecto predeterminado a los elementos TreeView que no necesitan personalización. El controlador dibuja las etiquetas de nodo y la selección personalizada resalta manualmente.
Para obtener el ejemplo completo, vea el tema referente a TreeView.DrawNode.
' 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 'myTreeView_DrawNode
// 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 void myTreeView_DrawNode(Object sender, DrawTreeNodeEventArgs e)
{
// Draw the background and node text for a selected node.
if ((int)(e.get_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.get_Graphics().FillRectangle(Brushes.get_Green(),
NodeBounds(e.get_Node()));
// Retrieve the node font. If the node font has not been set,
// use the TreeView font.
Font nodeFont = e.get_Node().get_NodeFont();
if (nodeFont == null) {
nodeFont = ((TreeView)sender).get_Font();
}
// Draw the node text.
e.get_Graphics().DrawString(e.get_Node().get_Text(),
nodeFont, Brushes.get_White(),
RectangleF.op_Implicit(Rectangle.Inflate(e.get_Bounds(), 2, 0)));
}
// Use the default background and node text.
else {
e.set_DrawDefault(true);
}
// If a node tag is present, draw its string representation
// to the right of the label text.
if (e.get_Node().get_Tag() != null) {
e.get_Graphics().DrawString(e.get_Node().get_Tag().ToString(),
tagFont, Brushes.get_Yellow(), e.get_Bounds().get_Right() + 2,
e.get_Bounds().get_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 ((int)(e.get_State() & TreeNodeStates.Focused) != 0) {
Pen focusPen = new Pen(Color.get_Black());
try {
focusPen.set_DashStyle(System.Drawing.Drawing2D.DashStyle.Dot);
Rectangle focusBounds = NodeBounds(e.get_Node());
focusBounds.set_Size(new Size(focusBounds.get_Width() - 1,
focusBounds.get_Height() - 1));
e.get_Graphics().DrawRectangle(focusPen, focusBounds);
}
finally {
focusPen.Dispose();
}
}
} //myTreeView_DrawNode
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.