Eighth chapter-Object Linking and Embedding (ii) (2)

Source: Internet
Author: User
Tags definition ole

8.4 OLE Automation

OLE Automation is a mechanism for Windows applications to manipulate another program. OLE 2.0 provides a way to integrate applications, which is the command operation between applications.

With OLE 2.0, programmers can define a set of commands to get them into other programs. These commands can be taken with parameters. It looks much like an application is calling a function or procedure. This approach enables the interaction of two applications without the participation of the person.

An automated program is called an Automation object or Automation server, and an application that operates or automates another program is called an Automation controller or an Automation client.

Delphi2.0 fully supports OLE2.0 application automation and can write automation controllers and services with Delphi 2.0

Manager The potential to be programmable between applications is enormous. Users can create macros or other commands that enable an application to work through other applications. The macro language of an existing application can easily be extended to include commands and function calls that a group of applications can perform.

Here are two applications, where MEMOEDIT.DPR is a text editor for a multiple-document interface, and as an OLE Automation server, AUTOFROM.DPR is an Automation controller. Run AutoForm before, in the Delphi Integrated Development Environment Click menu (Run | parameters), Delphi Pop-up Run Parameters dialog box, such as Figure 8.5, input parameters after running state as shown in Figure 8.6. AutoForm multiple buttons for a form. Memoedit can be manipulated, such as pressing the creat button, Memoedit generates three subforms, as shown in Figure 8.7, "addtext", the subform will appear "This text is added through OLE Automation" string "

Memoedit consists of three modules:

Mainfrom MDI main form

Editfrom MDE child forms and automation classes

Memoauto Application Automation objects

The following routines describe the basic concepts and development of OLE Automation.

8.4.1 Tautoobject Object

Tautoobject is the base class for all objects in the Delphi Automation server, and any automation objects are derived from the Tautoobject class.

The definition of an OLE object is similar to the definition of another class. Its automated part is like the public part of the ordinary class, which can refer to the properties and methods declared in this section. The compiler creates the automated part into the portal of OLE Automation objects. But the automated part of the code has many limitations:

A property method can be defined, but cannot define a field;

All properties, parameters, and function types must be one of the following types:

Smallint,integer,single,double,currency,tdatetime,string,wordbool, Varint

A property declaration can only include an access definition character (read and Write), and other definitions such as index,stored.

Default,odefault are not available;

The access definition must list the corresponding method identifier and cannot use the domain identifier;

Supports array types;

Property overloading is not allowed;

Methods can be virtual, but not dynamic, allowing method overloading.

The Tmemodoc class is defined in the Editfrom unit:

Type

Tmemodoc = Class (tautoobject)

Private

Feditform:teditform;

Funtion cretfilename:string;

Funtion Cretmodified:wordbool;

Procedure Setfilename (Const value:string);

Automated

Procedure clear;

Procedure Ineart (Const text:string);

Procedure Save;

Procedure close;

Procedure Filename:string Read Gretfilename write

Setfilename;

Procedure Modified:wordbool Read gretmodified

End

The Tmemedoc class is an internal automation class for Memoedit programs and therefore does not require registration. The external OLE Automation controller cannot refer directly to it. If you want the external controller to operate on an Automation object, call Automation in the cell that declares the automation object. RegisterClass for registration. The routine Memoauto cell defines the Tmemoapp object and registers it.

Unit Memoauto

...

Type

Tmemoapp = Class (tautoobject)

Implementation

...

Procedure Registermemoapp

Const

Autoclassinfo:tautoclassinfo = (

Autoclass:tmemoapp;

Progid:memoedit,application

Classin: ' {fiff4880-200d-11cf-bdcf-d020afoe5b81} ';

Description: ' Memo Editor application ';

Instancing:acsingle Instance);

Begin

Automation,registerclass (Autoclassinfo)

End

Inibialization

Registermemoapp;

End

Automation objects are registered with the Automation object in the initialization section. Registered information is used to uniquely identify the server object. This information is used to add an Automation object to the server. Once the program registers an Automation object, the global Automation object is automatically managed with the OLE Automation API.

The registered OLE Automation object is a reference to the count, because the object may be controlled by multiple controllers. When you are finished using an OLE object, call the release method, which reduces the number of references and, when the number of references is zero, calls the free method to dispose of the object.

The OLE object is typically exported as a Variant type (variants), and any OLE object's methods and properties must return a Variant type that contains an OLE object, Tautoobject provides a variant type of OLEObject property. Instead of directly getting the class or pointer in the server, the controller refers to the OLEObject property of the OLE object.

The Newmemo function of a routine memoauto cell is to provide an interface that references a Tmemodoc object by referencing the OLEObject property.

function tmemoapp,newmemo:variant;

Begin

Result: = Mainform,creatememo ('), OleObject;

End

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.