Deiphi writing the WPS Office plugin (II)

Source: Internet
Author: User

using Delphi to develop Wpsoffice plug-in (three); (response to various events of WPS); Wpsoffice's three major Components , in order to facilitate Two-time development ,; One, WPS event interface; Wpsoffice events are provided in the form of an interface; wps_tlb. PAS, with applicatio;[' {000209fe-0000-4b30-a9;procedurestartup;dispid1;procedu

(Response to WPS for various events)

WPS Office three components , in order to facilitate two development , have reserved the event interface, different programs have different events, by responding to different events, developers can develop different functions of the plug-in program. Today we are going to learn how to respond to various events of WPS, examples of WPS text, the other two components you can explore on your own.

One, the WPS event interface

The WPS Office event is provided in the form of an interface, with the WPS text as an example, and its interface is declared in the

Wps_tlb. In Pas, there are applicationevents interfaces and documentevents interfaces, which are declared and described as follows: ApplicationEvents = dispinterface

[' {000209fe-0000-4b30-a977-d214852036fe} ']

Procedure Startup; DispID 1;//triggered at startup

Procedure Quit; DISPID 2;//Trigger when exiting

Procedure DocumentChange; DISPID 3;//trigger when document changes

Procedure DocumentOpen (const DOC: _document); DISPID 4;//trigger when opening a document

Procedure DocumentBeforeClose (const DOC: _document; var cancel:wordbool); DISPID 6;//Trigger before closing document

Procedure DocumentBeforePrint (const DOC: _document; var cancel:wordbool); DispID 7;//before printing a document

Procedure DocumentBeforeSave (const DOC: _document; var saveasui:wordbool; var cancel:wordbool); DISPID 8;//trigger before saving a document

Procedure NewDocument (const DOC: _document); DISPID 9;//New document is triggered

Procedure WindowActivate (const DOC: _document; const Wn:window); DispID triggered when 10;//window is activated

Procedure WindowDeactivate (const DOC: _document; const Wn:window); DispID 11;//window loses focus when triggered

Procedure WindowSelectionChange (const sel:selection); DispID 12;//document selection is changed

Procedure Windowbeforerightclick (const sel:selection; var cancel:wordbool); DispID 13;//Trigger before right click

Procedure Windowbeforedoubleclick (const sel:selection; var cancel:wordbool); DispID 14;//trigger before double-clicking event

Procedure WindowSize (const DOC: _document; const Wn:window); DISPID 25;//window size changes when the trigger

End

DocumentEvents = dispinterface

[' {000209f6-0000-4b30-a977-d214852036fe} ']

Procedure New; DispID 4;//When creating a new document

Procedure Open; DISPID 5;//Open Document Trigger

Procedure Close; DispID 6;//when closing a document

End

So how do we get our plugin to respond to it? This is a complex problem, it requires us to write an event receiver class to hook up the event interface, and then connect to the application's application interface, and finally write the response process in the plug-in can be. Because the process is more complex, so we use the already packaged class, it can simplify our writing process, I will provide this series of packaged unit files, mainly have Wpsevents.pas, etevents. PAS wppevents. PAS, Ksoevents. Pas four, each unit corresponding to a component , kso corresponding to the common toolbar related events, the specific situation you can view the source code , we can download the use.

Second, plug-in function and implementation process

The plug-in we are writing today implements two functions, one is to move the cursor to the end of the document when a new document is opened, (by default, at the beginning of the document), and when a new document is created, the document's creation date is automatically added to the header of the document, and the current page number and total pages are added at the footer. To implement these two functions, we will respond to the WPS document opening event (DocumentOpen) and the new document event (NewDocument) to include our implementation code in both events.

Follow the previous steps to create the plug-in framework , and then declare the event sink object and the response procedure. The two procedure and sink objects we want to respond to are declared as follows:

Procedure Ondocumentopen (sender:tobject; const DOC: _document); Procedure OnNewDocument (sender:tobject; const DOC: _document); twpsapp:twpsapplicationevents;

Where Doc is the document object you want to open or create, you can use it to manipulate the document. In the plug-in connection event, we create the event sink object and connect it to the Application object, and then hook up the event to our defined process. Finally, remember to release the object we created when the plugin is disconnected. The specific implementation details are as follows:

Procedure twpsaddin.onconnection (const application:idispatch; Connectmode:ext_connectmode; Const Addininst:idispatch; var custom:psafearray);

Begin

Fapp:=application as _application;

twpsapp:=twpsapplicationevents.create;//creating a sink object instance

Twpsapp.connect (Fapp);//Connect to Application interface

twpsapp.documentopen:=ondocumentopen;//hooking events to the process of our declaration twpsapp.newdocument:=onnewdocument;

End

Procedure twpsaddin.ondisconnection (RemoveMode:

Ext_disconnectmode; var custom:psafearray);

Begin

twpsapp.disconnect;//disconnecting

fapp:=nil;//Release Interface

End

(Refer to the detailed WPS API documentation for the invocation and use of various interface objects of WPS)

The implementation code for the first feature is very simple and requires just one line of code, as follows:

Procedure Twpsaddin.ondocumentopen (sender:tobject; const DOC: _document);

Begin

Fapp.Selection.EndKey (wpsstory,wpsmove);//move the cursor to the end.

End

The second function implementation is somewhat complex, need to have more understanding of the WPS API, the specific code is as follows, you can refer to, to facilitate your study :

Procedure Twpsaddin.onnewdocument (sender:tobject; const DOC: _document);//New Document Event

var Rng:range;

Begin

Rng:=doc.sections.item (1). Headers.item (wpsheaderfooterprimary). range;//getting the header area

Set the header text to the current date

Rng. Text:=formatdatetime (' created in yyyy m month D-Day Hh:nn:ss ', now); Set alignment left-justified

Rng. Paragraphs.alignment:=wpsalignrowleft;

Get Footer Area

Rng:=doc.sections.item (1). Footers.item (wpsheaderfooterprimary). Range;

Rng. Fields.Add (Rng,wpsfieldpage, ", true);//Add page Number field

Rng. InsertBefore (' first ');//Add the word "first" in front

Rng. Expand (wpsparagraph);//Extended area to full segment

Rng. InsertAfter (' page total ');//Insert after paragraph

Rng. Collapse (wpscollapseend);//Shrink area to end of paragraph

Rng. Fields.Add (Rng,wpsfieldnumpages, ", true);//Insert Total number of pages field rng. Expand (wpsparagraph);//Extended area to full segment

Rng. InsertAfter (' page ');//Insert after paragraph

Set Alignment to center

Rng. Paragraphformat.alignment:=wpsalignparagraphcenter;

Set text to Bold

Rng. Bold:=1;

End;

After the completion of the plugin, do not forget to write the plug-in configuration file, without it will not be installed to the plug-in platform. Since the plugin did not create any interface elements, we did not see changes on the toolbar after installation (which can be seen in the plugin platform, of course), but we saw the changes when we opened a document or created a new document. The focus of this section is on the event receiver Object (twpsdocumentevents), which encapsulates all the application events, the response of the document event is to be used (twpsdocumentevents) objects, and they work the same way. That is, create the sink object, connect to the application interface, and hook up the event interface into the corresponding method of this COM object. The corresponding method is declared in wpsevents:

Tapplicationdocumentopen = procedure (sender:tobject; const DOC: _document) of object;

Tapplicationdocumentbeforeclose = procedure (sender:tobject; const DOC: _document; var cancel:wordbool) of object;

Tapplicationdocumentbeforeprint = procedure (sender:tobject; const DOC: _document; var cancel:wordbool) of object;

Tapplicationdocumentbeforesave = procedure (sender:tobject; const DOC: _document; var Saveasui:wordbool; var Cancel:word Bool) of object;

Tapplicationnewdocument = procedure (sender:tobject; const DOC: _document) of object;

Tapplicationwindowactivate = procedure (sender:tobject; const DOC: _document; const wn:window) of object;

Tapplicationwindowdeactivate = procedure (sender:tobject; const DOC: _document; const wn:window) of object;

Tapplicationwindowselectionchange = procedure (sender:tobject; const sel:selection) of object;

Tapplicationwindowbeforerightclick = procedure (sender:tobject; const sel:selection; var cancel:wordbool) of object;

Tapplicationwindowbeforedoubleclick = procedure (sender:tobject; const sel:selection; var cancel:wordbool) of object;

Tapplicationwindowsize = procedure (sender:tobject; const DOC: _document; const wn:window) of object;

You can follow the declaration to write the required events to your plug-in program, after hooking up can be used.

We can try it.

End

Deiphi writing the WPS Office plugin (II)

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.