原創|其它|編輯:郝浩|2012-12-03 11:23:37.000|閱讀 861 次
概述:FastReport VCL報表控件擁有大量可用于報表設計的內置標準函數。此外,FastReport VCL報表還允許編寫和使用自定義函數。本文主要介紹我們就一起來看看如何將程序/函數添加到FastReport VCL報表。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
FastReport VCL報表控件擁有大量可用于報表設計的內置標準函數。此外,FastReport VCL報表還允許編寫和使用自定義函數。通過 FastReport中的“FastScript”庫接口可添加函數。
接下來,我們就一起來看看如何將程序/函數添加到FastReport。參數的數量和類型根據函數的變化而變化。 FastScript不支持“Set” 和 “Record"類型的參數。
在Delphi窗體中,聲明了函數,程序以及其代碼:
function TForm1.MyFunc(s: String; i: Integer): Boolean; begin // required logic end; procedure TForm1.MyProc(s: String); begin // required logic end;
為報表控件創建“onUser”函數處理器:
function TForm1.frxReport1UserFunction(const MethodName: String; var Params: Variant): Variant; begin if MethodName = 'MYFUNC' then Result := MyFunc(Params[0], Params[1]) else if MethodName = 'MYPROC' then MyProc(Params[0]); end;
通過報表控件的add方法將函數添加到函數列表中。
frxReport1.AddFunction('function MyFunc(s: String; i: Integer):Boolean'); frxReport1.AddFunction('procedure MyProc(s: String)');
所添加的函數可在報表腳本中使用,并可以被“TfrxMemoView”類型的對象引用。此外,該函數還可以顯示在"Data tree" 函數選項卡上。在該選項卡上,函數被劃分為多個類別,當選擇函數提示時,該函數便出現在標簽的底部窗格中。
修改上面的代碼示例實現在不同類別中注冊函數,并顯示描述性提示:
frxReport1.AddFunction('function MyFunc(s: String; i: Integer): Boolean', 'My functions', ' MyFunc function always returns True'); frxReport1.AddFunction('procedure MyProc(s: String)', 'My functions', ' MyProc procedure does not do anything');
所添加的函數將出現在“My functions”類別中。
請使用下列其中一個類別名稱,在已有類別中注冊函數:
-'ctString' :string function
- 'ctDate': date/time functions
- 'ctConv' :conversion functions
- 'ctFormat' :formatting
- 'ctMath' :mathematical functions
- 'ctOther': other functions
如果類別名稱在左邊空白處,函數便被置于函數樹的根目錄。若要添加大量的函數,建議所有的邏輯被放置在一個獨立的庫單元。例如:
unit myfunctions; interface implementation uses SysUtils, Classes, fs_iinterpreter; // you can also add a reference to any other external library here type TFunctions = class(TfsRTTIModule) private function CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; var Params: Variant): Variant; public constructor Create(AScript: TfsScript); override; end; function MyFunc(s: String; i: Integer): Boolean; begin // required logic end; procedure MyProc(s: String); begin // required logic end; { TFunctions } constructor TFunctions.Create; begin inherited Create(AScript); with AScript do AddMethod('function MyFunc(s: String; i: Integer): Boolean', CallMethod, 'My functions', ' MyFunc function always returns True'); AddMethod('procedure MyProc(s: String)', CallMethod, 'My functions', ' MyProc procedure does not do anything''); end; end; function TFunctions.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String; var Params: Variant): Variant; begin if MethodName = 'MYFUNC' then Result := MyFunc(Params[0], Params[1]) else if MethodName = 'MYPROC' then MyProc(Params[0]); end; initialization fsRTTIModules.Add(TFunctions); end.
保存該文件,并將其擴展名命名為.pas。然后,添加索引到該文件夾。此時,你便可以在任何報表組件中使用這些自定義的函數,無需編寫代碼來將這些函數添加的每個“TfrxReport”組件,也無需為每個報表控件的 “onUser” 事件處理程序編寫額外的代碼。
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:慧都控件網