fastscript增加三方控件

A.关于如何使用第三方控件,增加方法、属性、事件)
举例如下:
如:有一控件为edtbutton:TedtButton,我们需要在动态脚本中使用该控件。我们采用如下方法:
我们可以把该控件申明在fs_iformsrtti单元里面(当然也可以申明在其他的单元如fs_idbrtti里面,但是遵守一个原则是尽量使得功能相似的控件放在同一个单元里面,这样只需要把该单元所对应的控件拖动到form上即可,提高系统运行效率)
如:fs_iformsrtti单元对应控件板上的fsiformsrtti。以此类推
AddClass(TedtButton, 'TControl');

对于增加方法:请看如下例子:
如需要增加Tedit类的CopyToClipboard、CutToClipboard、PasteFromClipboard方法。则代码如下所示:
with AddClass(TEdit, 'TWinControl') do
begin
AddMethod('procedure CopyToClipboard', CallMethod);
AddMethod('procedure CutToClipboard', CallMethod);
AddMethod('procedure PasteFromClipboard', CallMethod);
end;

在 CallMethod中需要增加相应方法的实现。
function TFunctions.CallMethod(Instance: TObject; ClassType: TClass;
const MethodName: String; var Params: Variant): Variant;
var
Form: TCustomForm;
begin
Result := 0;
if ClassType = TControl then
begin
if MethodName = 'HIDE' then
TControl(Instance).Hide
else if MethodName = 'SHOW' then
TControl(Instance).Show
else if MethodName = 'SETBOUNDS' then
TControl(Instance).SetBounds(Params[0], Params[1], Params[2], Params[3])
end
else if ClassType = TWinControl then
begin
if MethodName = 'SETFOCUS' then
TWinControl(Instance).SetFocus
end
else if ClassType = TEdit then //需要增加的实现(只是对于Tedit);
begin
if MethodName = uppercase('CopyToClipboard') then
Tedit(Instance).CopyToClipboard ;
if MethodName = uppercase('CutToClipboard') then
Tedit(Instance).CutToClipboard ;
if MethodName = uppercase('PasteFromClipboard') then
Tedit(Instance).PasteFromClipboard ;
end
End

对于增加属性:请看如下例子:
如需要增加TdataSet的RecordCount属性,则代码如下所示:
with AddClass(TDataSet, 'TComponent') do
begin
AddMethod('procedure Open', CallMethod);
……
AddProperty('FieldCount', 'Integer', GetProp, nil);
AddProperty('RecordCount', 'Integer',GetProp,nil); 因为RecordCount属性只有读没有写。
AddProperty('Active', 'Boolean', GetProp, SetProp);既能读又能写。
End

如果有写过程,则需要在 GetProp过程中增加相应属性的实现。

function TFunctions.GetProp(Instance: TObject; ClassType: TClass;
const PropName: String): Variant;
begin
…….
if ClassType = TField then
begin
……
Result := _TField.Size
else if PropName = 'VALUE' then
Result := _TField.Value
……
end
else if ClassType = TDataSet then
begin
……
else if PropName = 'FIELDCOUNT' then
Result := _TDataSet.FieldCount
else if PropName = 'RECORDCOUNT' then
Result := _TDataSet.RecordCount
……
end。

procedure TFunctions.SetProp(Instance: TObject; ClassType: TClass;
const PropName: String; Value: Variant);
……
if ClassType = TField then
begin
_TField := TField(Instance);
if PropName = 'ASBOOLEAN' then
_TField.AsBoolean := Value
else if PropName = 'ASCURRENCY' then
_TField.AsCurrency := Value
else if PropName = 'ASDATETIME' then
_TField.AsDateTime := Value
else if PropName = 'ASFLOAT' then
_TField.AsFloat := Value
else if PropName = 'ASINTEGER' then
_TField.AsInteger := Value
else if PropName = 'ASSTRING' then
_TField.AsString := Value
else if PropName = 'ASVARIANT' then
_TField.AsVariant := Value
else if PropName = 'VALUE' then
_TField.Value := Value
end
else if ClassType = TDataSet then
begin
_TDataSet := TDataSet(Instance);
if PropName = 'FILTER' then
_TDataSet.Filter := Value
else if PropName = 'FILTERED' then
_TDataSet.Filtered := Value
else if PropName = 'FILTEROPTIONS' then
_TDataSet.FilterOptions := IntToFilterOptions(Value)
else if PropName = 'ACTIVE' then
_TDataSet.Active := Value
end
……

B.调用Delphi过程:

1. 先创建事件处理方法:TfsCallMethodEvent
2. 然后再用调用TfsScript.AddMethod方法,第一个参数为Delphi方法的语法,第二个参数为TfsCallMethodEvent链接的一个句柄。
如在Delphi有一个过程为DelphiFunc,
…..
procedure TForm1.DelphiFunc(s: String; i: Integer);
begin
ShowMessage(s + ', ' + IntToStr(i));
end;

{TfsCallMethodEvent}
function TForm1.CallMethod(Instance: TObject; ClassType: TClass; const MethodName: String;
var Params: Variant): Variant;
begin
if MethodName = 'DELPHIFUNC' then //注意方法名称都为大写比较。
DelphiFunc(Params[0], Params[1]);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
{ clear all items }
fsScript1.Clear;
{ script text }
fsScript1.Lines := Memo1.Lines;
{ frGlobalUnit contains standard types and functions }
fsScript1.Parent := fsGlobalUnit;
{ make DelphiFunc procedure visible to a script }
fsScript1.AddMethod('procedure DelphiFunc(s: String; i: Integer)', CallMethod);

{ compile the script }
if fsScript1.Compile then
fsScript1.Execute else { execute if compilation was succesfull }
ShowMessage(fsScript1.ErrorMsg); { show an error message }
end;

C.调用FastScript过程:与调用Delphi函数类似。
举例说明:如果在动态脚本里面有一个'ScriptFunc'的一个过程,则在delphi代码里需要写如下:
fsScript1.Clear;
{ script text }
fsScript1.Lines := Memo1.Lines;
{ frGlobalUnit contains standard types and functions }
fsScript1.Parent := fsGlobalUnit;
{ make DelphiFunc procedure visible to a script }

{ compile the script }
if fsScript1.Compile then
{ Call script function with one string parameter and one integer param }
fsScript1.CallFunction('ScriptFunc', VarArrayOf(['Call ScriptFunc', 1])) else
ShowMessage(fsScript1.ErrorMsg); { show an error message }
end;

例如动态脚本内容如下:
procedure ScriptFunc(Msg: String; Num: Integer);
begin
ShowMessage('1st param: ' + Msg +
' 2nd param: ' + IntToStr(Num));
end;

begin
DelphiFunc('Call DelphiFunc', 1);
end.

最新文章

  1. shell 从1加到100
  2. Bugtags 远程配置功能介绍
  3. C语言回顾-运算符和循环
  4. Python--matplotlib绘图可视化知识点整理
  5. [codeforces 528]B. Clique Problem
  6. https大势已来?看腾讯专家如何在高并发压测中支持https
  7. JavaScript与Java的区别
  8. javascript 高级程序设计学习笔记(面向对象的程序设计)继承
  9. @Scheduled(cron = "0 0 * * * ?")实现定时任务
  10. React Native之AppRegistry模块
  11. BZOJ_4016_[FJOI2014]最短路径树问题_最短路+点分治
  12. 基于C#的钉钉SDK开发(1)--对官方SDK的重构优化
  13. hihoCoder1033 交错和 数位DP
  14. exception ‘PHPExcel_Calculation_Exception‘ with message ‘粉丝数据!C2679 -> Formula Error: Operator ‘=‘ has no operands
  15. 《Linux内核设计与实现》第三章学习笔记
  16. ext.js打印出提示弹窗(鼓捣了两天终于尼玛出来了)
  17. 背水一战 Windows 10 (49) - 控件(集合类): Pivot, Hub
  18. 冒泡排序的C、C++实现
  19. 日志收集-Flume-ng-mongodb-sink
  20. 解决问题E: 无法获得锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)

热门文章

  1. LightOj:1422-Halloween Costumes
  2. 对java多线程的一些浅浅的理解
  3. CodeForces 8D Two Friends 判断三个圆相交
  4. WebService的简介, 原理, 使用,流程图
  5. iOS-----5分钟学会枚举的正确使用姿势-Enumeration宏
  6. Swagger Edit自动生成代码工具
  7. UVa——400Unix ls(字典序文本处理输出iomanip)
  8. BZOJ2285 [SDOI2011]保密 【01分数规划 + 网络流】
  9. hdu 1558 线段相交+并查集路径压缩
  10. Nginx配置https双向认证