TreeNodeStates 枚举

定义

定义表示 TreeNode 的可能状态的常数。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum TreeNodeStates
继承
TreeNodeStates
属性

字段

名称 说明
Checked 8

已选中该节点。

Default 32

该节点处于其默认状态。

Focused 16

该节点有焦点。

Grayed 2

该节点已禁用。

Hot 64

该节点为热节点。 此状态发生在 HotTracking 属性设置为 true 且鼠标指针位于该节点上时。

Indeterminate 256

该节点处于不确定状态。

Marked 128

该节点已标记。

Selected 1

该节点已选择。

ShowKeyboardCues 512

该节点应指示一个键盘快捷键。

示例

下面的示例演示如何使用所有者绘图自定义 TreeView 控件。 示例中 TreeView 的 控件在普通节点标签旁边显示可选节点标记。 使用 TreeNode.Tag 属性指定节点标记。 控件 TreeView 还使用自定义颜色,包括自定义突出显示颜色。

可以通过设置颜色属性来自定义大多数 TreeView 颜色,但选择突出显示颜色不能作为属性使用。 此外,默认选择突出显示矩形仅围绕节点标签扩展。 必须使用所有者绘图来绘制节点标记,并绘制足够大以包含节点标记的自定义突出显示矩形。

在此示例中, 事件的处理程序 TreeView.DrawNode 通过调用 类的方法 DrawTreeNodeEventArgs 绘制未选定的节点。 这些方法为不需要自定义的 TreeView 元素提供默认外观。 处理程序会手动绘制节点标记和自定义选择。

有关完整示例,请参阅 TreeView.DrawNode 参考主题。

C#
// 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);
        }
    }
}

注解

此枚举由 State 类的 DrawTreeNodeEventArgs 属性使用。 有关详细信息,请参阅 TreeView.DrawNode 事件。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9

另请参阅