How to add extra columns after the resource name.


This is a more specific example of adding custom graphics (FAQ 25.)

This example adds two columns. Add integer fields to the form for FTextWidth, FCol1X, FLine1X, FCol2X and FLine2X.

Screen shot

procedure TForm1.RACAdjustPlotRect(Sender: TObject;
                                   Canvas: TCanvas;
                                   var PlotRect: TRect);
var
  i, w, mw, sw: integer;
begin
  mw := 0;
  Canvas.Font.Assign(RAC.Font);
  for i := 0 to RAC.Count - 1 do begin
    w := Canvas.TextWidth(RAC.Resources[i].Name);
    if w > mw then
      mw := w;
  end;

  // allow room for our data and lines
  sw := Canvas.TextWidth(' ');
  FTextWidth := Canvas.TextWidth('12.34');

  // must add to existing PlotRect.left - as when
  // printing, PlotRect may already contain print area
  PlotRect.Left := PlotRect.Left + mw + FTextWidth * 2 + sw * 6;

  // calculate column coordinates
  FCol2X := PlotRect.Left - FTextWidth - sw;
  FLine2X := FCol2X - sw;
  FCol1X := FLine2X - FTextWidth - sw;
  FLine1X := FCol1X - sw;
end;

procedure TForm1.RACOwnerDrawResourceName(Sender: TObject;
                                          Resource: TssResource;
                                          Canvas: TCanvas;
                                          Rct: TRect);
var
  y: integer;
  str: string;
begin
  with Canvas do begin
    Font.Assign(RAC.Font);
    // display resource names plus addition column data
    y := (rct.Bottom + rct.Top - TextHeight('Hg')) div 2;
    TextOut(rct.Left + 2, y, Resource.Name);
    str := Format('%4.2f', [V1]); // first column
    TextOut(FCol1X + FTextWidth - TextWidth(str), y, str);
    str := Format('%4.2f', [V2]); // second column
    TextOut(FCol2X + FTextWidth - TextWidth(str), y, str);
   end;
end;

procedure TForm1.RACDrawProgress(Sender: TObject;
                                 Canvas: TCanvas;
                                 DrawRect: TRect;
                                 Progress: TssDrawProgress);
begin
  if Progress = dpBackgroundDone then
    // draw column background colours
    with RAC, Canvas do begin
      with Pen do begin
        Color := clBlack;
        Style := psSolid;
        Width := 1;
      end;
      Brush.Color := $B8E4CE;
      Brush.Style := bsSolid;
      Rectangle(FLine1X, PlotRect.Top,
                FLine2X+1, PlotRect.Bottom);
      Brush.Color := $E6E2B7;
      Rectangle(FLine2X, PlotRect.Top,
                PlotRect.Left+1, PlotRect.Bottom);
    end;
end;