fastscript增加三方控件

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.

 

posted @ 2017-02-10 15:12  delphi中间件  阅读(1176)  评论(0编辑  收藏  举报