FastReport VCL 表开发人员手册:自定义函数连接到 表

FastReport 内置了大量的标准函数,用于 表设计。

FastReport 内置了大量的标准函数,用于 表设计。还可以添加您自己的自定义功能。函数连接是使用FastReport中包含的“FastScript”脚本库接口进行的(要了解FastScript的更多信息,请参阅它的库手册)。

如何连接过程和/或函数的示例。函数的参数数量和类型可能会有所不同。不能传递“Set”和“Record”类型的参数,因为FastScript不支持它们。您必须传递更简单的类型的参数,例如传递TRect为X0, Y0, X1, Y1: Integer。查看更多关于在 FastScript 文档中添加具有不同参数的函数的过程。

在 Delphi 表单上声明函数或过程及其代码。

function TForm1.MyFunc(s: String; i: Integer): Boolean;begin// necessary logicend; procedure TForm1.MyProc(s: String);begin// necessary logicend;

为 告组件创建 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 方法添加到函数列表中(通常在 Delphi 表单的 on create 或 on show 事件中)。

  frxReport1.AddFunction('function MyFunc(s: String; i: Integer): Boolean');  frxReport1.AddFunction('procedure MyProc(s: String)'); 

现在可以在 告脚本中使用连接函数;此外,可以从TfrxMemoView类型对象中引用它。此功能也显示在“数据树”窗口的功能页选项卡中。在这个窗口中,函数被分成几类,当您选择任何函数时,窗口底部窗格中会出现有关该函数的提示。

修改上面的代码示例,将函数注册到单独的类别中,并显示函数描述提示:

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'); 

您添加的项目现在将显示在“我的功能”类别下。

如果要在现有类别之一中注册函数,请使用以下类别名称之一:
‘ctString’——字符串函数;
‘ctDate’ – 日期/时间函数;
‘ctConv’ – 转换函数;
‘ctFormat’ – 格式化;
‘ctMath’ – 数学函数;
‘ctOther’ – 其他功能。

如果指定了空白类别名称,则函数将放置在函数树根中。

如果您希望添加大量函数,建议将所有逻辑放在一个单独的库单元中。下面是一个例子:

unit myfunctions;interfaceimplementationuses SysUtils, Classes, fs_iinterpreter;// you can also add a reference to any other external library heretype  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// necessary logicend; procedure MyProc(s: String);begin// necessary logicend; { 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 扩展名保存文件,然后在 Delphi 项目表单的 uses 子句中添加对它的引用,您的所有自定义函数将可用于任何 表组件。无需编写代码将这些函数添加到每个TfrxReport,也无需为每个 表组件的“onuserfunction”处理程序编写额外的代码。

如果您对 FastReport 感兴趣,欢迎加入 FastReport QQ 交流群:702295239

还想要更多吗可以点击阅读【FastReport 表2021最新资源盘点】查找需要的教程资源。上是FastReport .NET慧正在 火热销售中!>>查看价格详情

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年6月23日
下一篇 2021年6月23日

相关推荐

发表回复

登录后才能评论