1.新建项目,选择C#,选择.NET Framework2.0,新建一个Windows窗体控件库项目,命名为ActiveXDemo;
2.右击ActiveXDem项目,选择属性——应用程序——程序集信息,勾选“使程序集COM可见”;
3.右击ActiveXDem项目,选择属性——应用程序——生成,勾选“为COM互操作注册”(注意,此处如果是在debug状态下修改的,那么在调到release状态下还需要再设置一次);
4.右击ActiveXDem项目,添加一个“用户控件”,命名为MiugoX.cs,新建项目是会自动生成一个UserControl.cs,可以删除;
5.为控件类MiugoX.cs添加GUID,可以在工具——创建GUID,新建一个GUID
[Guid("A0FCB80C-97AB-4C33-B879-70B479169B2F"), ProgId("ActiveXDemo.MiugoX"), ComVisible(true)]
6.为了让ActiveX控件获得客户端的信任,控件类还需要实现一个名为“IObjectSafety”的接口(在实现IObjectSafety接口之后,浏览器就不会再弹出是否允许运行之类的对话框)。先创建该接口(注意,不能修改该接口的GUID值):添加一个接口IObjectSafety,实现该接口的目的就是提高程序的安全性,以便客户端IE在不更改设置的情况下可以该运行ActiveX控件
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace ActiveXDemo
{
[ComImport, GuidAttribute("A58D34F0-BAB8-4B13-915D-165384FBC44E")] //CB5BDC81-93C1-11CF-8F20-00805F2CD064 是定值,前面出错,GUID不能改变
[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);
}
}
7.用户控件MiugoX.cs需要继承并实现IObjectSafety接口(固定格式,GUID不能改变)
public partial class MiugoX : UserControl, 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 = ;
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 = ;
if (_fSafeForScripting == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER; break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag: Rslt = S_OK;
pdwEnabledOptions = ;
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;
}
}
8.按照windows窗体的思路完成控件的开发,从工具箱中拖拽控件并编写事件和方法
public void SayHelloWord()
{
MessageBox.Show("你好 ActiveX !");
}
public void ShowMessage(string msg)
{
MessageBox.Show(msg);
}
public string GetHelloWord()
{
return "Hello ActiveX";
}
public void CreateText()
{
labelResult.Text = ""; if (txtFileName.Text == "")
{
labelResult.Text = "请输入文件名?"; return;
}
string dir = "C:\\ActiveDemo";
string filePath = dir + "\\" + txtFileName.Text + ".txt"; if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
if (!File.Exists(filePath))
{
File.CreateText(filePath);
labelResult.Text += filePath + "创建成功\r\n";
}
else
{
labelResult.Text += filePath + "已存在";
}
}
public void GetFiles()
{
string dir = "C:\\ActiveDemo"; if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
string[] fileNameList = Directory.GetFiles(dir); if (fileNameList.Length == )
{
labelResult.Text = "没有文件"; return;
}
StringBuilder sb = new StringBuilder(); foreach (string fileName in fileNameList)
{
sb.Append(fileName + "\r\n");
}
labelResult.Text = "";
labelResult.Text = sb.ToString();
}
private void btnCreate_Click(object sender, EventArgs e)
{
CreateText();
}
private void btnGet_Click(object sender, EventArgs e)
{
GetFiles();
}
9.这时ActiveX控件已经开发完成,右击项目名称,选择生成(Dll注册时一直提示不是以管理员身份运行,这时需要在所有程序——VS2010右击以管理员身份运行,再次重新生成,在注册表中就可以看到已经注册的dll。)
10.ActiveX控件调试,新建一个html页面,添加如下代码:
<html>
<head>
</head>
<body>
<object id="activeXDemo" classid="clsid:A0FCB80C-97AB-4C33-B879-70B479169B2F" codebase="Install/ActiveXDemo.cab#version=1,2,0,0/"></object>
<input type="button" value="click me" onclick="activeXDemo.ShowMessage('Today Exam')"/>
</body>
</html>
用Ie浏览器打开,底部会提示是否允许ActiveX运行,选择是,并弹出如下对话框
选择是,即可操作ActiveX控件。
总结:既可以通过classid进行调用,也可以通过JS方法实现互操作
var Excel = new ActiveXObject("ActiveXDemo.Miugox");
alert(Excel.SayHelloWord);