Skip to main content

TcxCustomTreeView.OnAdvancedCustomDrawItem Event

Enables you to paint tree view nodes before or after default painting takes place.

Declaration

property OnAdvancedCustomDrawItem: TTVAdvancedCustomDrawItemEvent read; write;

Remarks

The OnAdvancedCustomDrawItem event fires for each individual node of the control before and after its default painting takes place. The Stage parameter enables you to identify whether node painting is about to be performed or has been completed.

If the Stage parameter returns cdPrePaint, node painting has not been performed yet. In this instance, you may perform your own painting of the processed node and set the DefaultDraw parameter to False. This value for the DefaultDraw parameter implies that default painting of the processed node will not be performed. Note that if you only need to perform painting before the default drawing routine is executed, you should handle the OnCustomDrawItem event instead.

If the Stage parameter returns cdPostPaint, node painting has already been completed. At this time, you may perform any additional painting. For instance, you can paint a stop sign over the node’s image. Note that the DefaultDraw parameter is not in effect when the Stage parameter value is cdPostPaint. The reason is that no other painting will take place.

The Sender parameter of the event specifies the control whose item is being painted. Use the Canvas property of the object representing this parameter to obtain the canvas used to paint. The Node parameter enables you to determine the currently processed node. Use it to obtain the bounding rectangle of the node (the DisplayRect method) and other information needed when painting (caption text, image indexes, etc). The State parameter can be used to determine the state of the painted node. cdsSelected cdsHot included into this parameter’s value indicate that the node is selected or hot-tracked respectively. (Note that node hot-tracking is only available when the HotTrack property is set to True). And finally, the PaintImages parameter specifies whether node images should be painted by the default painting procedure. This parameter value is not in effect if the DefaultDraw parameter is set to False, since no default painting is performed in this instance.

The code below shows you how to use the OnAdvancedCustomDrawItem event for additional painting after the default procedure has been completed. The handler will strike out node captions residing at the third nesting level. Note that the color of the strike line will be different for selected and unselected nodes.

procedure TForm1.cxTreeView4AdvancedCustomDrawItem(Sender: TCustomTreeView;
  Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage;
  var PaintImages, DefaultDraw: Boolean);
var ARect: TRect;
begin
  if (Stage = cdPostPaint) and (Node.Level = 2) then
  begin
    ARect := Node.DisplayRect(True);
    with Sender.Canvas do
    begin
      Pen.Color := clRed;
      if cdsSelected in State then
        Pen.Color := clWhite;
      Pen.Style := psSolid;
      Pen.Width := 2;
      MoveTo(ARect.Left, ARect.Top + (ARect.Bottom - ARect.Top) div 2);
      LineTo(ARect.Right, ARect.Top + (ARect.Bottom - ARect.Top) div 2);
    end;
  end;
end;

Here is the result of code execution:

If you need to paint the background or foreground of the control, use the OnCustomDraw and OnAdvancedCustomDraw events.

See Also