---------------- 使用自定義函數(shù) ----------------------------------------
Q: 我怎樣添加我的自定義函數(shù)?
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;
然后,你就可以在報表(任何表達(dá)式或腳本)的任何地方使用 SumToStr 函數(shù)了。
Q: 但是它僅僅能工作在一個TfrReport組件中??晌蚁朐谌魏蔚胤剑ㄔ谒械腡frReport組件中)使用的我的自定義函數(shù)?
A: 使 OnUserFunction event 句柄作為所有組件的公用句柄。如果你不能做到這一點,你需要創(chuàng)建函數(shù)庫:
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;
要注冊函數(shù)庫,調(diào)用
frRegisterFunctionLibrary(TMyFunctionLibrary);
要卸載函數(shù)庫,調(diào)用
frUnRegisterFunctionLibrary(TMyFunctionLibrary);
Q: 我怎樣將我的函數(shù)添加到函數(shù)列表中 (用表達(dá)式生成器)?
A: 使用 frAddFunctionDesc 過程 (在FR_Class 單元中):
frAddFunctionDesc(FuncLib, ‘SUMTOSTR‘, ‘My functions‘,
‘SUMTOSTR(<Number>)/Converts number to its verbal presentation.‘);
注意: "/" 符號是必須的! 它從它的描述中分隔函數(shù)語法。
FuncLib 被聲明為你自己的函數(shù)庫 (如果你不使用函數(shù)庫可以將其設(shè)置為nil). 當(dāng)函數(shù)庫未注冊時,所有它的函數(shù)將自動從函數(shù)列表中刪除。
---------------- 使用變量 -------------------------------------
Q: 我怎樣編程實現(xiàn)填充變量列表(在數(shù)據(jù)詞典中)?
A: 數(shù)據(jù)詞典中的所有變量及分類都被存儲在 TfrReport.Dictionary.Variables 中.
with frReport1.Dictionary do
begin
// 創(chuàng)建分類(名稱用空白)
Variables[‘ New category‘] := ‘‘;
// 創(chuàng)建變量
Variables[‘New Variable‘] := ‘CustomerData.Customers."CustNo"‘;
Variables[‘Another Variable‘] := ‘Page#‘;
end;
Q: 我定義了字符串變量:
with frReport1.Dictionary do
Variables[‘Month‘] := ‘March‘;
但是當(dāng)我運(yùn)行報表是,出現(xiàn)了錯誤,為什么?
A: 因為 FastReport 假定數(shù)據(jù)詞典中的字符串變量值是一個表達(dá)式,它需要分析、計算它。
可以使用其它的方法:
with frReport1.Dictionary do
Variables[‘Month‘] := ‘‘‘‘ + ‘March‘ + ‘‘‘‘;
或者, 使用 frVariables 來傳輸固定數(shù)據(jù)到報表。
Q: 我不想在數(shù)據(jù)詞典中顯示某些數(shù)據(jù)集?
A: 使用 TfrReport.Dictionary.DisabledDatasets:
with frReport1.Dictionary do
begin
// 關(guān)閉該數(shù)據(jù)集
DisabledDatasets.Add(‘CustomerData.Bio‘);
// 或者, 關(guān)閉整個數(shù)據(jù)模塊/窗體
DisabledDatasets.Add(‘CustomerData*‘);
end;
Q: 我怎樣將數(shù)據(jù)傳送到報表?
A: 有幾個方法可以實現(xiàn)它. 第一是使用全局對象 frVariables (在 FR_Class 單元中被定義):
frVariables[‘My variable‘] := 10;
這段代碼創(chuàng)建了一個名稱為“My variable”,值為 10 的變量。這是最好的傳輸固定數(shù)據(jù)的報表的方法。
第二種方法是使用 TfrReport.OnGetValue 事件. 這可以使用這個方法來傳送動態(tài)數(shù)據(jù)、記錄等。
procedure TForm1.frReport1GetValue(ParName: String; var ParValue: Variant);
begin
if ParName = ‘MyField‘ then
ParValue := Table1MyField.Value;
end;
最后, 第三種方法是通過編程在數(shù)據(jù)詞典中定義變量(可以參考以前的問題):
with frReport1.Dictionary do
begin
Variables[‘MyVariable‘] := ‘CustomerData.Customers."CustNo"‘;
Variables[‘Another Variable‘] := ‘10‘;
end;
Q: 我能在報表和程序間傳送數(shù)據(jù)嗎?
A: 使用 frVariables 對象. 如果你在報表的任何對象的腳本中寫入以下代碼:
MyVariable := 10
那么,在你的程序中,你可以使用以下代碼來獲取 MyVariable 的值:
v := frVariables[‘MyVariable‘];
---------------- 腳本 (FastReport Pascal) ---------------------------------
Q: Band 中是否可以使用腳本?
A: 當(dāng)然. 選擇 band ,然后按 Ctrl+Enter 或在對象瀏覽器中選擇 "OnBeforePrint" 屬性。
Q: 報表頁中是否可以使用腳本?
A: 當(dāng)然. 選擇頁 (在空白處單擊) ,然后在對象瀏覽器中選擇 "OnBeforePrint" 屬性。如果該頁是一個對話框窗體,那么這個屬性就是 "OnActivate".
Q: 我有兩個對象: Memo1 和 Memo2. 我能否在 Memo1 的腳本中調(diào)用 Memo2 的屬性和方法?
A: 當(dāng)然, 例如,你可以這樣做: 對象名.屬性名.
Q: 在腳本中,我可以使用對象的哪些屬性?
A: 幾乎所有你能在對象瀏覽器中看到的屬性。例如,可以使用 Font.Name, Font.Size等來存取字體屬性。
---------------- 其它問題 --------------------------------------------
Q: 怎樣改變多頁報表中某一頁的順序?
A: 拖動頁標(biāo)簽到目的位置。
Q: 我想查看所有的字段及變量,我想在報表中使用列表來實現(xiàn)它?
A: 設(shè)置 TfrReport.MixVariablesAndDBFields := True.現(xiàn)在,所有的數(shù)據(jù)字段及變量可在“插入數(shù)據(jù)字段”對話框中可存取了。
Q: 我不想顯示導(dǎo)入選項對話框?
A: 在導(dǎo)入組件(比如,TfrTextExport)中設(shè)置所有必需的選項,然后通過設(shè)置ShowDialog屬性為False來關(guān)閉此對話框。
Q: 為什么 TotalPages 變量不起作用? 它總是返回 0.
A: 在你的報表中設(shè)置 Two-pass 選項. 要設(shè)置它,你需要在報表設(shè)計器的“文件”菜單中,打開“報表選項”對話框。
Q: 我用BLOB字段來存儲我的報表。當(dāng)我運(yùn)行報表設(shè)計器時,它顯示我的報表未命名?
A: 在運(yùn)行報表設(shè)計器前,這樣做:
frReport1.FileName := ‘Name of my report‘;
Q: 我想在重新定義報表設(shè)計器中的“打開”及“保存”按鈕的功能?
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 方法可以通過對象名稱找到該對象。
var
t: TfrMemoView;
begin
t := TfrMemoView(frReport1.FindObject(‘Memo1‘));
if t <> nil then
t.Memo.Text := ‘FastReport‘;
end;
Q: 我想在用戶預(yù)覽(TfrPreview組件)中自定義熱鍵?
A: 這個組件有個窗口: Tform 屬性. 將自定義句柄指定到 Window.OnKeyDown 屬性.
Q: Fast Report 2.4 不能裝載 FreeReport 2.21 文件?
A: 這僅需要使用16進(jìn)制數(shù)改變報表文件的第一字節(jié),然后在源代碼中修改下面的部分。在這些修改之后, 裝載報表并保存它. 最后,返回到源代碼處.
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: 怎樣不在打印預(yù)覽中打印報表?
A: 這里有一段代碼:
frReport1.PrepareReport;
frReport1.PrintPreparedReport(‘‘, 1, True, frAll);
或
frReport1.PrintPreparedReportDlg;
Q: 我想在報表中旋轉(zhuǎn)圖片。問題是這張圖片是由我的應(yīng)用程序生成的。是否有方法可以在打印前將這幅圖片裝載到報表中?
A: 使用 TfrReport.OnBeforePrint 事件:
if View.Name = ‘Picture1‘ then
TfrPictureView(View).Picture.LoadFromFile(…) 或
.Assign 或
.你所想要做的任何事情
(出處:www.delphibbs.com)