C # using JavaScript in ActiveX Controls

Source: Internet
Author: User

 

1. Reference Microsoft. mshtml

The path of Microsoft. mshtml is C:/program files/Microsoft. NET/primary InterOP assemblies/Microsoft. mshtml. dll. After adding a reference, write it in the ActiveX class:

using mshtml;

2. Use C # to implement two com classes: ioleclientsite and iolecontainer


using System.Runtime.InteropServices;[ComImport, Guid("00000118-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]public interface IOleClientSite{    void SaveObject();    void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);    void GetContainer(out IOleContainer ppContainer);    void ShowObject();    void OnShowWindow(bool fShow);    void RequestNewObjectLayout();}[ComImport, Guid("0000011B-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]public interface IOleContainer{    void EnumObjects([In, MarshalAs(UnmanagedType.U4)] int grfFlags,        [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppenum);    void ParseDisplayName([In, MarshalAs(UnmanagedType.Interface)] object pbc,        [MarshalAs(UnmanagedType.BStr)] string pszDisplayName,        [Out, MarshalAs(UnmanagedType.LPArray)] int[] pchEaten,        [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppmkOut);    void LockContainer([In, MarshalAs(UnmanagedType.I4)] int fLock);}

3. Call the JavaScript Method

You can compile the following calljavascript method in the corresponding class of the ActiveX control:

Private void calljavascript (string filenames) {type typeioleobject = This. getType (). getinterface ("ioleobject", true); object oleclientsite = typeioleobject. invokemember ("getclientsite", bindingflags. instance | bindingflags. invokemethod | bindingflags. public, null, this, null); ioleclientsite oleclientsite2 = oleclientsite as ioleclientsite; iolecontainer pobj; oleclientsite2.getcontainer (Out pobj); // parameter array object [] ARGs = new object [1]; ARGs [0] = filenames; // obtain the script set ihtmldocument pdoc2 = (ihtmldocument) pobj; object script = pdoc2.script; try {// call the JavaScript method onscaned and pass the parameter, this method may not be implemented on the page, so the Exception Processing script is required. getType (). invokemember ("onscaned", bindingflags. instance | bindingflags. invokemethod | bindingflags. public, null, script, argS);} catch {}}

4. Implement the corresponding JavaScript method on the page

Add the following JavaScript Method to the page that contains the ActiveX control:


<mce:script type="text/javascript"><!--function OnScaned(files){    if (files)    {        //do something    }}// --></mce:script>

In this way, when the calljavascript method is called in the ActiveX control, the onscaned method in the page is finally called to implement the ActiveX "Event" mechanism.

 

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.