Export (0) Print
Expand All

DrawTreeNodeEventArgs.Bounds Property

Gets the size and location of the TreeNode to draw.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public Rectangle Bounds { get; }

Property Value

Type: System.Drawing.Rectangle
A Rectangle that represents the bounds of the TreeNode to draw.

The location specified by this property is relative to the upper-left corner of the TreeView control.

When the TreeView.DrawMode property is set to OwnerDrawText, the area specified by the Bounds property is the region in which a node can be clicked to select it. If you draw outside this hit test region, you should provide your own code for selecting a node when its visible area is clicked. You may also want to provide your own hit test code when the TreeView.DrawMode property is set to OwnerDrawAll. In this mode, the Bounds property encompasses the whole node across the entire width of the TreeView control, rather than the just hit test region.

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 using the TreeNode.Tag property. The TreeView control also uses custom colors, including 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 TreeView.DrawNode event draws the node tags and the custom selection highlight manually. Unselected nodes do not need customization. For these, the DrawDefault property is set to true so that they will be drawn by the operating system.

For the complete example, see the DrawTreeNodeEventArgs overview reference topic.

// 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);
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1
Show:
© 2015 Microsoft