您的位置 首页 教育

c#编写activeX控件详细教程,底部附有下载地址

1、新建windows窗体控件库,命名为 MyActiveXDemo,打开Properties/Assembl…

1、新建windows窗体控件库,命名为 MyActiveXDemo,打开Properties/AssemblyInfo.cs,修改为如下:

2、打开工具箱,在自动生成的UserControl1.cs设计视图中添加一个button,如下图:

3、双击button1,为按钮添加方法,这里演示只添加了一个弹出消息,如下

private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("ActiveX Message.");
}

4、创建接口IObjectSafety.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace MyActiveXDemo
{
[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);
}
}

5、修改UserControl1.cs,使其继承接口IObjectSafety,完整的UserControl1.cs如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MyActiveXDemo
{
[Guid("73D3BE6A-E3B3-46F7-AFFE-8673390DA3F6"), ProgId("ActiveXDemo.UserControl1"), ComVisible(true)]
public partial class UserControl1 : UserControl, IObjectSafety
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("ActiveX Message.");
}
#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;
}
#endregion
}
}

6、右键项目MyActiveXDemo属性,

      a、应用程序=》点击程序集信息=》勾选“使程序集COM可见”

      b、生成=》勾选“为COM互操作注册”

 

 

 

7、解决方案右键=》添加=》其它项目类型=》安装和部署,选中“InstallShield Limited Edition Project”,命名为“ActiveXSetup”,确定

8、点击application files=》My Product Name=》右下角点击add Product Outputs,在弹出框内勾选“主输出”,确定,如下图

9、右键ActiveXSetup,生成,成功后在ActiveXSetupExpressDVD-5DiskImagesDISK1下可以看到ActiveXSetup.msi安装程序,到这里就成功了一大半了。

10、网上下载一个cabarc.exe,放在与ActiveXSetup.msi安装文件相同目录,下载地址:https://sqdownd.rbread05.cn/down/42010_20161130174135.rar,在相同目录创建一个install.inf文件,文件内容如下:

[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
hook1=hook1
[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%ActiveXSetup.msi" /qn

11、同时在同目录创建一个build.bat,内容如下:

"cabarc.exe" n test.cab ActiveXSetup.msi install.inf

双击build.bat,执行完毕后将会生成一个test.cab文件,test.cab就是我们打包好的一个ActiceX控件了。

 

如何在浏览器上加载ActiveX控件?请看下一篇:asp.net 如何在网页上加载ActiveX控件,内附下载链接

本文来自网络,不代表0514资讯网立场,转载请注明出处:https://0514zx.com/info/6335.html
0514zx.com

作者: 0514zx.com

优质职场领域创作者
联系我们

联系我们

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部