demo_intf.htm
来自「Delphi脚本控件」· HTM 代码 · 共 294 行
HTM
294 行
<html>
<head>
<link rel=stylesheet type="text/css" href="styles.css">
</head>
<body>
<h3>
Importing Interface types.
</h3>
<hr>
<h4>
Delphi code:
</h4>
<blockquote>
<pre>
<font color="blue"><b>unit</b></font> Intf;
<font color="blue"><b>interface</b></font>
<font color="blue"><b>uses</b></font>
SysUtils, Classes, Dialogs;
<font color="blue"><b>type</b></font>
ITest = <font color="blue"><b>interface</b></font>
[<font color="Red">'{E7AA427A-0F4D-4A96-A914-FAB1CA336337}'</font>]
<font color="blue"><b>function</b></font> GetProp: <font color="blue"><b>String</b></font>;
<font color="blue"><b>procedure</b></font> SetProp(Value: <font color="blue"><b>String</b></font>);
<font color="blue"><b>procedure</b></font> Proc; <font color="blue"><b>overload</b></font>;
<font color="blue"><b>procedure</b></font> Proc(A: Integer); <font color="blue"><b>overload</b></font>;
<font color="blue"><b>procedure</b></font> Proc2; <font color="blue"><b>cdecl</b></font>;
<font color="blue"><b>property</b></font> Prop: <font color="blue"><b>String</b></font> <font color="blue"><b>read</b></font> GetProp <font color="blue"><b>write</b></font> SetProp;
<font color="blue"><b>end</b></font>;
TTest = <font color="blue"><b>class</b></font>(TInterfacedObject, ITest)
<font color="blue"><b>private</b></font>
fStr: <font color="blue"><b>String</b></font>;
<font color="blue"><b>function</b></font> GetProp: <font color="blue"><b>String</b></font>;
<font color="blue"><b>procedure</b></font> SetProp(Value: <font color="blue"><b>String</b></font>);
<font color="blue"><b>public</b></font>
<font color="blue"><b>constructor</b></font> Create;
<font color="blue"><b>destructor</b></font> Destroy; <font color="blue"><b>override</b></font>;
<font color="blue"><b>procedure</b></font> Proc; <font color="blue"><b>overload</b></font>;
<font color="blue"><b>procedure</b></font> Proc(A: Integer); <font color="blue"><b>overload</b></font>;
<font color="blue"><b>procedure</b></font> Proc2; <font color="blue"><b>cdecl</b></font>;
<font color="blue"><b>property</b></font> Prop: <font color="blue"><b>String</b></font> <font color="blue"><b>read</b></font> GetProp <font color="blue"><b>write</b></font> SetProp;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>implementation</b></font>
<font color="blue"><b>function</b></font> TTest.GetProp: <font color="blue"><b>String</b></font>;
<font color="blue"><b>begin</b></font>
result := fStr;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TTest.SetProp(Value: <font color="blue"><b>String</b></font>);
<font color="blue"><b>begin</b></font>
fStr := Value;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TTest.Proc;
<font color="blue"><b>begin</b></font>
ShowMessage(fstr);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TTest.Proc(A: Integer);
<font color="blue"><b>begin</b></font>
ShowMessage(IntToStr(A));
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TTest.Proc2;
<font color="blue"><b>begin</b></font>
ShowMessage(<font color="Red">'2'</font>);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>constructor</b></font> TTest.Create;
<font color="blue"><b>begin</b></font>
fStr := <font color="Red">'abc'</font>;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>destructor</b></font> TTest.Destroy;
<font color="blue"><b>begin</b></font>
ShowMessage(<font color="Red">'Done'</font>);
<font color="blue"><b>inherited</b></font>;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>end</b></font>.
</pre>
</blockquote>
<h4>
Code, generated by paxScript Importer:
</h4>
<blockquote>
<pre>
<font color="blue"><b>unit</b></font> IMP_Intf;
<font color="blue"><b>interface</b></font>
<font color="blue"><b>uses</b></font>
SysUtils,
Classes,
Dialogs,
Intf,
BASE_SYS,
BASE_EXTERN,
PaxScripter;
<font color="blue"><b>procedure</b></font> RegisterIMP_Intf;
<font color="blue"><b>implementation</b></font>
<font color="blue"><b>procedure</b></font> TTest_Proc3(MethodBody: TPAXMethodBody);
<font color="blue"><b>begin</b></font>
<font color="blue"><b>with</b></font> MethodBody <font color="blue"><b>do</b></font>
TTest(Self).Proc();
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TTest_Proc4(MethodBody: TPAXMethodBody);
<font color="blue"><b>begin</b></font>
<font color="blue"><b>with</b></font> MethodBody <font color="blue"><b>do</b></font>
TTest(Self).Proc(Params[0].AsInteger);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TTest_Proc2(MethodBody: TPAXMethodBody);
<font color="blue"><b>begin</b></font>
<font color="blue"><b>with</b></font> MethodBody <font color="blue"><b>do</b></font>
TTest(Self).Proc2();
<font color="blue"><b>end</b></font>;
<font color="blue"><b>function</b></font> TTest__GetProp(Self:TTest):<font color="blue"><b>String</b></font>;
<font color="blue"><b>begin</b></font>
result := Self.Prop;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TTest__PutProp(Self:TTest;<font color="blue"><b>const</b></font> Value: <font color="blue"><b>String</b></font>);
<font color="blue"><b>begin</b></font>
Self.Prop := Value;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> RegisterIMP_Intf;
<font color="blue"><b>var</b></font> H: Integer;
<font color="blue"><b>begin</b></font>
H := RegisterNamespace(<font color="Red">'Intf'</font>, -1);
// <font color="blue"><b>Begin</b></font> <font color="blue"><b>of</b></font> <font color="blue"><b>interface</b></font> ITest
RegisterInterfaceType(<font color="Red">'ITest'</font>,ITest,<font color="Red">'IUnknown'</font>,IUnknown,H);
RegisterInterfaceMethod(ITest,
<font color="Red">'function GetProp: String;'</font>);
RegisterInterfaceMethod(ITest,
<font color="Red">'procedure SetProp(Value: String);'</font>);
RegisterInterfaceMethod(ITest,
<font color="Red">'procedure Proc; overload;'</font>);
RegisterInterfaceMethod(ITest,
<font color="Red">'procedure Proc(A: Integer); overload;'</font>);
RegisterInterfaceMethod(ITest,
<font color="Red">'procedure Proc2; cdecl;'</font>);
RegisterInterfaceProperty(ITest,<font color="Red">'property Prop: String read GetProp write SetProp;'</font>);
// <font color="blue"><b>End</b></font> <font color="blue"><b>of</b></font> <font color="blue"><b>interface</b></font> ITest
// <font color="blue"><b>Begin</b></font> <font color="blue"><b>of</b></font> <font color="blue"><b>class</b></font> TTest
RegisterClassType(TTest, H);
RegisterMethod(TTest,
<font color="Red">'constructor Create;'</font>,
@TTest.Create);
RegisterMethod(TTest,
<font color="Red">'destructor Destroy; override;'</font>,
@TTest.Destroy);
RegisterStdMethodEx(TTest,<font color="Red">'Proc'</font>,TTest_Proc3,0,[typeVARIANT]);
RegisterStdMethodEx(TTest,<font color="Red">'Proc'</font>,TTest_Proc4,1,[typeINTEGER,typeVARIANT]);
RegisterStdMethodEx(TTest,<font color="Red">'Proc2'</font>,TTest_Proc2,0,[typeVARIANT]);
RegisterMethod(TTest,
<font color="Red">'function TTest__GetProp(Self:TTest):String;'</font>,
@TTest__GetProp, true);
RegisterMethod(TTest,
<font color="Red">'procedure TTest__PutProp(Self:TTest;const Value: String);'</font>,
@TTest__PutProp, true);
RegisterProperty(TTest,
<font color="Red">'property Prop:String read TTest__GetProp write TTest__PutProp;'</font>);
// <font color="blue"><b>End</b></font> <font color="blue"><b>of</b></font> <font color="blue"><b>class</b></font> TTest
<font color="blue"><b>end</b></font>;
<font color="blue"><b>initialization</b></font>
RegisterIMP_Intf;
<font color="blue"><b>end</b></font>.
</pre>
</blockquote>
<h4>
Demo application:
</h4>
<blockquote>
<pre>
<font color="blue"><b>unit</b></font> Unit1;
<font color="blue"><b>interface</b></font>
<font color="blue"><b>uses</b></font>
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, BASE_SYS, BASE_PARSER, PaxScripter, PaxPascal, StdCtrls,
Intf, IMP_Intf;
<font color="blue"><b>type</b></font>
TForm1 = <font color="blue"><b>class</b></font>(TForm)
Button1: TButton;
PaxScripter1: TPaxScripter;
PaxPascal1: TPaxPascal;
<font color="blue"><b>procedure</b></font> Button1Click(Sender: TObject);
<font color="blue"><b>procedure</b></font> PaxScripter1AssignScript(Sender: TPaxScripter);
<font color="blue"><b>procedure</b></font> FormCreate(Sender: TObject);
<font color="blue"><b>private</b></font>
{ <font color="blue"><b>Private</b></font> declarations }
hostobj: ITest;
<font color="blue"><b>public</b></font>
{ <font color="blue"><b>Public</b></font> declarations }
<font color="blue"><b>end</b></font>;
<font color="blue"><b>var</b></font>
Form1: TForm1;
<font color="blue"><b>implementation</b></font>
{$R *.dfm}
<font color="blue"><b>uses</b></font>
IMP_SysUtils;
<font color="blue"><b>procedure</b></font> GetIntf(<font color="blue"><b>out</b></font> Obj: ITest);
<font color="blue"><b>begin</b></font>
obj := TTest.Create;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TForm1.Button1Click(Sender: TObject);
<font color="blue"><b>begin</b></font>
hostobj := TTest.Create;
PaxScripter1.Run;
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TForm1.PaxScripter1AssignScript(Sender: TPaxScripter);
<font color="blue"><b>begin</b></font>
Sender.AddModule(<font color="Red">'1'</font>, <font color="Red">'paxPascal'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'uses SysUtils, Intf;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'var I: ITest;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'var X: TTest;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'procedure P;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'var L: ITest;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'begin'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' hostobj.QueryInterface(IUnknown, L);'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' print <font color="Red">"RefCount = "</font> + IntToStr(IntfRefCount(hostobj));'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' print <font color="Red">"RefCount = "</font> + IntToStr(IntfRefCount(L));'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' hostobj.Proc();'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' hostobj := nil;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' print <font color="Red">"RefCount = "</font> + IntToStr(IntfRefCount(L));'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'end;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'P();'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'X := TTest.Create;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'if X.GetInterface(ITest, I) then'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'begin'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' print <font color="Red">"RefCount = "</font> + IntToStr(IntfRefCount(I));'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' I.Proc();'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' I.Proc(10);'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' I.Proc2();'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' I.Prop := <font color="Red">"pqr"</font>;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">' print I.Prop;'</font>);
Sender.AddCode(<font color="Red">'1'</font>, <font color="Red">'end;'</font>);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>procedure</b></font> TForm1.FormCreate(Sender: TObject);
<font color="blue"><b>begin</b></font>
PaxScripter1.RegisterInterfaceVar(<font color="Red">'hostobj'</font>, @ hostobj, ITest);
<font color="blue"><b>end</b></font>;
<font color="blue"><b>initialization</b></font>
RegisterRoutine(<font color="Red">'procedure GetIntf(out obj: ITest);'</font>, @GetIntf);
<font color="blue"><b>end</b></font>.
</pre>
</blockquote>
<p>
<HR>
<font size = 1 color ="gray">
Copyright © 1999-2005
VIRT Laboratory. All rights reserved.
</font>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?