閱讀英文

共用方式為


TreeNodeStates 列舉

定義

定義表示 TreeNode 可能狀態的常數。

此列舉支援其成員值的位元組合。

C#
[System.Flags]
public enum TreeNodeStates
繼承
TreeNodeStates
屬性

欄位

名稱 Description
Checked 8

已核取節點。

Default 32

節點處於預設狀態。

Focused 16

節點有焦點。

Grayed 2

節點停用。

Hot 64

節點作用中 (Hot)。 當 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

另請參閱