Skip to content

Owner drawing

Usually, the node content solely consists of node controls that render the bound data in the treeview. However, there may be cases when you need to customize the node appearance manually to fit specific requirements.

Below is what Flexible TreeView provides for that matter.

Node background owner drawing#

The node background appearance under the drawn node controls content can be changed, i.e., drawn manually. To do this, a custom treeview class derived from ARMSoft.FlexibleTreeView.FlexibleTreeView should be created, and its DrawNode method should be overridden.

The DrawNode method gets the following parameters to paint itself:

  • node - the node being drawn.
  • context - node drawing operation information.
  • bounds - drawing area bounds.

The context parameter contains the main information about the drawing operation. Use the context.Graphics to access the System.Drawing.Graphics instance to draw the primitives. Also, by using the context.SetClip method, the current drawing operation clipping region can be set or limited.

The bounds parameter contains the drawing node bounds rectangle.

Below is an example of how to manually draw the background under a node.

Example

class FlexibleTreeViewEx : FlexibleTreeView
{
    protected override void DrawNode(Node node, DrawContext context, Rectangle bounds)
    {
        context.SetClipFromBounds(true);
        using (LinearGradientBrush brush = new LinearGradientBrush(bounds, Color.LightCoral, Color.LightBlue, 90))
        {
            context.Graphics.FillRectangle(brush, bounds);
        }
        context.RestoreClip();

        base.DrawNode(node, context, bounds);
    }
}

FlexibleTreeViewEx tree = new FlexibleTreeViewEx();

NodeTextBox textCtrl = new NodeTextBox();
textCtrl.AttachTo(tree);

Node node = new Node("Custom background node");
node.AttachTo(tree);
Class FlexibleTreeViewEx
    Inherits FlexibleTreeView
    Protected Overrides Sub DrawNode(node As Node, context As DrawContext, bounds As Rectangle)
        context.SetClipFromBounds(True)
        Using brush As New LinearGradientBrush(bounds, Color.LightCoral, Color.LightBlue, 90)
            context.Graphics.FillRectangle(brush, bounds)
        End Using
        context.RestoreClip()

        MyBase.DrawNode(node, context, bounds)
    End Sub
End Class

Dim tree As New FlexibleTreeViewEx()

Dim textCtrl As New NodeTextBox()
textCtrl.AttachTo(tree)

Dim node As New Node("Custom background node")
node.AttachTo(tree)