ActiveX (2) Js listens to ActiveX events and jsactivex

Source: Internet
Author: User

ActiveX (2) Js listens to ActiveX events and jsactivex

 

In the previous article: ActiveX (1) in the First simple Demo, JavaScript can be used to call methods in ActiveX in one way, in many cases, the execution of ActiveX methods is time-consuming and performance-consuming. In this case, ActiveX method execution uses an asynchronous policy. How does js obtain the asynchronous execution result when the method is asynchronous? There are only two solutions, one is round training and the other is round y.

 

How does Js listen to events in the case of policy? This will be the focus of this article:

Continue to the previous Demo. Assume that the technical requirements are as follows: When press enter, ActiveX actively pushes the password in the password box to Js (using the Notify mechanism ).

In general development, the ActiveX Code is as follows:

[Guid ("30a3c1b8-9a9a-440a-9e87-80d0ee827658")] public partial class PasswordControl: UserControl {public delegate void policyhandler (string pwd ); /// <summary> /// listen to this event in js /// </summary> public event policyhandler ony y; public PasswordControl () {InitializeComponent ();} public string GetPwd () {return this.txt Pwd. text;} private void txtPwd_KeyPress (object sender, KeyPressEventArgs e) {if (Keys) e. KeyChar = Keys. Enter) {// press Enter to trigger the event if (this. OnNotify! = Null) {this.onpolicy(this.txt Pwd. Text );}}}}

 

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

However, after trying, we couldn't monitor events in ActiveX, So Google found a solution.

1) Add a new interface,

2) Add a new GUID for The New Interface (it cannot be the same as the GUID of PasswordControl)

3) add attributes: [InterfaceType (ComInterfaceType. InterfaceIsIDispatch)]

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

That is, the following code is formed:

    [Guid("FDA0A081-3D3B-4EAB-AE01-6A40FDDA9A60")]    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]    public interface IComEvents    {        [DispId(0x00000001)]        void OnNotify(string pwd);    }

5) add attributes for PasswordControl: [ComSourceInterfaces (typeof (IComEvents)]

That is, as follows:

[ComSourceInterfaces (typeof (IComEvents)] [Guid ("30a3c1b8-9a9a-440a-9e87-80d0ee827658")] public partial class PasswordControl: UserControl {//... other code}

 

6) The Js end listens to events in the following way:

<Script language = "Javascript" event = "OnNotify (pwd)" for = "activieX"> function activieX: OnNotify (pwd) {console.info ("password: "+ pwd) ;}</script>

Event: Specifies the event to listen.

For: Specifies the listener object.

The js function name is in the form of [listener object]: [listener event]. Note that there are two colons in the middle.

In this way, Js can receive the ActiveX push password when you press enter in the password box. Is it easy?

 

 

Note the following two points:

1. The delegate corresponding to the event must be customized, and the system Action should not be used <T>

2,The js function name is in the format of [listener object]: [listener event], with two colons in the middle.

 

Test Project source code: TestActiveX.zip

(To be continued ...)

 

Related Article

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.