ActiveX (ii) Js listens for events in ActiveX

Source: Internet
Author: User

In the previous essay: ActiveX (a) The first simple demo, JS one-way call ActiveX methods can be implemented, in many cases, the execution of methods in ActiveX is relatively time-consuming and performance-intensive. In this case, the ActiveX method executes using the asynchronous policy, then how can JS get asynchronous execution results when the method is asynchronous? The scheme is nothing but two kinds, one is rotation, one is notify.

What if NOTIFY,JS is listening to its events? This will be the next focus of this essay:

Continue with the previous demo, assuming that the technical requirements are as follows: When you press ENTER, the ActiveX password in the Password box is actively pushed to JS (using the Notify mechanism).

In general development, the code for ActiveX is as follows:

[Guid ("30a3c1b8-9a9a-417a-9e87-80d0ee827658")]     Public Partial classPasswordcontrol:usercontrol { Public Delegate voidNotifyHandler (stringpwd); /// <summary>        ///listen for this event in JS/// </summary>         Public EventNotifyHandler onnotify;  PublicPasswordcontrol () {InitializeComponent (); }         Public stringgetpwd () {return  This. Txtpwd.text; }        Private voidTxtpwd_keypress (Objectsender, KeyPressEventArgs e) {            if(Keys) E.keychar = =keys.enter) {//Enter trigger event                if( This. OnNotify! =NULL)                {                     This. OnNotify ( This. Txtpwd.text); }            }        }    }

We only need to listen to the corresponding events in Js, can complete the technical requirements.

However, after trying, can not hear the ActiveX events, so Google a bit and found a solution.

1) Add a new interface,

2) Add a new GUID for the new interface (cannot be the same as the Passwordcontrol GUID )

3) Add attribute: [InterfaceType (Cominterfacetype.interfaceisidispatch)]

4) Add the same signature method as the event that needs to be monitored.

That is, the code that resembles the following is formed:

    [Guid ("fda0a081-3d3b-4eab-ae01-6a40fdda9a60")]    [InterfaceType (Cominterfacetype.interfaceisidispatch)]      Public Interface icomevents    {        [DispID (0x00000001)]        void onnotify (string  pwd );    }

5) Add properties for Passwordcontrol: [ComSourceInterfaces (typeof (Icomevents))]

That is, as shown below:

    [ComSourceInterfaces (typeof(icomevents))]    [Guid ("30a3c1b8-9a9a-417a-9e87-80d0ee827658")]      Public Partial class Passwordcontrol:usercontrol    {        //... Other code    }    

6) The JS end listens for events in the following way:

    <script language="Javascript"Event="onnotify (pwd)"  for ="activiex">        function activiex::onnotify (pwd) {   (" Password:" + pwd);        }     </script>

Event: Specifies the monitored events

For: Specifies the Listener object

The JS function name uses: [Listener Object]::[Listener event], note, middle is two colons.

In this way, JS can be in the Password box to enter the time, received the password of the ActiveX push. Isn't it simple?

There are two points to note:

1, the event of the corresponding delegate need to customize, do not use the system action<t>

2,JS function name use: [Listener Object]::[Listener event] form, the middle is two colon.

Test Project Source:

(not to be continued ...)

ActiveX (ii) Js listens for events in ActiveX

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: 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.