TreeNodeStates TreeNodeStates TreeNodeStates TreeNodeStates Enum

定义

定义表示 TreeNode 的可能状态的常数。Defines constants that represent the possible states of a TreeNode.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class TreeNodeStates
[System.Flags]
public enum TreeNodeStates
type TreeNodeStates = 
Public Enum TreeNodeStates
继承
TreeNodeStatesTreeNodeStatesTreeNodeStatesTreeNodeStates
属性

字段

Checked Checked Checked Checked 8

已选中该节点。The node is checked.

Default Default Default Default 32

该节点处于其默认状态。The node is in its default state.

Focused Focused Focused Focused 16

该节点有焦点。The node has focus.

Grayed Grayed Grayed Grayed 2

该节点已禁用。The node is disabled.

Hot Hot Hot Hot 64

该节点为热节点。The node is hot. 此状态发生在 HotTracking 属性设置为 true 且鼠标指针位于该节点上时。This state occurs when the HotTracking property is set to true and the mouse pointer is over the node.

Indeterminate Indeterminate Indeterminate Indeterminate 256

该节点处于不确定状态。The node in an indeterminate state.

Marked Marked Marked Marked 128

该节点已标记。The node is marked.

Selected Selected Selected Selected 1

该节点已选择。The node is selected.

ShowKeyboardCues ShowKeyboardCues ShowKeyboardCues ShowKeyboardCues 512

该节点应指示一个键盘快捷键。The node should indicate a keyboard shortcut.

示例

下面的示例演示如何自定义TreeView使用所有者描述的控件。The following example demonstrates how to customize a TreeView control using owner drawing. TreeView在示例中的控件显示可选节点标记以及正常的节点标签。The TreeView control in the example displays optional node tags alongside the normal node labels. 使用指定节点标记TreeNode.Tag属性。Node tags are specified using the TreeNode.Tag property. TreeView控件还使用自定义颜色,包括自定义突出显示颜色。The TreeView control also uses custom colors, including a custom highlight color.

你可以自定义的大多数TreeView设置颜色的颜色属性,但不能作为属性的选择突出显示颜色。You can customize most of the TreeView colors by setting color properties, but the selection highlight color is not available as a property. 此外,默认选择突出显示矩形将扩展仅围绕节点标签。Additionally, the default selection highlight rectangle extends only around a node label. 若要绘制的节点标记并绘制自定义突出显示矩形足够大以容纳一个节点标记,则必须使用所有者描述。Owner drawing must be used to draw the node tags and to draw a customized highlight rectangle large enough to include a node tag.

在示例中,处理程序TreeView.DrawNode事件通过调用的方法绘制未选定的节点DrawTreeNodeEventArgs类。In the example, a handler for the TreeView.DrawNode event draws unselected nodes by calling methods of the DrawTreeNodeEventArgs class. 这些方法提供的默认外观TreeView不需要自定义的元素。These methods provide the default appearance for TreeView elements that do not need customization. 该处理程序手动绘制节点标记和自定义选择突出显示。The handler draws the node tags and the custom selection highlight manually.

有关完整示例,请参阅TreeView.DrawNode参考主题。For the complete example, see the TreeView.DrawNode 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 'myTreeView_DrawNode

注解

此枚举由State属性的DrawTreeNodeEventArgs类。This enumeration is used by the State property of the DrawTreeNodeEventArgs class. 有关详细信息,请参阅 TreeView.DrawNode 事件。For more information, see the TreeView.DrawNode event.

适用于

另请参阅