DrawTreeNodeEventArgs Classe
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Fornece dados para o evento de DrawNode .
public ref class DrawTreeNodeEventArgs : EventArgs
public class DrawTreeNodeEventArgs : EventArgs
type DrawTreeNodeEventArgs = class
inherit EventArgs
Public Class DrawTreeNodeEventArgs
Inherits EventArgs
- Herança
O exemplo de código a seguir demonstra como personalizar um TreeView controle usando o desenho do proprietário. O TreeView controle no exemplo exibe marcas de nó opcionais junto com os rótulos de nó padrão. As marcas de nó são especificadas usando a TreeNode.Tag propriedade . O TreeView controle também usa cores personalizadas, incluindo uma cor de realce personalizada.
Você pode personalizar a maioria das cores definindo as TreeView propriedades de cor, mas a cor de realce de seleção não está disponível como uma propriedade. Além disso, o retângulo de realce de seleção padrão se estende apenas em torno de um rótulo de nó. O desenho do proprietário deve ser usado para desenhar as marcas de nó e desenhar um retângulo de realce personalizado grande o suficiente para incluir uma marca de nó.
No exemplo, um manipulador para o TreeView.DrawNode evento desenha as marcas de nó e a seleção personalizada realçam manualmente. Nós não selecionados não precisam de personalização. Para eles, a DrawDefault propriedade é definida como para true
que elas sejam desenhadas pelo sistema operacional.
Além disso, um manipulador para o Control.MouseDown evento fornece teste de ocorrência. Por padrão, um nó só pode ser selecionado clicando na região ao redor do rótulo. O Control.MouseDown manipulador de eventos seleciona um nó que é clicado em qualquer lugar dentro dessa região ou dentro da região em torno de uma marca de nó, se presente.
#using <System.dll>
#using <System.Windows.Forms.dll>
#using <System.Drawing.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class TreeViewOwnerDraw: public Form
{
private:
TreeView^ myTreeView;
// Create a Font object for the node tags.
System::Drawing::Font^ tagFont;
public:
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 );
}
protected:
// Clean up any resources being used.
~TreeViewOwnerDraw()
{
if ( tagFont != nullptr )
{
delete tagFont;
}
}
// 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);
}
}
}
// Selects a node that is clicked on its label or tag text.
void myTreeView_MouseDown( Object^ /*sender*/, MouseEventArgs^ e )
{
TreeNode^ clickedNode = myTreeView->GetNodeAt( e->X, e->Y );
if ( NodeBounds( clickedNode ).Contains( e->X, e->Y ) )
{
myTreeView->SelectedNode = clickedNode;
}
}
// Returns the bounds of the specified node, including the region
// occupied by the node label and any node tag displayed.
Rectangle NodeBounds( TreeNode^ node )
{
// Set the return value to the normal node bounds.
Rectangle bounds = node->Bounds;
if ( node->Tag != nullptr )
{
// Retrieve a Graphics object from the TreeView handle
// and use it to calculate the display width of the tag.
Graphics^ g = myTreeView->CreateGraphics();
int tagWidth = (int)g->MeasureString( node->Tag->ToString(), tagFont ).Width + 6;
// Adjust the node bounds using the calculated value.
bounds.Offset( tagWidth / 2, 0 );
bounds = Rectangle::Inflate( bounds, tagWidth / 2, 0 );
g->~Graphics();
}
return bounds;
}
};
[STAThreadAttribute]
int main()
{
Application::Run( gcnew TreeViewOwnerDraw );
}
using System;
using System.Drawing;
using System.Windows.Forms;
public class TreeViewOwnerDraw : Form
{
private TreeView myTreeView;
// Create a Font object for the node tags.
Font tagFont = new Font("Helvetica", 8, FontStyle.Bold);
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);
}
// Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing)
{
tagFont.Dispose();
}
base.Dispose(disposing);
}
[STAThreadAttribute()]
static void Main()
{
Application.Run(new TreeViewOwnerDraw());
}
// 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);
}
}
}
// Selects a node that is clicked on its label or tag text.
private void myTreeView_MouseDown(object sender, MouseEventArgs e)
{
TreeNode clickedNode = myTreeView.GetNodeAt(e.X, e.Y);
if (NodeBounds(clickedNode).Contains(e.X, e.Y))
{
myTreeView.SelectedNode = clickedNode;
}
}
// Returns the bounds of the specified node, including the region
// occupied by the node label and any node tag displayed.
private Rectangle NodeBounds(TreeNode node)
{
// Set the return value to the normal node bounds.
Rectangle bounds = node.Bounds;
if (node.Tag != null)
{
// Retrieve a Graphics object from the TreeView handle
// and use it to calculate the display width of the tag.
Graphics g = myTreeView.CreateGraphics();
int tagWidth = (int)g.MeasureString
(node.Tag.ToString(), tagFont).Width + 6;
// Adjust the node bounds using the calculated value.
bounds.Offset(tagWidth/2, 0);
bounds = Rectangle.Inflate(bounds, tagWidth/2, 0);
g.Dispose();
}
return bounds;
}
}
Imports System.Drawing
Imports System.Windows.Forms
Public Class TreeViewOwnerDraw
Inherits Form
Private WithEvents myTreeView As TreeView
' Create a Font object for the node tags.
Private tagFont As New Font("Helvetica", 8, FontStyle.Bold)
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
<STAThreadAttribute()> _
Shared Sub Main()
Application.Run(New TreeViewOwnerDraw())
End Sub
' 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
' Selects a node that is clicked on its label or tag text.
Private Sub myTreeView_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
Dim clickedNode As TreeNode = myTreeView.GetNodeAt(e.X, e.Y)
If NodeBounds(clickedNode).Contains(e.X, e.Y) Then
myTreeView.SelectedNode = clickedNode
End If
End Sub
' Returns the bounds of the specified node, including the region
' occupied by the node label and any node tag displayed.
Private Function NodeBounds(ByVal node As TreeNode) As Rectangle
' Set the return value to the normal node bounds.
Dim bounds As Rectangle = node.Bounds
If (node.Tag IsNot Nothing) Then
' Retrieve a Graphics object from the TreeView handle
' and use it to calculate the display width of the tag.
Dim g As Graphics = myTreeView.CreateGraphics()
Dim tagWidth As Integer = CInt(g.MeasureString( _
node.Tag.ToString(), tagFont).Width) + 6
' Adjust the node bounds using the calculated value.
bounds.Offset(tagWidth \ 2, 0)
bounds = Rectangle.Inflate(bounds, tagWidth \ 2, 0)
g.Dispose()
End If
Return bounds
End Function 'NodeBounds
End Class
Use o evento para personalizar a aparência de nós em um TreeView controle usando o DrawNode desenho do proprietário.
O DrawNode evento é gerado por um TreeView controle quando sua TreeView.DrawMode propriedade é definida TreeViewDrawMode.OwnerDrawAll como ou TreeViewDrawMode.OwnerDrawText e um nó é exibido ou atualizado. O DrawTreeNodeEventArgs passado para o manipulador de eventos contém informações sobre o nó a ser desenhado, além de fornecer métodos para ajudá-lo a desenhar o nó.
Use as State propriedades ou Node para recuperar informações sobre o nó a ser desenhado. Use a Graphics propriedade para fazer o desenho real dentro da área especificada pela Bounds propriedade . Para fazer com que o sistema operacional desenhe um nó que não precisa ser desenhado pelo proprietário, defina a DrawDefault propriedade como true
.
Quando a TreeView.DrawMode propriedade é definida como TreeViewDrawMode.OwnerDrawText, a área indicada pela Bounds propriedade inclui apenas a parte de rótulo do nó. Quando a TreeView.DrawMode propriedade é definida TreeViewDrawMode.OwnerDrawAllcomo , a Bounds área inclui todo o nó, incluindo a área normalmente usada para ícones, caixas de seleção, sinais de adição e menos e linhas que conectam os nós.
Draw |
Inicializa uma nova instância da classe DrawTreeNodeEventArgs. |
Bounds |
Obtém o tamanho e o local do TreeNode a ser desenhado. |
Draw |
Obtém ou define um valor que indica se o TreeNode deve ser desenhado pelo sistema operacional em vez de pelo proprietário. |
Graphics | |
Node |
Obtém o TreeNode a ser desenhado. |
State |
Obtém o estado atual do TreeNode a ser desenhado. |
Equals(Object) |
Determina se o objeto especificado é igual ao objeto atual. (Herdado de Object) |
Get |
Serve como a função de hash padrão. (Herdado de Object) |
Get |
Obtém o Type da instância atual. (Herdado de Object) |
Memberwise |
Cria uma cópia superficial do Object atual. (Herdado de Object) |
To |
Retorna uma cadeia de caracteres que representa o objeto atual. (Herdado de Object) |
Produto | Versões |
---|---|
.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 |
Comentários do .NET
O .NET é um projeto código aberto. Selecione um link para fornecer comentários: