---------------- 使用自訂函數 ----------------------------------------
Q: 我怎樣添加我的自訂函數?
A: 使用 TfrReport.OnUserFunction 事件.
這裡有一個簡單的例子:
procedure TForm1.frReport1UserFunction(const Name: String;
p1, p2, p3: Variant; var val: Variant```````````````);
begin
if AnsiCompareText(‘SUMTOSTR‘, Name) = 0 then
val := My_Convertion_Routine(frParser.Calc(p1));
end;
然後,你就可以在報表(任何錶達式或指令碼)的任何地方使用 SumToStr 函數了。
Q: 但是它僅僅能工作在一個TfrReport組件中。可我想在任何地方(在所有的TfrReport組件中)使用的我的自訂函數?
A: 使 OnUserFunction event 控制代碼作為所有組件的公用控制代碼。如果你不能做到這一點,你需要建立函數庫:
type TMyFunctionLibrary = class(TfrFunctionLibrary)
public
constructor Create; override;
procedure DoFunction(Fno: Integer; p1, p2, p3: Variant;
var val: Variant); override;
end; constructor TMyFunctionLibrary.Create;
begin
inherited Create;
with List do
begin
Add(‘DATETOSTR‘);
Add(‘SUMTOSTR‘);
end;
end;
procedure TMyFunctionLibrary.DoFunction(Fno: Integer; p1, p2, p3: Variant;var val: Variant);
begin
val := 0;
case Fno of
0: val := My_DateConvertion_Routine(frParser.Calc(p1));
1: val := My_SumConvertion_Routine(frParser.Calc(p1));
end;
end; 要註冊函數庫,調用
frRegisterFunctionLibrary(TMyFunctionLibrary);
要卸載函數庫,調用 frUnRegisterFunctionLibrary(TMyFunctionLibrary);
Q: 我怎樣將我的函數添加到函數列表中 (用運算式產生器)?
A: 使用 frAddFunctionDesc 過程 (在FR_Class 單元中): frAddFunctionDesc(FuncLib, ‘SUMTOSTR‘, ‘My functions‘,
‘SUMTOSTR(<Number>)/Converts number to its verbal presentation.‘); 注意: "/" 符號是必須的! 它從它的描述中分隔函數文法。
FuncLib 被聲明為你自己的函數庫 (如果你不使用函數庫可以將其設定為nil). 當函數庫未註冊時,所有它的函數將自動從函數列表中刪除。
---------------- 使用變數 ------------------------------------- Q: 我怎樣編程實現填充變數列表(在資料詞典中)? A: 資料詞典中的所有變數及分類都被儲存在 TfrReport.Dictionary.Variables 中. with frReport1.Dictionary do
begin
// 建立分類(名稱用空白)
Variables[‘ New category‘] := ‘‘;
// 建立變數
Variables[‘New Variable‘] := ‘CustomerData.Customers."CustNo"‘;
Variables[‘Another Variable‘] := ‘Page#‘;
end; Q: 我定義了字串變數: with frReport1.Dictionary do
Variables[‘Month‘] := ‘March‘; 但是當我運行報表是,出現了錯誤,為什麼? A: 因為 FastReport 假定資料詞典中的字串變數值是一個運算式,它需要分析、計算它。
可以使用其它的方法: with frReport1.Dictionary do
Variables[‘Month‘] := ‘‘‘‘ + ‘March‘ + ‘‘‘‘; 或者, 使用 frVariables 來傳輸固定資料到報表。
Q: 我不想在資料詞典中顯示某些資料集?
A: 使用
TfrReport.Dictionary.DisabledDatasets: with frReport1.Dictionary do
begin
DisabledDatasets.Add(‘CustomerData.Bio‘);// 關閉該資料集
DisabledDatasets.Add(‘CustomerData*‘);// 或者, 關閉整個資料模組/表單
end;
Q: 我怎樣將資料傳送到報表?
A: 有幾個方法可以實現它.
第一是使用全域對象 frVariables (在 FR_Class 單元中被定義):
frVariables[‘My variable‘] := 10; 這段代碼建立了一個名稱為“My variable”,值為 10 的變數。這是最好的傳輸固定資料的報表的方法。
第二種方法是使用 TfrReport.OnGetValue 事件. 這可以使用這個方法來傳送動態資料、記錄等。
procedure TForm1.frReport1GetValue(ParName: String; var ParValue: Variant);
begin
if ParName = ‘MyField‘ then
ParValue := Table1MyField.Value;
end;
最後, 第三種方法是通過編程在資料詞典中定義變數(可以參考以前的問題):
with frReport1.Dictionary do
begin
Variables[‘MyVariable‘] := ‘CustomerData.Customers."CustNo"‘;
Variables[‘Another Variable‘] := ‘10‘;
end;
Q: 我能在報表和程式間傳送資料嗎?
A: 使用 frVariables 對象. 如果你在報表的任何對象的指令碼中寫入以下代碼: MyVariable := 10 那麼,在你的程式中,你可以使用以下代碼來擷取 MyVariable 的值:
v := frVariables[‘MyVariable‘];
---------------- 指令碼 (FastReport Pascal) ---------------------------------
Q: Band 中是否可以使用指令碼?
A: 當然. 選擇 band ,然後按 Ctrl+Enter 或在物件瀏覽器中選擇 "OnBeforePrint" 屬性。 Q: 報表頁中是否可以使用指令碼? A: 當然. 選擇頁 (在空白處單擊) ,然後在物件瀏覽器中選擇 "OnBeforePrint" 屬性。如果該頁是一個對話方塊表單,那麼這個屬性就是 "OnActivate".
Q: 我有兩個對象: Memo1 和 Memo2. 我能否在 Memo1 的指令碼中調用 Memo2 的屬性和方法?
A: 當然, 例如,你可以這樣做: 對象名.屬性名稱.
Q: 在指令碼中,我可以使用對象的哪些屬性?
A: 幾乎所有你能在物件瀏覽器中看到的屬性。例如,可以使用 Font.Name, Font.Size等來存取字型屬性。
Q: 怎樣改變多頁報表中某一頁的順序?
A: 拖動頁標籤到目的位置。
Q: 我想查看所有的欄位及變數,我想在報表中使用列表來實現它?
A: 設定 TfrReport.MixVariablesAndDBFields := True.現在,所有的資料欄位及變數可在“插入資料欄位”對話方塊中可存取了。
Q: 我不想顯示匯入選項對話方塊?
A: 在匯入組件(比如,TfrTextExport)中設定所有必需的選項,然後通過設定ShowDialog屬性為False來關閉此對話方塊。
Q: 為什麼 TotalPages 變數不起作用? 它總是返回 0.
A: 在你的報表中設定 Two-pass 選項. 要設定它,你需要在報表設計師的[檔案] 功能表中,開啟“報表選項”對話方塊。
Q: 我用BLOB欄位來儲存我的報表。當我運行報表設計師時,它顯示我的報表未命名?
A: 在運行報表設計師前,這樣做: frReport1.FileName:= ‘Name of my report‘;
Q: 我想在重新定義報表設計師中的“開啟”及“儲存”按鈕的功能?
A: 查看 TfrDesigner 組件. 它有幾個必需的事件: OnLoadReport 和OnSaveReport.
這裡有一小段代碼例子:
procedure TForm1.frDesigner1LoadReport(Report: TfrReport;var ReportName: String; var Opened: Boolean);
begin
with MyOpenDialog do
begin
Opened := ShowModal = mrOk;
if Opened then
begin
Report.LoadFromBlobField(…);
ReportName := …;
end;
end;
end;
procedure TForm1.frDesigner1SaveReport(Report: TfrReport;var ReportName: String; SaveAs: Boolean; var Saved: Boolean);
begin
if SaveAs then
with MySaveDialog do
begin
Saved := ShowModal = mrOk;
if Saved then
begin
Report.SaveToBlobField(…);
ReportName := …;
end;
end
else
Report.SaveToBlobField(…);
end;
Q: 在 QR 中, 我可以寫這樣的代碼: QRLabel1.Caption := ‘Some text‘. 我可以用FR這樣做嗎?
A: FR 對象並不是一個組件 (這並不像 QR, RB). 但使用 TfrReport.FindObject 方法可以通過對象名稱找到該對象。
vart: TfrMemoView;
begin
t := TfrMemoView(frReport1.FindObject(‘Memo1‘));
if t <> nil then
t.Memo.Text := ‘FastReport‘;
end;
Q: 我想在使用者預覽(TfrPreview組件)中自訂熱鍵?
A: 這個組件有個視窗: Tform 屬性. 將自訂控制代碼指定到 Window.OnKeyDown 屬性.
Q: Fast Report 2.4 不能裝載 FreeReport 2.21 檔案?
A: 這僅需要使用16進位數改變報表檔案的第一位元組,然後在原始碼中修改下面的部分。在這些修改之後, 裝載報表並儲存它.
最後,返回到原始碼處.
FR_Class: function ReadString(Stream: Tstream): String;
begin
if frVersion >= 23 then
Result := frReadString(Stream)
else
Result := frReadString22(Stream);
end;
procedure ReadMemo(Stream: Tstream; Memo: Tstrings);
begin
if frVersion >= 23 then
frReadMemo(Stream, Memo)
else
frReadMemo22(Stream, Memo);
end;
FR_Utils:
procedure frReadMemo(Stream: Tstream; l: Tstrings);
var s: String; b: Byte; n: Word;
begin
l.Clear;
l.Text := frReadString(Stream); exit;
Stream.Read(n, 2);
if n > 0 then
repeat
Stream.Read(n, 2);
SetLength(s, n);
Stream.Read(s[1], n);
l.Add(s);
Stream.Read(b, 1);
until b = 0
else
Stream.Read(b, 1);
end;
function frReadString(Stream: Tstream): String;
var s: String; n: Integer; b: Byte;
begin
Stream.Read(n, 4);
SetLength(s, n);
Stream.Read(s[1], n);
if (n > 0) and (s[n] = #$0A) then
SetLength(s, n - 2);
// Stream.Read(b, 1);
Result := s;
end;
Q: 怎樣不在預覽列印中列印報表?
A: 這裡有一段代碼: frReport1.PrepareReport;
frReport1.PrintPreparedReport(‘‘, 1, True, frAll);或frReport1.PrintPreparedReportDlg;
Q: 我想在報表中旋轉圖片。問題是這張圖片是由我的應用程式產生的。是否有方法可以在列印前將這幅圖片裝載到報表中?
A: 使用 TfrReport.OnBeforePrint
事件: if View.Name = ‘Picture1‘ then
TfrPictureView(View).Picture.LoadFromFile(…) 或.Assign 或.你所想要做的任何事情