Question
“How can I can create a simple report wizard that implements a standard template?”
Solution
The following example shows how to create a custom report wizard and register it with RB. The TmyReportWizard class descends from TppCustomReportWizard and overrides a few simple methods. The ClassDescription and ClassBitmpa are used by the report designer’s ‘New…’ dialog. The Execute method loads a report template, called base.rtm. The initialization/finalization is used to register/unregister the wizard.
Download: ReportTemplateWizard.zip
Sample Delphi code:
TmyReportWizard = class(TppCustomReportWizard)
private
FReport: TppCustomReport;
FReportName: String;
protected
function GetReport: TObject; override;
function GetReportName: String; override;
public
constructor Create(aOwner: TComponent); override;
destructor Destroy; override;
class function ClassDescription: String; override;
class function ClassBitmap: THandle; override;
function Execute: Boolean; override;
end; {class, TmyReportWizard}
implementation
{$R myRptWiz.RES}
{******************************************************************************
*
** R E P O R T W I Z A R D
*
{******************************************************************************}
{------------------------------------------------------------------------------}
{ TmyReportWizard.Create }
constructor TmyReportWizard.Create(aOwner: TComponent);
begin
inherited Create(aOwner);
FReport := nil;
FReportName := '';
end;
{------------------------------------------------------------------------------}
{ TmyReportWizard.Destroy }
destructor TmyReportWizard.Destroy;
begin
inherited Destroy;
end;
{------------------------------------------------------------------------------}
{ TmyReportWizard.ClassDescription }
class function TmyReportWizard.ClassDescription: String;
begin
Result := 'My Default Template';
end;
{------------------------------------------------------------------------------}
{ TmyReportWizard.ClassBitmap }
class function TmyReportWizard.ClassBitmap: THandle;
begin
Result := ppBitmapFromResource('PPREPORTWIZARD');
end;
{------------------------------------------------------------------------------}
{ TmyReportWizard.Execute }
function TmyReportWizard.Execute: Boolean;
begin
Result := True;
// get a reference to the report
DoOnCreateReport(DataName, TObject(FReport));
if (FReport = nil) then Exit;
// load the default report template
FReport.Template.FileName := ExtractFilePath(ParamStr(0)) + 'base.rtm';
FReport.Template.LoadFromFile;
end;
{------------------------------------------------------------------------------}
{ TmyReportWizard.GetReport }
function TmyReportWizard.GetReport: TObject;
begin
Result := FReport;
end;
{------------------------------------------------------------------------------}
{ TmyReportWizard.GetReportName }
function TmyReportWizard.GetReportName: String;
begin
Result := FReportName;
end;
{******************************************************************************
*
** I N I T I A L I Z A T I O N / F I N A L I Z A T I O N
*
{******************************************************************************}
initialization
// replace default wizard used to create empty reports
ppRegisterDefaultBlankReportWizard(TmyReportWizard);
// replace the default dialog based wizard
// ppRegisterDefaultReportWizard(TmyReportWizard);
// add a new wizard
// ppRegisterWizard(TmyReportWizard);
finalization
ppUnRegisterWizard(TmyReportWizard);
end.