コードを使用したレポートの構築
FastReport エンジンは通常、レポートの構成を担当します。これは、レポートのバンドを特定の順序で、接続されているデータ ソースが必要とする回数だけ表示することによって、完成したレポートを形成していきます。ときには、FastReport のエンジンでは生成できない、非標準のフォームのレポートの作成が必要になることもあります。この場合は、TfrxReport.OnManualBuild
イベントを利用して、レポートを手動で構成する機能を使用できます。このイベントのハンドラーを定義すると、FastReport エンジンはハンドラーに管理を引き渡します。それと同時に、レポートの形成に対する責任の配分は、次のように変更されます。
FastReport エンジン:
レポートの準備(スクリプト、データ ソースの初期化、バンドのツリーの形成)
すべての計算(集計関数、イベント ハンドラー)
新しいページ/列の形成(ページ、列ヘッダー、列フッター、レポート タイトル、レポートの概要の自動表示)
その他のルーチン ワーク
ハンドラー:
- 特定の順序でのバンドの表示
OnManualBuild ハンドラーの本質は、FastReport のエンジンに対して、特定のバンドの表示に関するコマンドを発行することです。残りの作業はエンジン自体が実行します。たとえば、現在のページに場所がなくなったらすぐに新しいページを作成するとか、スクリプトの実行を行うとかなどです。
エンジンは TfrxCustomEngine
クラスで表されます。このクラスのインスタンスへのリンクは、TfrxReport.Engine
プロパティにあります。
プロパティまたはメソッド | 説明 |
---|---|
procedure NewColumn | 新しい列を作成します。列が最後の列である場合、これは新しいページを作成します。 |
procedure NewPage | 新しいページを作成します。 |
procedure ShowBand(Band: TfrxBand) | バンドを表示します。 |
procedure ShowBand(Band: TfrxBandClass) | 指定された種類のバンドを表示します。 |
function FreeSpace: Extended | ページの空き領域の量を返します(ピクセル単位)。次のバンドが表示された後、この値は減少します。 |
property CurColumn: Integer | 現在の列の番号を返す、または設定します。 |
property CurX: Extended | 現在の X 位置を返す、または設定します。 |
property CurY: Extended | 現在の Y 位置を返す、または設定します。次のバンドが表示された後、この値は上がります。 |
property DoublePass: Boolean | レポートは 2 パス(ダブルパス)であるかどうかを定義します。 |
property FinalPass: Boolean | 現在のパスが最後のパスであるかどうかを定義します。 |
property FooterHeight: Extended | ページ フッターの高さを返します。 |
property HeaderHeight: Extended | ページ ヘッダーの高さを返します。 |
property PageHeight: Extended | ページの印刷可能な領域の高さを返します。 |
property PageWidth: Extended | ページの印刷可能な領域の幅を返します。 |
property TotalPages: Integer | 完成したレポートのページ数を返します(ダブルパス レポートの第 2 パスでのみ)。 |
単純なハンドラーの例を挙げてみましょう。レポートには 2 つのマスター データ バンドがあり、これらはデータに接続されていません。ハンドラーは、これらのバンドを交互に各 6 回表示します。バンドを 6 回繰り返した後、小さなすき間が作られます。
Pascal:
var
i: Integer;
Band1, Band2: TfrxMasterData;
{ 必要なバンドを探す }
Band1 := frxReport1.FindObject('MasterData1') as TfrxMasterData;
Band2 := frxReport1.FindObject('MasterData2') as TfrxMasterData;
for i := 1 to 6 do
begin
{ バンドを順々にリード/推測する }
frxReport1.Engine.ShowBand(Band1);
frxReport1.Engine.ShowBand(Band2);
{ 小さいすき間を作る }
if i = 3 then
frxReport1.Engine.CurY := frxReport1.Engine.CurY + 10;
end;
C++:
int i;
TfrxMasterData * Band1;
TfrxMasterData * Band2;
// 必要なバンドを探す
Band1 := dynamic_cast <TfrxMasterData *> (frxReport1->FindObject("MasterData1"));
Band2 := dynamic_cast <TfrxMasterData *> (frxReport1->FindObject("MasterData2"));
for(i = 1; i <= 6; i++)
{
// バンドを順々にリード/推測する
frxReport1->Engine->ShowBand(Band1);
frxReport1->Engine->ShowBand(Band2);
// 小さいすき間を作る
if(i == 3)
frxReport1->Engine->CurY += 10;
}
次の例では、バンドの 2 つのグループをお互い横に並べて表示します。
Pascal:
var
i, j: Integer;
Band1, Band2: TfrxMasterData;
SaveY: Extended;
Band1 := frxReport1.FindObject('MasterData1') as TfrxMasterData;
Band2 := frxReport1.FindObject('MasterData2') as TfrxMasterData;
SaveY := frxReport1.Engine.CurY;
for j := 1 to 2 do
begin
for i := 1 to 6 do
begin
frxReport1.Engine.ShowBand(Band1);
frxReport1.Engine.ShowBand(Band2);
if i = 3 then
frxReport1.Engine.CurY := frxReport1.Engine.CurY + 10;
end;
frxReport1.Engine.CurY := SaveY;
frxReport1.Engine.CurX := frxReport1.Engine.CurX + 200;
end;
C++:
int i, j;
TfrxMasterData * Band1;
TfrxMasterData * Band2;
Extended SaveY;
Band1 = dynamic_cast <TfrxMasterData *> (frxReport1->FindObject("MasterData1"));
Band2 = dynamic_cast <TfrxMasterData *> (frxReport1->FindObject("MasterData2"));
SaveY = frxReport1->Engine->CurY;
for(j = 1; j <= 2; j++)
{
for(i = 1; i <= 6; i++)
{
frxReport1->Engine->ShowBand(Band1);
frxReport1->Engine->ShowBand(Band2);
if(i == 3)
frxReport1->Engine->CurY += 10;
}
frxReport1->Engine->CurY = SaveY;
frxReport1->Engine->CurX += 200;
}