Writing Custom Report Components

Top  Previous  Next

 

FastReport has a large number of components that can be placed on a report design page. They are: text, picture, line, geometrical figure, OLE, rich text, bar code, diagram etc. You can also write your own custom component and then attach it to FastReport.

 

FastReport has several classes from which components can be inherited. For more details, see “FastReport Class Hierarchy”. The TfrxView class is of primary interest, since most report components are inherited from it.

 

As a minimum the “Draw” method in the TfrxReportComponent base class should be defined.

 

procedure Draw(Canvas: TCanvas;

              ScaleX, ScaleY, OffsetX, OffsetY: Extended); virtual;

 

This method is called when the component is painted in the designer, in the preview window and during output printing. TfrxView overrides this method for drawing the object frame and background. This method should draw the component's contents on the “Canvas” drawing surface. The object coordinates and sizes are stored in the “AbsLeft”, “AbsTop” and “Width”, “Height” properties respectively.

 

The “ScaleX” and “ScaleY” parameters define the object scaling in the X-axis and Y-axis respectively. These parameters equal 1 at 100% zoom and can change if the user modifies zooming either in the designer or in the preview window. The “OffsetX” and “OffsetY” parameters shift the object along the X-axis and Y-axis. So, taking all these parameters into account the upper left corner coordinate will be:

 

X := Round(AbsLeft * ScaleX + OffsetX);

Y := Round(AbsTop * ScaleY + OffsetY);

 

To simplify operations with coordinates, the “BeginDraw” method (with parameters similar to “Draw”) is defined in the “TfrxView” class.

 

procedure BeginDraw(Canvas: TCanvas;

                   ScaleX, ScaleY, OffsetX, OffsetY: Extended); virtual;

 

This method should be called in the first line of the “Draw” method. It transforms the coordinates into FX, FY, FX1, FY1, FDX, FDY and FFrameWidth integer values, which can be used later in TCanvas methods. It also copies Canvas, ScaleX and ScaleY values into the FCanvas, FScaleX and FScaleY variables, which can be referred to in any class method.

 

There are also two methods in the TfrxView class for drawing object backgrounds and frames.

 

procedure DrawBackground;

procedure DrawFrame;

 

The BeginDraw method should be called before calling these two methods.

 

Let's look at how to create a component which will display an arrow.

 

type

 TfrxArrowView = class(TfrxView)

 public

   { we should override only two methods }

  procedure Draw(Canvas: TCanvas;

                  ScaleX, ScaleY, OffsetX, OffsetY: Extended); override;

  class function GetDescription: String; override;

 published

   { place required properties in the published section }

  property BrushStyle;

  property Color;

  property Frame;

 end;

 

class function TfrxArrowView.GetDescription: String;

begin

 { component description will be displayed next to its icon in toolbar }

 Result := 'Arrow object';

end;

 

procedure TfrxArrowView.Draw(Canvas: TCanvas;

                            ScaleX, ScaleY, OffsetX, OffsetY: Extended);

begin

 { call this method to transform coordinates }

 BeginDraw(Canvas, ScaleX, ScaleY, OffsetX, OffsetY);

with Canvas do

begin

   { set colors }

   Brush.Color := Color;

   Brush.Style := BrushStyle;

   Pen.Width := FFrameWidth;

   Pen.Color := Frame.Color;

   { draw arrow }

   Polygon(

     [Point(FX, FY + FDY div 4),

     Point(FX + FDX * 38 div 60, FY + FDY div 4),

     Point(FX + FDX * 38 div 60, FY),

     Point(FX1, FY + FDY div 2),

     Point(FX + FDX * 38 div 60, FY1),

     Point(FX + FDX * 38 div 60, FY + FDY * 3 div 4),

     Point(FX, FY + FDY * 3 div 4)]);

end;

end;

 

{ registration }

var

 Bmp: TBitmap;

 

initialization

 Bmp := TBitmap.Create;

 Bmp.LoadFromResourceName(hInstance, 'frxArrowView');

 frxObjects.RegisterObject(TfrxArrowView, Bmp);

 

finalization

 { delete from list of available components }

 frxObjects.Unregister(TfrxArrowView);

 Bmp.Free;

 

end.

 

 

To create a component which displays data from a DB move the DataSet and DataField properties into the “published” section and then override the “GetData” method. Let's look at this by using the TfrxCheckBoxView standard component as an example.

 

The “TfrxCheckBoxView” component can be connected to a DB field using the “DataSet” and “DataField” properties, which are declared in the TfrxView base class. This component also has the “Expression” property which can hold an expression. As soon as the expression has been calculated the result is placed in the “Checked” property. The component displays a cross when “Checked” is “True.” Below are the most important parts of the component’s definition.

 

TfrxCheckBoxView = class(TfrxView)

private

 FChecked: Boolean;

 FExpression: String;

procedure DrawCheck(ARect: TRect);

public

procedure Draw(Canvas: TCanvas;

                ScaleX, ScaleY, OffsetX, OffsetY: Extended); override;

procedure GetData; override;

published

property Checked: Boolean read FChecked write FChecked default True;

property DataField;

property DataSet;

property Expression: String read FExpression write FExpression;

end;

 

procedure TfrxCheckBoxView.Draw(Canvas: TCanvas;

                               ScaleX, ScaleY, OffsetX, OffsetY: Extended);

begin

 BeginDraw(Canvas, ScaleX, ScaleY, OffsetX, OffsetY);

 

 DrawBackground;

 DrawCheck(Rect(FX, FY, FX1, FY1));

 DrawFrame;

end;

 

procedure TfrxCheckBoxView.GetData;

begin

inherited;

if IsDataField then

   FChecked := DataSet.Value[DataField]

else if FExpression <> '' then

   FChecked := Report.Calc(FExpression);

end;