8.4.3 Automation another program
Each server in the system registration has a key called ProgID, mainly used to identify the server controller. Any controller can use the ProgID number to create an OLE object instance. Routine AutoForm is a controller program that creates an instance of an OLE object in its main form.
Procedure Tmainform.formcreate (Sender:tobject);
Begin
Try
Memoedit: = Createoleobject (' memoedit.application ');
Except
Messagedlg (
' An instance of the ' Memoedit application OLE automation Class could
Not to created,make sure that memoedit application has been registered
Using a "memoedit|regserver" command line,
mterror,[mbok],0)
Halt;
End
End
Once the controller has created an OLE Automation object instance, it can manipulate it. OLE automatic objects include properties and methods, although OLE Automation objects are not the same concept as objects in object-oriented Pascal, but Delphi allows calls to methods of OLE objects using similar syntax.
Many of the AutoForm procedures refer to the methods of OLE Automation objects:
Procedure Tmainform,tilebuttonclick (Sender:tobject);
Begin
Memoedit,tilewindow;
End
Where Tilewindows is the method defined in the OLE object Tmemoapp.
AutoForm also obtains a reference to the Tmemodoc of the OLE object inside the server through the Newmemo method of Tmemoapp.
Procedure Tmainform,createbuttonclick (Sender:tobject);
Var
I:integer;
Begin
Closememo
For I: = 1-3 do memos[2]: = Memoedit.newmemo;
End
The Newmemo is defined in the Memoauto unit as follows:
function IMemoApp.NewMemo:Variant;
Begin
Result: = Mainform,creatememo ('), OleObject;
End
After the controller obtains the internal OLE object for the server, it can refer to its method:
Procedure Tmainform.addtextbuttonclick (Sender,tobject);
Var
I:integer;
Begin
For I: = 1 to 3 do
If not Var isempty (Memo[i]) then
memo[i],insert{' This text is added through OLE automation ' #13 #10);
End
Insert is a method defined in Tmemodoc to insert a string into a subform.