First, preface
After several studies, now we have mastered the entire development process of ActiveX, but to play the true power of ActiveX, must rely on JS. Let's learn now!
Second, JS call ActiveX method
Simply write a public method in the UserControl subclass (that is, the custom ActiveX control).
C#
[Guid ("0203dabd-51b8-4e8e-a1eb-156950ee1668")] Public Partial class Uploader:usercontrol, iobjectsafety{ public Uploader () { InitializeComponent (); } // ActiveX method called by JS public void sayhi (msg) { MessageBox.Show (msg);} }
Html
//Note: Object must be written in <object></object> to get through document.getElementById<ObjectClassid="clsid:xxxxxxxxxx"Id="Ax"></Object><script type="Text/javascript">document.getElementById ('Ax'). Sayhi ('Test');//Call the ActiveX method</script>
Third, ActiveX Call JS method
1. Introduce the ' microsoft.mshtml ' assembly, which is located in ' C:\Program files\microsoft.net\primary Interop assemblies\microsoft.mshtml.dll '
2. Implement COM class ' IOleClientSite '
C#
[ComImport, Guid ("00000118-0000-0000-c000-000000000046"), InterfaceType (Cominterfacetype.interfaceisiunknown)] Public InterfaceIOleClientSite {voidSaveobject (); voidGetMoniker (UINTDwassign,UINTDwwhichmoniker,ObjectPPMK); voidGetContainer ( outIOleContainer Ppcontainer); voidShowobject (); voidOnshowwindow (BOOLfshow); voidrequestnewobjectlayout ();}
3. Implement COM class ' IOleContainer '
C#
[ComImport, Guid ("0000011b-0000-0000-c000-000000000046"), InterfaceTypeAttribute (Cominterfacetype.interfaceisiunknown)] Public InterfaceIOleContainer {voidEnumObjects ([In, MarshalAs (UNMANAGEDTYPE.U4)]intGrfflags, [out, MarshalAs (UnmanagedType.LPArray)]Object[] ppenum); voidParseDisplayName ([In, MarshalAs (Unmanagedtype.interface)]ObjectPBC, [In, MarshalAs (UNMANAGEDTYPE.BSTR)]stringpszDisplayName, [out, MarshalAs (UnmanagedType.LPArray)]int[] Pcheaten, [out, MarshalAs (UnmanagedType.LPArray)]Object[] ppmkout); voidLockcontainer ([In, MarshalAs (UNMANAGEDTYPE.I4)]intFLock); }
4. Invoking the JS method in the user control
In the MyUserControl.cs
C#
#regionInvoking the JS functionPrivateType Typeioleobject =NULL; PrivateIOleClientSite Oleclientsite =NULL; PrivateIOleContainer POBJ =NULL; /// <summary> ///invoking the JS function/// </summary> /// <param name= "FnName" >JS function name</param> /// <param name= "args" >Enter the parameter</param> protected voidCalljs (stringFnName,params Object[] args) { if(Typeioleobject = =NULL) {Typeioleobject= This. GetType (). GetInterface ("IOleObject",true); ObjectTmpoldclientsite = Typeioleobject.invokemember ("GetClientSite", BindingFlags.Instance| BindingFlags.InvokeMethod |BindingFlags.Public,NULL, This, NULL); Oleclientsite= Tmpoldclientsite asIOleClientSite; Oleclientsite.getcontainer ( outPOBJ); } //get the Script collection for a pageIHTMLDocument PDOC2 =(ihtmldocument) pObj; ObjectScript =Pdoc2.script; Try { //call the JavaScript method onscaned and pass the arguments, because this method may not be implemented in the page, so exception handlingscript. GetType (). InvokeMember (FnName, BindingFlags.Instance| BindingFlags.InvokeMethod |BindingFlags.Public,NULL, script, args); } Catch { } } #endregion
V. The pits that have been trampled
1. If the ActiveX control cannot be loaded under IE9
Check if it is a 64-bit IE9. 32-bit ActiveX controls cannot be used on 64-bit IE. (See the process name through Task Manager, if the browser process name followed by 32 is 32 bits)
Vi. Summary
《. NET Magic Hall: The history of the most complete ActiveX development tutorial series finally finished, a Bo eyeball title, hope that the content of the series will not let everyone down. Thanks for watching, haha!
Respect the original, reprint please indicate from: http://www.cnblogs.com/fsjohnhuang/p/3823687.html ^_^ Fat Boy John