Assembly: System.Windows.Forms (in System.Windows.Forms.dll)
Use this event to customize the appearance of nodes in a TreeView control using owner drawing.
This event is raised only when the DrawMode property is set to TreeViewDrawMode values of OwnerDrawAll or OwnerDrawText. The following table indicates how the TreeNode can be customized when the DrawMode property is set to these values.
DrawMode property value
The appearance of the entire TreeNode can be customized. Icons, check boxes, plus and minus signs, and lines connecting the nodes must be drawn manually if they are desired.
The region that the TreeNode.Text value would occupy if it were drawn using the font specified by the TreeView control's Font property is the region in which a node can be clicked to select it. This is called the hit test region. If you draw outside this region, you should provide your own code that selects a node when its visible area is clicked.
The hit test region corresponds to the DrawTreeNodeEventArgs.Bounds property when using OwnerDrawText. When using OwnerDrawAll, however, the DrawTreeNodeEventArgs.Bounds property encompasses the entire width of the TreeView. In this case, you can access the hit test region by getting the DrawTreeNodeEventArgs.Node value and accessing its TreeNode.Bounds property. You can then draw the hit test region of the node within these bounds, or you can provide your own hit test code. Note that setting the TreeNode.NodeFont property does not change the size of the hit test region, which is calculated using the font specified for the entire TreeView.
For more information about how to handle events, see Consuming Events.
The following code example demonstrates how to customize a TreeView control using owner drawing. The TreeView control in the example displays optional node tags alongside the standard node labels. Node tags are specified by using the TreeNode.Tag property. The TreeView control also uses custom colors, which include a custom highlight color.
You can customize most of the TreeView colors by setting color properties, but the selection highlight color is not available as a property. Additionally, the default selection highlight rectangle extends only around a node label. Owner drawing must be used to draw the node tags and to draw a customized highlight rectangle large enough to include a node tag.
In the example, a handler for the event draws the node tags and the custom selection highlight manually. Unselected nodes do not need customization. For these, the DrawTreeNodeEventArgs.DrawDefault property is set to true so that they will be drawn by the operating system.
Additionally, a handler for the MouseDown event provides hit-testing. By default, a node can be selected only by clicking the region around its label. The MouseDown event handler selects a node that is clicked anywhere within this region or within the region around a node tag, if present.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.