c#Activex控件中调用JavaScript

1.引用Microsoft.mshtml 

Microsoft.mshtml的路径是C:/Program Files/Microsoft.NET/Primary Interop Assemblies/Microsoft.mshtml.dll,添加引用后在ActiveX对应类中编写:

[csharp]  view plain copy
  1. <strong>using</strong> mshtml;  


2.用c#实现两个COM类,IOleClientSite和IOleContainer


[csharp]  view plain copy
  1. <strong><textarea cols="50" rows="15" name="code" class="c-sharp">using System.Runtime.InteropServices;  
  2. [ComImport,  
  3.  Guid("00000118-0000-0000-C000-000000000046"),  
  4.  InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]  
  5. public interface IOleClientSite  
  6. {  
  7.     void SaveObject();  
  8.     void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);  
  9.     void GetContainer(out IOleContainer ppContainer);  
  10.     void ShowObject();  
  11.     void OnShowWindow(bool fShow);  
  12.     void RequestNewObjectLayout();  
  13. }  
  14.   
  15. [ComImport,  
  16.  Guid("0000011B-0000-0000-C000-000000000046"),  
  17.  InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]  
  18. public interface IOleContainer  
  19. {  
  20.     void EnumObjects([In, MarshalAs(UnmanagedType.U4)] int grfFlags,  
  21.         [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppenum);  
  22.     void ParseDisplayName([In, MarshalAs(UnmanagedType.Interface)] object pbc,  
  23.         [MarshalAs(UnmanagedType.BStr)] string pszDisplayName,  
  24.         [Out, MarshalAs(UnmanagedType.LPArray)] int[] pchEaten,  
  25.         [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppmkOut);  
  26.     void LockContainer([In, MarshalAs(UnmanagedType.I4)] int fLock);  
  27. }</textarea> </strong>  


3.调用JavaScript方法 

在Activex控件的对应类中就可以编写如下的CallJavaScript方法: 

[csharp]  view plain copy
  1. <strong><textarea cols="50" rows="15" name="code" class="c-sharp">private void CallJavaScript(string Filenames)  
  2. {  
  3.     Type typeIOleObject = this.GetType().GetInterface("IOleObject"true);  
  4.     object oleClientSite = typeIOleObject.InvokeMember("GetClientSite",  
  5.      BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,  
  6.     null,  
  7.     this,  
  8.     null);  
  9.   
  10.     IOleClientSite oleClientSite2 = oleClientSite as IOleClientSite;  
  11.     IOleContainer pObj;  
  12.     oleClientSite2.GetContainer(out pObj);  
  13.   
  14.     //参数数组  
  15.   
  16.     object[] args = new object[1];  
  17.     args[0] = Filenames;  
  18.   
  19.     //获取页面的Script集合  
  20.   
  21.     IHTMLDocument pDoc2 = (IHTMLDocument) pObj;  
  22.     object script = pDoc2.Script;  
  23.   
  24.     try  
  25.     {  
  26.         //调用JavaScript方法OnScaned并传递参数,因为此方法可能并没有在页面中实现,所以要进行异常处理  
  27.   
  28.         script.GetType().InvokeMember("OnScaned",  
  29.         BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,  
  30.        null,  
  31.         script,  
  32.         args);  
  33.     }  
  34.     catch { }  
  35. }</textarea> </strong>  


4.在页面中实现相应的JavaScript方法 

在包含这个ActiveX控件的页面中添加如下的JavaScript方法:


[html]  view plain copy
  1. <strong><textarea cols="50" rows="15" name="code" class="javascript"><mce:script type="text/javascript"><!--  
  2. function OnScaned(files)  
  3. {  
  4.     if (files)  
  5.     {  
  6.         //do something  
  7.     }  
  8. }  
  9. // --></mce:script></textarea> </strong>  


这样,在ActiveX控件中调用CallJavaScript方法时,最终就会调用到页面中的OnScaned方法,藉此实现了ActiveX的“事件”机制。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#ActiveX + JS +Flex通訊 全實現 因項目部署需要,特把之前寫的與讀卡器通訊的模塊改為ActiveX插件,給Flash調用,由於Flash不能直接調用ActiveX,所以,用JS作為間層,作為Flash和ActiveX通訊的橋梁。 開發環境:WINXP _EN 開發工具:VS2005,Flex 3. .net 2.0 ActiveX控件開發 1:新建一個工程項目,MyActiveXDemo,選擇Visual C#->Windows Control Library 2:修改UserControl1.cs為ReadICCard.cs 3:設置工程發布屬性,工程屬性->Application->Assembly information, 把Make assembly Com-Visible 勾上。 4:到 工程屬性->Build->Output, 把 Register for COM interop 勾上。 5:修改AssemblyInfo.cs文件,加上紅色方框部分內容: 6:在ReadICCardActiveX 控件里,添加一個GUID,可以使用 工具-创建GUID 菜单创建一个GUID 7:为了让ActiveX控件获得客户端的信任,控件类还需要实现一个名为“IObjectSafety”的接口。先创建该接口(注意,不能修改该接口的GUID值):(這里是直接copy網上的) using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices; namespace Preresearch.CSharpActiveX { [ComImport, GuidAttribute( " CB5BDC81-93C1-11CF-8F20-00805F2CD064 " )] [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] public interface IObjectSafety { [PreserveSig] int GetInterfaceSafetyOptions( ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions); [PreserveSig()] int SetInterfaceSafetyOptions( ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions); } } 然后在控件继承并实现该接口: #region IObjectSafety 成员 private const string _IID_IDispatch = " {00020400-0000-0000-C000-000000000046} " ; private const string _IID_IDispatchEx = " {a6ef9860-c720-11d0-9337-00a0c90dcaa9} " ; private const string _IID_IPersistStorage = " {0000010A-0000-0000-C000-000000000046} " ; private const string _IID_IPersistStream = " {00000109-0000-0000-C000-000000000046} " ; private const string _IID_IPersistPropertyBag = " {37D84F60-42CB-11CE-8135-00AA004BB851} " ; private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001 ; private const
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值