How To…Render a Form Control to a Report

Question

“How can I render a control on my form, to my report?”

Solution

The following example uses the ReportBuilder PaintBox control to render a TProgressBar and a TGauge to a report. The TProgressBar is a TWinControl descendant and the TGauge is a TGraphicControl descendant.

Download:  RenderControlToPaintBox.zip

procedure TForm1.ppPaintBox1Print(Sender: TObject);
begin

  // size the paint box
  ppPaintBox1.spWidth := ProgressBar1.Width;
  ppPaintBox1.spHeight := ProgressBar1.Height;

  // use the TWinControl.PaintTo method to render to the PaintBox.Canvas
  ProgressBar1.PaintTo(ppPaintBox1.Canvas, 0, 0);

end;

type

  // used to provide access to a TGraphicControl.Canvas
  TMyGraphicControlAccess = class(TGraphicControl)
    public
      property Canvas;
  end;

procedure TForm1.ppPaintBox2Print(Sender: TObject);
var
  lSourceCanvas: TCanvas;
begin

  // size the paint box
  ppPaintBox2.spWidth := Gauge1.Width;
  ppPaintBox2.spHeight := Gauge1.Height;

  // get access to the TGraphicControl.Canvas
  lSourceCanvas := TMyGraphicControlAccess(Gauge1).Canvas;

  // Copy the image to the PaintBox.Canvas
  ppPaintBox2.Canvas.CopyRect(lSourceCanvas.ClipRect, lSourceCanvas, lSourceCanvas.ClipRect);

end;