Implementation of the Automation client based on Visual C + +

Source: Internet
Author: User
Tags numeric value

I. Overview

We are in the process of writing programs, developing software, if you can take advantage of the functions of existing programs, it can greatly reduce the programmer's workload in the development process, while achieving a multiplier effect. For example, in the project, many software needs word processing function, although MFC provides some methods, but the specific implementation of both trouble, and some difficulties, if we can directly use Office to provide functionality, not beautiful! For this to happen, it's easy to use ActiveX automation technology. This article mainly describes the concept of automation, and through an example of the operation of Word documents to help readers understand how to automate VC programming.

Second, automation

1. The concept of automation

In Windows program development, automation is based on com/dcom technology: It enables an application to manipulate objects in another program, using the rich functionality provided by the program, or an application Programs allow another program to use the various functions it provides by providing methods and properties for objects and objects. Which, depending on the principle of whether the program is a beneficiary or a boon, the application that provides the object and object property methods is called the Automation server side; An application that uses an object property method is called an automated client. The Automation server lets other programs tell it what to do, and it reveals functions and data called methods (Methods) and attributes (properties). For example, members in the Microsoft Office suite, such as Word, Excel, and so on, are automation servers. Depending on the location of the automated client and server side, automation can be divided into two categories:

1 if the server-side and the client's two different applications are on the same computer, called local automation;

2 If the server-side and the client's two different applications are network-based, on separate computers, called Remote Automation.

The reader's attention is that, because of the use of COM technology, the client can not directly access the server-side object to achieve access to it, it must by getting a pointer to the interface to use the functionality provided by the object. This interface, called IDispatch, is a simplified special interface that uses a variety of different languages (including languages such as VB that cannot use pointers). This part of the content readers can be in the following implementation of the process to savor.

When it comes to automation, you can't just mention the ActiveX control, which is actually a minimal automation server loaded into the process. This means that they perform extremely quickly, formerly known as OLE custom controls, to replace the 16-bit VBX controls used in VB and VC. Because an ActiveX control is typically saved as an. ocx file, it is also called an OCX control. Because the controls are a miniaturized Automation server, they need to be used by the Automation client, and we refer to the client application as a container application. ActiveX controls contain events, in addition to properties and methods, that trigger an event, such as a user's click, when something needs to be noticed by the container application.

2. Data types in Automation

One of the most important issues in implementing automated programming is the programmer's understanding of how the data is delivered on the automated client and server side. How to provide a unified security mechanism to achieve data delivery? On the VC development platform, it provides a data type called Variant to solve this problem. The data type is composed of two parts, the first part is the type of data, and the second part is the numeric value of the specific data. Based on the Variant data type, VC provides the COleVariant class, which encapsulates the variant data type, which means that objects of the COleVariant class can be used wherever the variant is used. In the operation date and currency variables, VC also provides two classes: COleCurrency and COleDateTime, specific content, readers can refer to MSDN.

Third, the operation Word example

The example in this article demonstrates how to automate word implementation, mainly to assist in explaining how automation is implemented in Visual C + + programming, to help readers have a perceptual understanding of the automated implementation process, so for simplicity, the function of the program is simply to select a Word2000 *. Doc document and display its contents in the view of the program. To do this, you need to start Word to open the files selected by the user, select all and Clipboard operations, and then paste the data into view.

In the concrete implementation of this example, our application is an automated client and Word is an automated server side. First generate a new single document application, give the project a name of "KK", depending on the base class selection CEditView, because many of the operations in Office are implemented by the VBA language, so we in the process of using automation to operate word, in order to understand the working process of Word, We want to know about VBA, which can be achieved by recording macros in Word. The steps to record a macro are as follows: Start Word2000, select the Record Macro submenu under the Tools menu, name a new macro as Macro1, and start recording the macro, a floating bar appears in the window and click on the button above to stop or pause the recording of the macro. Here is the macro code that implements the operation to open a Word document, select all the contents of the document, and store the data on the Clipboard:

' Sub Macro1()
'
Documents.Open FileName:="基于 Visual C.doc", ConfirmConversions:=False, _
ReadOnly:=False, AddToRecentFiles:=False, PasswordDocument:="", _
PasswordTemplate:="", Revert:=False, WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:=wdOpenFormatAuto
Selection.WholeStory
Selection.Copy
End Sub

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.