TreeView.DrawMode 属性

定义

获取或设置绘制控件的模式。

public:
 property System::Windows::Forms::TreeViewDrawMode DrawMode { System::Windows::Forms::TreeViewDrawMode get(); void set(System::Windows::Forms::TreeViewDrawMode value); };
public System.Windows.Forms.TreeViewDrawMode DrawMode { get; set; }
member this.DrawMode : System.Windows.Forms.TreeViewDrawMode with get, set
Public Property DrawMode As TreeViewDrawMode

属性值

TreeViewDrawMode 值之一。 默认值为 Normal

例外

属性值不是有效的 TreeViewDrawMode 值。

示例

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

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

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

TreeViewOwnerDraw()
{
   tagFont = gcnew System::Drawing::Font( "Helvetica",8,FontStyle::Bold );

   // Create and initialize the TreeView control.
   myTreeView = gcnew TreeView;
   myTreeView->Dock = DockStyle::Fill;
   myTreeView->BackColor = Color::Tan;
   myTreeView->CheckBoxes = true;
   
   // Add nodes to the TreeView control.
   TreeNode^ node;
   for ( int x = 1; x < 4; ++x )
   {
      // Add a root node to the TreeView control.
      node = myTreeView->Nodes->Add( String::Format( "Task {0}", x ) );
      for ( int y = 1; y < 4; ++y )
      {
         // Add a child node to the root node.
         node->Nodes->Add( String::Format( "Subtask {0}", y ) );
      }
   }
   myTreeView->ExpandAll();
   
   // Add tags containing alert messages to a few nodes 
   // and set the node background color to highlight them.
   myTreeView->Nodes[ 1 ]->Nodes[ 0 ]->Tag = "urgent!";
   myTreeView->Nodes[ 1 ]->Nodes[ 0 ]->BackColor = Color::Yellow;
   myTreeView->SelectedNode = myTreeView->Nodes[ 1 ]->Nodes[ 0 ];
   myTreeView->Nodes[ 2 ]->Nodes[ 1 ]->Tag = "urgent!";
   myTreeView->Nodes[ 2 ]->Nodes[ 1 ]->BackColor = Color::Yellow;
   
   // Configure the TreeView control for owner-draw and add
   // a handler for the DrawNode event.
   myTreeView->DrawMode = TreeViewDrawMode::OwnerDrawText;
   myTreeView->DrawNode += gcnew DrawTreeNodeEventHandler( this, &TreeViewOwnerDraw::myTreeView_DrawNode );
   
   // Add a handler for the MouseDown event so that a node can be 
   // selected by clicking the tag text as well as the node text.
   myTreeView->MouseDown += gcnew MouseEventHandler( this, &TreeViewOwnerDraw::myTreeView_MouseDown );
   
   // Initialize the form and add the TreeView control to it.
   this->ClientSize = System::Drawing::Size( 292, 273 );
   this->Controls->Add( myTreeView );
}
public TreeViewOwnerDraw()
{
    // Create and initialize the TreeView control.
    myTreeView = new TreeView();
    myTreeView.Dock = DockStyle.Fill;
    myTreeView.BackColor = Color.Tan;
    myTreeView.CheckBoxes = true;

    // Add nodes to the TreeView control.
    TreeNode node;
    for (int x = 1; x < 4; ++x)
    {
        // Add a root node to the TreeView control.
        node = myTreeView.Nodes.Add(String.Format("Task {0}", x));
        for (int y = 1; y < 4; ++y)
        {
            // Add a child node to the root node.
            node.Nodes.Add(String.Format("Subtask {0}", y));
        }
    }
    myTreeView.ExpandAll();

    // Add tags containing alert messages to a few nodes 
    // and set the node background color to highlight them.
    myTreeView.Nodes[1].Nodes[0].Tag = "urgent!";
    myTreeView.Nodes[1].Nodes[0].BackColor = Color.Yellow;
    myTreeView.SelectedNode = myTreeView.Nodes[1].Nodes[0];
    myTreeView.Nodes[2].Nodes[1].Tag = "urgent!";
    myTreeView.Nodes[2].Nodes[1].BackColor = Color.Yellow;

    // Configure the TreeView control for owner-draw and add
    // a handler for the DrawNode event.
    myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawText;
    myTreeView.DrawNode += 
        new DrawTreeNodeEventHandler(myTreeView_DrawNode);

    // Add a handler for the MouseDown event so that a node can be 
    // selected by clicking the tag text as well as the node text.
    myTreeView.MouseDown += new MouseEventHandler(myTreeView_MouseDown);

    // Initialize the form and add the TreeView control to it.
    this.ClientSize = new Size(292, 273);
    this.Controls.Add(myTreeView);
}
Public Sub New()

    ' Create and initialize the TreeView control.
    myTreeView = New TreeView()
    myTreeView.Dock = DockStyle.Fill
    myTreeView.BackColor = Color.Tan
    myTreeView.CheckBoxes = True
    
    ' Add nodes to the TreeView control.
    Dim node As TreeNode
    Dim x As Integer
    For x = 1 To 3

        ' Add a root node to the TreeView control.
        node = myTreeView.Nodes.Add(String.Format("Task {0}", x))
        Dim y As Integer
        For y = 1 To 3 

            ' Add a child node to the root node.
            node.Nodes.Add(String.Format("Subtask {0}", y))
        Next y
    Next x
    myTreeView.ExpandAll()
    
    ' Add tags containing alert messages to a few nodes 
    ' and set the node background color to highlight them.
    myTreeView.Nodes(1).Nodes(0).Tag = "urgent!"
    myTreeView.Nodes(1).Nodes(0).BackColor = Color.Yellow
    myTreeView.SelectedNode = myTreeView.Nodes(1).Nodes(0)
    myTreeView.Nodes(2).Nodes(1).Tag = "urgent!"
    myTreeView.Nodes(2).Nodes(1).BackColor = Color.Yellow
    
    ' Configure the TreeView control for owner-draw.
    myTreeView.DrawMode = TreeViewDrawMode.OwnerDrawText

    ' Add a handler for the MouseDown event so that a node can be 
    ' selected by clicking the tag text as well as the node text.
    AddHandler myTreeView.MouseDown, AddressOf myTreeView_MouseDown
    
    ' Initialize the form and add the TreeView control to it.
    Me.ClientSize = New Size(292, 273)
    Me.Controls.Add(myTreeView)
End Sub

注解

TreeView 节点通常由操作系统绘制。 属性 DrawMode 允许自定义节点的外观。 为此,请将 设置为 DrawModeTreeViewDrawMode.OwnerDrawAllTreeViewDrawMode.OwnerDrawText ,并为 事件提供处理程序 DrawNode 。 这称为所有者绘图。

适用于

另请参阅