Delphi 7 has included Rave Reports as the default reporting solution, replacing Quick Reports. Since they work in very different paradigms, many people were confused by the new environment. This is intended as an introduction for people who haven't worked with Rave yet, and would like to start.
You can develop reports with Rave using two different ways: Code Based or with the Visual Designer. This document describes how to work with the code based engine. Part II will describe the Visual Designer.
Code Based Reports
With Code Based, you write reports using plain Delphi code. That provides a very flexible way displaying any kind of data, allowing any kind of complex layouts.
To write a code based report, just drop a TRvSystem component on the form and write the report on the OnPrint event handler. Sender is the report you are creating, and can be typecasted to TBaseReport. It contains all the methods you need to output information to that particular report.
Simple Code Base ReportHere's a simple report using the code based mechanism:
procedure TFormMain.RvSystemPrint(Sender: TObject);
with Sender as TBaseReport do
Print('Welcome to Code Based Reporting in Rave');
To execute this report, call RvSystem.Execute method.
So, what does that simple code do? First, it calls SetFont to select the font and size of the text that will be printed from that point on. Then it positions the cursor on the coordinates (1,1). These coordinates are expressed using the units set in the SystemPrinter.Units property of the RvSystem object, and it defaults to Inches. You can set it to unUser and set a number relative to Inches in the SystemPrinter.UnitsFactor property. For example, if UnitsFactor was set to 0.5 then 1 unit would correspond to half an inch. Finally, the code calls the Print method to output the text. Here's the output:
Tabular Code Based Report
Here's another example. It displays a list of the folders in the root of the current drive, along with a recursive count of number of files and folder, and total size of the files included in each folder.
procedure TFormMain.PrintTabularReport(Report: TBaseReport);
FolderList : TStringList;
i : Integer;
NumFiles : Cardinal;
NumFolders : Cardinal;
SizeFiles : Cardinal;
Root : string;
with Report do
PrintCenter('List of Folders in the Drive Root', 4);
SetTab(0.2, pjLeft, 1.7, 0, 0, 0);
SetTab(1.7, pjRight, 3.1, 0, 0, 0);
SetTab(3.1, pjRight, 3.5, 0, 0, 0);
SetTab(3.5, pjRight, 4.5, 0, 0, 0);
Bold := True;
PrintTab('Number of Files');
PrintTab('Number of Folders');
PrintTab('Size of Files');
Bold := False;
FolderList := TStringList.Create;
Root := IncludeTrailingPathDelimiter(ExtractFileDrive(ParamStr(0)));
for i := 0 to FolderList.Count - 1 do
NumFiles, NumFolders, SizeFiles);
Notice that a different approach has been taken: instead of specifying the coordinates of each text output, the printing was done using Lines and Columns as references. The line heigh depends on the size of the current font: each unit represents 1/72nds of an inch, so each line printed with a size 10 font will have, aproximatelly, a height of 0.138 inches. Lines are advanced after calls to PrintLn or NewLine. Colums are defined using calls to the SetTabs method, and the PrintTab method will print the text in the current column and advance to the next one. Here's the output:
You can find the full source, including the implementation of EnumFolders and GetFolderInfo, on CodeCentral.
Graphical Code Based Report
You can include shapes and images in your code based report, along with the text. The following example demonstrates that:
procedure TFormMain.PrintGraphicsReport(Report: TBaseReport);
Bitmap : TBitmap;
with Report do
Canvas.Brush.Color := clGray;
Rectangle(0.3, 0.3, 4.7, 3.3);
FontColor := clRed;
PrintXY(0.5,0.5, 'Just look at all the graphics!');
Bitmap := TBitmap.Create;
Canvas.Pen.Color := clBlue;
Canvas.Brush.Bitmap := Bitmap;
Canvas.Pen.Color := clBlack;
Canvas.Brush.Style := bsSolid;
Canvas.Brush.Color := clYellow;
Canvas.Brush.Color := clGreen;
In this example the methods Rectangle, Ellipse and Pie have been used draw shapes with different fills. Bitmaps were outputted using PrintBitmap and as the brush of the ellipses. Here's the output:
A sample application, containing full source code for those three examples can be found at CodeCentral.
As you can see, code based reporting offers a great flexibility and control of the layout you want, but require some work to implement them. In Part II we will see how the Visual Designer can be used to build powerful reports in a very easy way.
Looking for last minute shopping deals? Find them fast with Yahoo! Search.