DrawTreeNodeEventArgs.State Eigenschaft

Definition

Ruft den aktuellen Zustand des zu zeichnenden TreeNode ab.Gets the current state of the TreeNode to draw.

public:
 property System::Windows::Forms::TreeNodeStates State { System::Windows::Forms::TreeNodeStates get(); };
public System.Windows.Forms.TreeNodeStates State { get; }
member this.State : System.Windows.Forms.TreeNodeStates
Public ReadOnly Property State As TreeNodeStates

Eigenschaftswert

TreeNodeStates

Eine bitweise Kombination der TreeNodeStates-Werte, die den aktuellen Zustand des TreeNode angibt.A bitwise combination of the TreeNodeStates values indicating the current state of the TreeNode.

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie ein TreeView Steuerelement mithilfe der Besitzer Zeichnung angepasst wird.The following code example demonstrates how to customize a TreeView control using owner drawing. Das- TreeView Steuerelement im Beispiel zeigt optionale Knoten Tags neben den Standard Knoten Bezeichnungen an.The TreeView control in the example displays optional node tags alongside the standard node labels. Knoten Tags werden mithilfe der- TreeNode.Tag Eigenschaft angegeben.Node tags are specified using the TreeNode.Tag property. Das- TreeView Steuerelement verwendet auch benutzerdefinierte Farben, einschließlich einer benutzerdefinierten Hervorhebungs Farbe.The TreeView control also uses custom colors, including a custom highlight color.

Sie können die meisten Farben anpassen TreeView , indem Sie Farbeigenschaften festlegen, aber die Auswahl Hervorhebungs Farbe ist nicht als Eigenschaft verfügbar.You can customize most of the TreeView colors by setting color properties, but the selection highlight color is not available as a property. Außerdem erweitert das standardmäßige Auswahl Rechteck für die Auswahl nur die Knoten Bezeichnung.Additionally, the default selection highlight rectangle extends only around a node label. Besitzer Zeichnung muss zum Zeichnen der Knoten Tags und zum Zeichnen eines angepassten Hervorhebungs Rechtecks verwendet werden, das groß genug ist, um ein Knotentag einzuschließen.Owner drawing must be used to draw the node tags and to draw a customized highlight rectangle large enough to include a node tag.

Im Beispiel zeichnet ein Handler für das TreeView.DrawNode -Ereignis die Knoten Tags und die benutzerdefinierte Auswahl Markierung manuell.In the example, a handler for the TreeView.DrawNode event draws the node tags and the custom selection highlight manually. Nicht ausgewählte Knoten müssen nicht angepasst werden.Unselected nodes do not need customization. Hierzu wird die- DrawDefault Eigenschaft auf festgelegt, true sodass Sie vom Betriebssystem gezeichnet werden.For these, the DrawDefault property is set to true so that they will be drawn by the operating system.

Das komplette Beispiel finden Sie im DrawTreeNodeEventArgs Übersichts Referenz Thema.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

Hinweise

Verwenden Sie diese Eigenschaft, um zu überprüfen, ob TreeNode sich das zu zeichnende Objekt in einem bestimmten Zustand befindet.Use this property to check whether the TreeNode object to draw is in a particular state. Diese Eigenschaft stellt nur grundlegende Zustandsinformationen zum Knoten bereit.This property provides only basic state information about the node. Sie können diese Eigenschaft z. b. verwenden, um zu bestimmen, ob ein Knoten ausgewählt, aktiviert oder fokussiert ist.You can use this property, for example, to determine whether a node is selected, checked, or focused. Wenn Sie mehr wissen müssen, rufen Sie den Knoten über die- Node Eigenschaft ab.If you need to know more, retrieve the node through the Node property. Wenn Sie z. b. ermitteln möchten, ob der Knoten erweitert ist, müssen Sie den Knoten abrufen und die-Eigenschaft überprüfen TreeNode.IsExpanded .For example, to determine whether the node is expanded, you must retrieve the node and check the TreeNode.IsExpanded property.

Gilt für:

Weitere Informationen