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.