前言:
如今我们对“控件call”都比较着重,你们都想要了解一些“控件call”的相关文章。那么小编在网摘上收集了一些对于“控件call””的相关文章,希望同学们能喜欢,姐妹们快快来了解一下吧!1.创建Windows窗体控件库
这里的项目项目名称为: TestActiveX
在默认的用户控件上,放置一个button,并创建单击事件
3.创建IObjectSafety接口(重点)
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);}4.将下面的代码,粘贴在控件类中
#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 int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;private const int S_OK = 0;private const int E_FAIL = unchecked((int)0x80004005);private const int E_NOINTERFACE = unchecked((int)0x80004002);private bool _fSafeForScripting = true;private bool _fSafeForInitializing = true;public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions){ int Rslt = E_FAIL; string strGUID = riid.ToString("B"); pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA; switch (strGUID) { case _IID_IDispatch: case _IID_IDispatchEx: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForScripting == true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: Rslt = S_OK; pdwEnabledOptions = 0; if (_fSafeForInitializing == true) pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA; break; default: Rslt = E_NOINTERFACE; break; } return Rslt;}public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions){ int Rslt = E_FAIL; string strGUID = riid.ToString("B"); switch (strGUID) { case _IID_IDispatch: case _IID_IDispatchEx: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true)) Rslt = S_OK; break; case _IID_IPersistStorage: case _IID_IPersistStream: case _IID_IPersistPropertyBag: if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true)) Rslt = S_OK; break; default: Rslt = E_NOINTERFACE; break; } return Rslt;}#endregion5.在控件类,加上Guid特性,生成一个guid,这个在web页面使用的(切记)
[Guid("4a202141-e8fc-42c8-b061-5d2c5f5cd35d")] //记住这个Guid在页面上使用,要用这个public partial class UserControl1 : UserControl, IObjectSafety{ //此处省略若干代码和事件代码}6.设置程序集信息和注册com互操作
在项目属性,应用程序->程序集信息
设置程序集com可见
在项目属性,生成->为com互操作注册
7.在页面使用ActiveX控件
在这之前,需要将Activex控件打包成应用程序,如果是vs2017的话,默认没有打包工具,在vs2017的应用商店中安装,搜索关键字 Installer 即可.
<!DOCTYPE html><html><head> <meta charset="utf-8" /> <title>测试Activex控件</title></head><body> <!--控件id为clsid:后跟我们在控件类使用的guid,一定要一致,是因为要通过这个guid查找注册表,最终定位控件动态库--> <object id="TestActiveX" classid="clsid:4a202141-e8fc-42c8-b061-5d2c5f5cd35d" width="400" height="100"> </object></body></html>
看测试结果:
最重要的一步
在AssemblyInfo.cs文件,添加以下代码
//一定要加的[assembly: AllowPartiallyTrustedCallers()]进入真正的主题,因为上面都是准备工作的
1.在控件类,添加该函数
/// <summary>/// 公开调用,该函数要public去修饰,其他和正常函数一样/// </summary>/// <param name="txt">文本参数</param>/// <returns>返回调用结果</returns>public string TestActiveXMethod(string txt){ return $"hello {txt}";}
2.在页面上调用公开的csharp函数
<!DOCTYPE html><html><head> <meta charset="utf-8" /> <title>测试Activex控件</title></head><body> <!--控件id为clsid:后跟我们在控件类使用的guid,一定要一致,是因为要通过这个guid查找注册表,最终定位控件动态库--> <!--这个时候,已经不需要显示控件了--> <object id="TestActiveX" classid="clsid:4a202141-e8fc-42c8-b061-5d2c5f5cd35d" width="0" height="0"></object> <input type="text" id="txtUserName" /> <input type="button" id="btnCallActiveX" value="调用Activex控件中的函数" /> <script> var btnCallActiveX = document.getElementById('btnCallActiveX'); var txtUserName = document.getElementById('txtUserName'); btnCallActiveX.onclick=function(){ //调用ActiveX控件, 控件id 后跟控件公开的函数 var callResult = TestActiveX.TestActiveXMethod(txtUserName.value); alert(callResult); }; </script></body></html>
调用结果:
由于是迁移的文章,Activex现在用的比较少了,在特定的行业中还是有使用的,比如医疗行业软件,不过使用的是越来越少.
版权声明:
本站文章均来自互联网搜集,如有侵犯您的权益,请联系我们删除,谢谢。
标签: #控件call