Skip to main content

How to Custom Paint Indent Cells

  • 2 minutes to read

The following example demonstrates how to handle the tree list’s OnCustomDrawIndentCell event used to manually paint indent cells.

In the example, the event handler checks whether a node contains an expand button, and if the expand button exists, paints it (the InternalDrawExpandButton and InternalDrawExpandButtonSign procedures are used for this purpose).

//...
uses
//...
  cxGeometry;
procedure <Form>.<TreeList>CustomDrawIndentCell(Sender: TcxCustomTreeList; ACanvas: TcxCanvas; AViewInfo: TcxTreeListIndentCellViewInfo; var ADone: Boolean);
  procedure InternalDrawExpandButtonSign(ACanvas: TcxCanvas; const R: TRect; AExpanded: Boolean; AColor: TColor);
  var
  ASize, X, Y: Integer;
  begin
    with R do
    begin
      ASize := Right - Left - 2 * 2;
      X := (Left + Right) div 2;
      Y := (Top + Bottom) div 2;
    end;
    ACanvas.Brush.Color := AColor;
    ACanvas.FillRect(Rect(X - ASize div 2, Y, X + ASize div 2 + 1, Y + 1));
    if not AExpanded then
      ACanvas.FillRect(Rect(X, Y - ASize div 2, X + 1, Y + ASize div 2 + 1));
  end;
  procedure InternalDrawExpandButton (ACanvas: TcxCanvas; R: TRect; AExpanded: Boolean; AColor: TColor = clDefault);
  begin
    with ACanvas, R do
    begin
    Brush.Color := AColor;
    FillRect(cxRect(Left - 1, Top - 1, Right + 1, Bottom + 1));
    Brush.Color := clBtnFace;
    FillRect(cxRect(Left, Top, Right, Bottom));
    InternalDrawExpandButtonSign(ACanvas, R, AExpanded, clBtnText);
    end;
  end;
begin
  // assign the default font and color settings
  cxApplyViewParams(ACanvas, AViewInfo.ViewParams);
  // specify the indent cell color
  ACanvas.Brush.Color := $FAE6E6 - $000100;
  // paint indent cells
  ACanvas.FillRect(AViewInfo.VisibleRect);
  if (AViewInfo.Button) then
    begin
      // if the current indent cell painted belongs to a group node, then paint the expand button
      InternalDrawExpandButton(ACanvas, AViewInfo.GlyphRect, AViewInfo.Node.Expanded);
    end;
  // suppress the default painting
  ADone := True;
end;