

Void myTreeView_MouseDown( Object^ /*sender*/, MouseEventArgs^ e ) Selects a node that is clicked on its label or tag text. Rectangle focusBounds = NodeBounds( e->Node ) įocusBounds.Size = System::Drawing::Size( focusBounds.Width - 1, focusBounds.Height - 1 ) Į->Graphics->DrawRectangle( focusPen, focusBounds ) Pen^ focusPen = gcnew Pen( Color::Black ) įocusPen->DashStyle = System::Drawing::Drawing2D::DashStyle::Dot If ( (e->State & TreeNodeStates::Focused) != (TreeNodeStates)0 ) it large enough to include the text of the node tag, if present. If the node has focus, draw the focus rectangle large, making If a node tag is present, draw its string representationĮ->Graphics->DrawString( e->Node->Tag->ToString(), tagFont, Brushes::Yellow, (float)e->Bounds.Right + 2, (float)e->Bounds.Top ) Use the default background and node text. System::Drawing::Font^ nodeFont = e->Node->NodeFont Į->Graphics->DrawString( e->Node->Text, nodeFont, Brushes::White, Rectangle::Inflate( e->Bounds, 2, 0 ) ) include the text of a node tag, if one is present.Į->Graphics->FillRectangle( Brushes::Green, NodeBounds( e->Node ) ) method makes the highlight rectangle large enough to

Draw the background of the selected node. If ( (e->State & TreeNodeStates::Selected) != (TreeNodeStates)0 ) Draw the background and node text for a selected node. Void myTreeView_DrawNode( Object^ sender, DrawTreeNodeEventArgs^ e ) This->ClientSize = System::Drawing::Size( 292, 273 ) Initialize the form and add the TreeView control to it. MyTreeView->MouseDown += gcnew MouseEventHandler( this, &TreeViewOwnerDraw::myTreeView_MouseDown ) selected by clicking the tag text as well as the node text. Add a handler for the MouseDown event so that a node can be MyTreeView->DrawNode += gcnew DrawTreeNodeEventHandler( this, &TreeViewOwnerDraw::myTreeView_DrawNode ) MyTreeView->DrawMode = TreeViewDrawMode::OwnerDrawText Configure the TreeView control for owner-draw and add MyTreeView->SelectedNode = myTreeView->Nodes->Nodes MyTreeView->Nodes->Nodes->BackColor = Color::Yellow MyTreeView->Nodes->Nodes->Tag = "urgent!" and set the node background color to highlight them. Add tags containing alert messages to a few nodes Create and initialize the TreeView control.įor ( int x = 1 x Nodes->Add( String::Format( "Task ", y ) ) TagFont = gcnew System::Drawing::Font( "Helvetica",8,FontStyle::Bold ) Create a Font object for the node tags. Public ref class TreeViewOwnerDraw: public Form The MouseDown event handler selects a node that is clicked anywhere within this region or within the region around a node tag, if present. By default, a node can be selected only by clicking the region around its label. For these, the DrawTreeNodeEventArgs.DrawDefault property is set to true so that they will be drawn by the operating system.Īdditionally, a handler for the MouseDown event provides hit-testing. Unselected nodes do not need customization. In the example, a handler for the DrawNode event draws the node tags and the custom selection highlight manually. Owner drawing must be used to draw the node tags and to draw a customized highlight rectangle large enough to include a node tag. Additionally, the default selection highlight rectangle extends only around a node label. You can customize most of the TreeView colors by setting color properties, but the selection highlight color is not available as a property. The TreeView control also uses custom colors, which include a custom highlight color. Node tags are specified by using the TreeNode.Tag property. The TreeView control in the example displays optional node tags alongside the standard node labels.

#Treeview winforms how to
The following code example demonstrates how to customize a TreeView control using owner drawing. public:Įvent System::Windows::Forms::DrawTreeNodeEventHandler ^ DrawNode public event DrawNode member this.DrawNode : Public Custom Event DrawNode As DrawTreeNodeEventHandler Event Type DrawTreeNodeEventHandler Examples Occurs when a TreeView is drawn and the DrawMode property is set to a TreeViewDrawMode value other than Normal.
