FastScript脚本使用白皮书(Ver1.0)

〖转载〗FastScript脚本使用白皮书(Ver1.0)

 

1.  以使用的对象如下:

1.1.  原始类:

Tobject, TPersistent  ,TList, TStrings, TStringList, TStream, TFileStream, TMemoryStream, TComponent,  TfsXMLItem, TfsXMLDocument.

A)  Tobject:

方法:

 constructor Create

 Free

 function ClassName: String

B)TPersistent class(Tobject);

方法:

 procedure Assign(Source: TPersistent)

C)  TList class  (TObject)

方法:

 function Add(Item: TObject): Integer'

 procedure Clear', CallMethod);

 procedure Delete(Index: Integer)

 function IndexOf(Item: TObject)

 procedure Insert(Index: Integer; Item: TObject)

 function Remove(Item: TObject): Integer

属性:

 Count: Integer

 Items[i: Integer]: TObject'

D)  TStrings: class(TPersistent)

 function Add(const S: string): Integer

 function AddObject(const S: string; AObject: TObject): Integer

 procedure Clear', CallMethod);

 procedure Delete(Index: Integer)', CallMethod);

 function IndexOf(const S: string): Integer', CallMethod);

 function IndexOfName(const Name: string): Integer

 function IndexOfObject(AObject: TObject): Integer

 procedure Insert(Index: Integer; const S: string)

 procedure InsertObject(Index: Integer; const S: string; AObject:  TObject)

 procedure LoadFromFile(const FileName: string)

 procedure LoadFromStream(Stream: TStream)

 procedure SaveToFile(const FileName: string)

 procedure SaveToStream(Stream: TStream)

属性:

CommaTextstring

CountInteger

 Names', 'Integer', 'string', CallMethod, True);

 Objects', 'Integer', 'TObject',;

 Values', 'String', 'string',;

 Strings[i: Integer', 'string',

 'Text', 'string', GetProp, SetProp);

end;

E)TStringList:  class(TStrings)

方法:

function Find(s:  String; var Index: Integer): Boolean

 procedure Sort', CallMethod)

属性:

 Duplicates: TDuplicates

 SortedBoolean

F) TStreamclass(TObject)

方法:

function  Read(Buffer: string; Count: Longint): Longint

 function Write(Buffer: string; Count: Longint): Longint

 function Seek(Offset: Longint; Origin: Word): Longint

 function CopyFrom(Source: TStream; Count: Longint): Longint

 属性:

 PositionLongint

 SizeLongint

G)  TFileStreamclass('TStream')

 constructor Create(Filename: String; Mode: Word)

H)  TMemoryStreamclass('TStream')

 procedure Clear', CallMethod);

 procedure LoadFromStream(Stream: TStream)

 procedure LoadFromFile(Filename: String)

 procedure SaveToStream(Stream: TStream)

 procedure SaveToFile(Filename: String)

I)  TComponentclass(TPersistent)

方法:

constructor  Create(AOwner: TComponent)

 属性:

OwnerComponent

J) TfsXMLItem=class(TObject)

方法:

 constructor Create

 procedure AddItem(Item: TfsXMLItem)

 procedure Clear', CallMethod)

 procedure InsertItem(Index: Integer; Item: TfsXMLItem)

 function Add: TfsXMLItem

 function Find(const Name: String): Integer

 function FindItem(const Name: String):

 function Prop(const Name: String): String

 function Root: TfsXMLItem

属性:

 DataInteger

 CountInteger

 Items[i: Integer]: TfsXMLItem

 NameString

 ParentTfsXMLItem

 TextString

K)  TfsXMLDocument=class('TObject')

 方法:

 constructor Create

 procedure SaveToStream(Stream: TStream)

 procedure  LoadFromStream(Stream: TStream)

 procedure SaveToFile(const FileName: String)

 procedure LoadFromFile(const FileName: String)

 属性:

RootfsXMLItem'

-------------------------------------------------------------------------------------

1.2.  常用类:(如果需要使用此类控件需要加fsFormsRTTI1控件)

控件:

TWinControl类(TWinControl:  Class(Tcontrol)

TEdit,TMemo,Tmaskedit,TCombobox,TCheckBox,TRadioButton,TBitbtn,  TListBox,TForm, TTScrollBox

TControl类 (TControl:  Class(TComponent)

TSpeedbutton  Ttoolbar  Tpanel  TbitbtnTLabel  TGroupBox  TImageTShapeTToolbar

TComponent

TDataModule

------------------------------------------------------------------------------------

说明:

对于TControl

方法:

procedure  Hide

procedure Show

procedure SetBounds(ALeft,  ATop, AWidth, AHeight: Integer)', CallMethod);

事件:

 OnCanResize

OnClick

OnDblClick

OnMouseDown

OnMouseMove

OnMouseUp

OnResize

对于TWinControl除了有上面TControl的方法和事件外,还有:

方法:

procedure  SetFocus

事件:

OnEnter

 OnExit

 OnKeyDown

 OnKeyPress

 OnKeyUp

对于TCombobox还有如下事件:

OnChange

OnDropDown

OnCloseUp

除了有Published属性外还有如下属性:

 DroppedDownBoolean

 ItemIndexInteger

对于TEdit,TMemo还有如下方法:

procedure  CopyToClipboard

procedure  CutToClipboard

procedure  PasteFromClipboard

对于TlistBox除了有Published属性外还有如下属性:

 ItemIndexInteger

 SelCountinteger

 Selected[Index: Integer]:  Boolean

对于Form类还有:

方法:

procedure  Close

 procedure Hide

 procedure Show

 function ShowModal:  Integer

事件:

 OnActivate

 OnClose

 OnCloseQuery

 OnCreate

 OnDestroy

 OnDeactivate

 OnHide

 OnPaint

 OnShow

除了有Published属性外还有如下属性

Canvas

 ModalResult

---------------------------------------------------------------------------------------------------------------------

1.3.  数据敏感类:(如果需要使用此类控件需要加fsDBCtrlRTTI1控件)

控件:

TWinControl

TDBEditTDBComboBoxTDBImageTDBCheckBoxTDBListBoxTDBMemoTDBNavigatorTDBRadioGroupTDBLookupControlTDBLookupListBoxTDBLookupComboBoxTDBGrid

TGraphicControl(fsFormsRTTI1里定义  AddClass(TGraphicControl, 'TControl')

TDBText

TPersistent类:

TColumnTitleTColumnTDBGridColumns

说明:

对于TDBCheckBox除了有Published属性外还有如下属性:

 CheckedBoolean

对于TDBComboBox

除了有Published属性外还有如下属性:

 TextString

 DroppedDownBoolean

 ItemIndexInteger

除了Twincontrol事件外,还有如下事件:

 OnChange

 OnDropDown

 OnCloseUp

对于TDBListBox除了有Published属性外还有如下属性:

 ItemIndexInteger

 SelCountinteger

 Selected[Index: Integer]:  Boolean

对于TDBRadioGroup除了有Published属性外还有如下属性:

 ItemIndexInteger

 ValueString

对于TDBLookupListBox除了有Published属性外还有如下属性:

 KeyValueVariant

 SelectedItemString

对于TDBLookupComboBox除了有Published属性外还有如下属性:

 KeyValueVariant

 TextString

对于TDBGridColumns

除了有Published属性外还有如下属性:

 Items[iInteger]Tcolumn

方法:

 function Add: TColumn

 function Count: Integer

 procedure RebuildColumns

 procedure RestoreDefaults

1.4.  字段和数据集类:(  如果需要使用此类控件需要加fsDBRTTI1控件)

A)  TField  class(TComponent)

属性:

 AsBooleanBoolean

 AsCurrencyCurrency

 AsDateTimeTDateTime

 AsFloatDouble

 AsIntegerInteger

 AsStringString

 AsVariantVariant

 DataTypeTFieldType

 DisplayNameString

 DisplayTextString

 IsNullBoolean

 SizeInteger

 ValueVariant

B)  TFieldsclass(TObject)

属性:

Fields[i:  Integer]TField

C)  TStringField: class(TField)

D)  TNumericField: class(TField)

E)  TIntegerField: class(TNumericField)

F)  TSmallIntField: class(TIntegerField)

G)  TWordField: class(TIntegerField)

H)  TAutoIncField: class(TIntegerField)

I)  TFloatField: class(TNumericField)

J)  TCurrencyField: class(TFloatField)

K)  TBooleanField: class(TField)

L)  TDateTimeField: class(TField)

M)  TDateField: class(TDateTimeField)

N)  TTimeField: class(TDateTimeField)

O)  TBinaryField: class(TField)

P)  TBytesField : class(TBinaryField)

Q)  TVarBytesField: class(TBinaryField)

R)  TBCDField: class(TNumericField)

S)  TBlobField: class(TField)

方法:

 procedure LoadFromFile(const FileName: String)

 procedure LoadFromStream(Stream: TStream)

 procedure SaveToFile(const FileName: String)

 procedure SaveToStream(Stream: TStream)

T)  TMemoFieldclass(TBlobField)

U)  TGraphicField: (TBlobField)

V)  TFieldDef: class(TPersistent)

W)  TFieldDefs: class(TObject)

 function AddFieldDef: TFieldDef', CallMethod);

 function Find(const Name: string): TFieldDef', CallMethod);

 procedure Add(const Name: string; DataType: TFieldType; Size: Word; Required:  Boolean)

 procedure Clear

 procedure Update

属性:

 Items[iInteger]  TFieldDef

X)  TDataSource  class(TComponent)

Y)  TDataSet: TComponent

方法:

 procedure Open

 procedure Close

 procedure First

 procedure Last

 procedure Next

 procedure Prior

 procedure Cancel

 procedure Delete

 procedure Post

 procedure Append

 procedure Insert

 procedure Edit

function  FieldByName(const FieldName: string): TField

 procedure GetFieldNames(List: TStrings)

 function FindFirst: Boolean

 function FindLast: Boolean

 function FindNext: Boolean

 function FindPrior: Boolean', CallMethod);

 procedure FreeBookmark(Bookmark: TBookmark)

 function GetBookmark: TBookmark

 procedure GotoBookmark(Bookmark: TBookmark)', CallMethod);

 function Locate(const KeyFields:  string; const KeyValues: Variant;' +

 'Options: TLocateOptions): Boolean

 function IsEmpty: Boolean

属性:

 BofBoolean

 EofBoolean

 fieldCountInteger

 RecordCountInteger

 FieldDefsTFieldDefs

 FieldsTFields

 Filterstring

 FilteredBoolean

 FilterOptionsTFilterOptions

 ActiveBoolean

事件:

BeforeOpen

 AfterOpen;

 BeforeClose

 AfterClose

 BeforeInsert

 AfterInsert

 BeforeEdit

AfterEdit

 BeforePost

 AfterPost

 BeforeCancel

 AfterCancel

 BeforeDelete

 AfterDelete

 BeforeScroll

 AfterScroll

 OnCalcFields

 OnFilterRecord

 OnNewRecord

Z)  TParamclass(TPersistent)

方法:

 procedure Clear

属性:

AsBooleanBoolean

 AsCurrencyCurrency

 AsDateTimeTDateTime

 AsFloatDouble

 AsIntegerInteger

 AsDateTDate

 AsTimeTTime

 AsStringString

 BoundBoolean

 IsNullBoolean

 TextString

AA) TParamsclass(TPersistent)

方法:

function  ParamByName(const Value: string): TParam

事件:

function  FindParam(const Value: string): TParam

属性:

Item[i:  Integer]TParam

1.5.  ADO类:(  如果需要使用此类控件需要加fsADORTTI1控件)

TDataBase,TDataSource,TDataSet,TAdoconnection,TAdoDataSet,TAdoquery,TAdoTable,TAdoStoredProc,TClientDataSet

A)  TADOConnectionTComponent

B)  TParameterTPersistent

属性:

Items[iInteger]TParameter

C)  TCustomADODataSet:  class(TDataSet)

D)  TADODataSetclass(TCustomADODataSet)

E)  TCustomClientDataSetclass(TDataSet)

F)  TADOTableclass(TCustomADODataSet);

G)  TADOQueryclass(TCustomADODataSet') do

方法:

procedure  ExecSQL

H)  TADOStoredProcTCustomADODataSet

方法:

procedure  ExecProc

I)  TClientDataSet:  class(TCustomClientDataSet

方法:

procedure  Excute

1.6.  BDE类:(如果需要使用此类控件需要加fsBDERTTI1控件)

 TTable,Tquery,TDataBase,TStoreProc,TSession

对于TDataBase可以使用如下方法:

 procedure Open

对于TTable可以使用如下方法:

 procedure CreateTable

 procedure DeleteTable;

 procedure EmptyTable;

 function FindKey(const KeyValues: array): Boolean;

 procedure FindNearest(const KeyValues: array);

 procedure RenameTable(const NewTableName: string);

对于TQuery可以使用如下方法:

 procedure ExecSQL

 function ParamByName(const Value: string): TParam

 procedure Prepare

 ParamCount

对于TStoredProc可以使用如下方法:

 procedure ExecProc

 function ParamByName(const Value: string): TParam

 procedure Prepare

 ParamCount

1.7.  对话类:(如果需要使用此类控件需要加fsDialogRTTI1控件)

TOpenDialogTSaveDialogTColorDialog  TFontDialogTPrintDialogTPrinterSetupDialog

function  InputBox(ACaption, APrompt, ADefault: string): string

function  InputQuery(ACaption, APrompt: string; var Value: string): Boolean

1.8.  图像类:

控件:

TFontTPenTBrushTCanvasTGraphicTMetafileTimageTMetafileCanvasTBitmap

对于TCanvas:  TPersistent,除了TPersistent的方法外还有如下方法:

procedure  Draw(X, Y: Integer; Graphic: TGraphic)

 procedure Ellipse(X1, Y1, X2, Y2: Integer)

 procedure LineTo(X, Y: Integer)

 procedure MoveTo(X, Y: Integer)

 procedure Rectangle(X1, Y1, X2, Y2: Integer)

 procedure RoundRect(X1, Y1, X2, Y2, X3, Y3: Integer)

 procedure StretchDraw(X1, Y1, X2, Y2: Integer; Graphic: TGraphic)

 function TextHeight(const Text: string)Integer

 procedure TextOut(X, Y: Integer; const Text: string)

 function TextWidth(const Text: string)Integer

 PixelsX,  Y: Integer;):TColor

对于TGraphic=  class(TPersistent)

除了TPersistent的方法外,还有如下

   方法:

 procedure LoadFromFile(const Filename:  string)', CallMethod);

 procedure SaveToFile(const Filename:  string)', CallMethod);

   属性:

HeightInteger

 WidthInteger

对于TBitmap:  TGraphic

除了TPersistent的属性外,还有如下属性:

Canvas:  TCanvas

2.  常用函数:(系统默认可以使用下列函数)

转换类:

IntToStr(i:  Integer): String

FloatToStr(e:  Extended): String

DateToStr(e:  Extended): String

TimeToStr(e:  Extended): String

DateTimeToStr(e:  Extended): String

VarToStr(v:  variant): String

StrToInt(s:  String): Integer

StrToFloat(s :  String): Float

StrToDate(s:  String): TDate

StrToTime(s:  String): TTime

StrToDateTime(s:String):  TDateTime

格式化类:

Format(Fmt: String;  Args: array): String

FormatFloat(Fmt:  String; Value: Extended): String

FormatDateTime(Fmt:  String; DateTime: TDateTime): String

 FormatMaskText(EditMask: String; Value: String): String

时间类:

function  EncodeDate(Year, Month, Day: Word): TDateTime

procedure  DecodeDate(Date: TDateTime; var Year, Month, Day: Word)

function  EncodeTime(Hour, Min, Sec, MSec: Word): TDateTime

procedure  DecodeTime(Time: TDateTime; var Hour, Min, Sec, MSec: Word)

function  Date: TDateTime

function Time: TDateTime

function Now: TDateTime

function  DayOfWeek(aDate: DateTime): Integer

function  IsLeapYear(Year: Word): Boolean

function DaysInMonth(nYear, nMonth:  Integer): Integer

字符串类:

function  Length(s: String): Integer

function  Copy(s: String; from, count: Integer): String

function  Pos(substr, s: String): Integer

procedure  Delete(var s: String; from, count: Integer): String

procedure  Insert(s: String; var s2: String; pos: Integer): String'

function  Uppercase(s: String): String

function  Lowercase(s: String): String

function  Trim(s: String): String

function  NameCase(s: String): String

function  CompareText(s, s1: String): Integer

function  Chr(i: Integer): Char

function  Ord(ch: Char): Integer

procedure SetLength(var S: String; L:  Integer)

数学类:

function Round(e:  Extended): Integer

function  Trunc(e: Extended): Integer

function  Int(e: Extended): Integer

function  Frac(X: Extended): Extended

function  Sqrt(e: Extended): Extended

function  Abs(e: Extended): Extended

function  Sin(e: Extended): Extended

function  Cos(e: Extended): Extended

function  ArcTan(X: Extended): Extended

function  Tan(X: Extended): Extended

function  Exp(X: Extended): Extended

function  Ln(X: Extended): Extended

function Pi: Extended

其他:

procedure Inc(var  i: Integer; incr: Integer = 1)

procedure  Dec(var i: Integer; decr: Integer = 1)

procedure  RaiseException(Param: String)

procedure  ShowMessage(Msg: Variant)

procedure  Randomize

function  Random: Extended

function  ValidInt(cInt: String): Boolean

function  ValidFloat(cFlt: String): Boolean

function ValidDate(cDate: String):  Boolean

说明:

由于FastScript解释执行脚本,所以很多对象或者对象的属性都没有包括,也就是在Delphi中有的对象或属性、方法等在脚本里面可能不能用,以上面所述为准。但是对于大多数情况下,FastScript能满足,且能较方便地进行扩展。

1.在FastScript中,任何类都能用属于Published的属性,如果又加了属性,则也可以用。

2.子类继承父类的所有方法和属性。也就是子类可以使用父类的所有方法和属性。

如果有加了方法和事件,也一样可以用

编者:袁锋

   日期:  2004-06-05

附:

A.关于如何使用第三方控件,增加方法、属性、事件)

举例如下:

如:有一控件为edtbutton:TedtButton,我们需要在动态脚本中使用该控件。我们采用如下方法:

我们可以把该控件申明在fs_iformsrtti单元里面(当然也可以申明在其他的单元如fs_idbrtti里面,但是遵守一个原则是尽量使得功能相似的控件放在同一个单元里面,这样只需要把该单元所对应的控件拖动到form上即可,提高系统运行效率)

如:fs_iformsrtti单元对应控件板上的fsiformsrtti。以此类推

AddClass(TedtButton,  'TControl');

对于增加方法:请看如下例子:

如需要增加Tedit类的CopyToClipboardCutToClipboardPasteFromClipboard方法。则代码如下所示:

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

对于增加属性:请看如下例子:

如需要增加TdataSetRecordCount属性,则代码如下所示:

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.

你可能感兴趣的:(fastreport,fastscript)