ActiveX控件以前也叫做OLE控件或OCX控件,它是一些软件组件或对象,可以将其插入到WEB网页或其它应用程序中。使用ActiveX插件,可以轻松方便的在 Web页中插入多媒体效果、交互式对象以及复杂程序等等。
在Visual Studio 2008开发环境中,可以使用Windows控件库项目实现ActiveX控件的开发,但是需要对项目做一些必要的设置。下面就来看看如何使用Windows控件库项目开发一个ActiveX控件。首先创建一个应用程序解决方案,并添加一个Windows控件库项目:
更改“项目属性-应用程序-程序集信息”设置,勾选“使程序集 COM 可见”:
更改“项目属性-生成”设置,勾选“为 COM Interop 注册”(注意,此处如果实在debug状态下修改的,那在调到release状态下还需要再设置一次):
http://images.cnblogs.com/cnblogs_com/yilin/WindowsLiveWriter/CActiveX_13F30/clip_image006_2.jpg
修改AssemblyInfo.cs文件,添加[assembly: AllowPartiallyTrustedCallers()]项(需要引用System.Security名称空间):
C# code
编码实现
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;
[assembly: AssemblyTitle("Yilin.Preresearch.CSharpActiveX")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("10BAR")]
[assembly: AssemblyProduct("Yilin.Preresearch.CSharpActiveX")]
[assembly: AssemblyCopyright("Copyright © 10BAR 2009")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
[assembly: AllowPartiallyTrustedCallers()]
[assembly: ComVisible(true)]
[assembly: Guid("114d1f0c-43b8-40ac-ae7c-5adccc19aef3")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
按照开发Windows用户控件一样的思路完成该控件的开发,本例中主要实现了两个业务功能,一个是提供一个公共方法,用于读取USBKey中保存的签名证书,保存到本地C盘根目录下,并返回操作信息;另一个业务功能提供UI界面,包括一个Button控件和一个Label控件,Button控件的Click事件调用前面提供的那个方法,并将返回信息显示到Label控件上。这样做可以达到两个目的,其一,ActiveX控件提供公共方法供B/S程序直接调用,从后实现业务功能;其二,ActiveX控件可以提供B/S程序UI界面,通过响应B/S程序中对UI的操作事件实现业务功能。
完成控件开发后,为了使该用户控件作为一个ActiveX控件进行使用,还需要做以下修改:
首先,为控件类添加GUID,这个编号将用于B/S系统的客户端调用时使用(可以使用 工具-创建GUID 菜单创建一个GUID):
C# code
Guid(
"4A44CF4E-F859-4328-AA22-3E9D7AFFF1AB"
)]
public
partial
class
Hello : UserControl
{
其次,为了让ActiveX控件获得客户端的信任,控件类还需要实现一个名为“IObjectSafety”的接口。先创建该接口(注意,不能修改该接口的GUID值):
C# code
编码实现
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);
}
}
然后在控件类中继承并实现该接口:
C# code
编码实现
#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
然后怎么部署的我想你应该知道的!!
然后怎么部署的我想你应该知道的!!
我们使用 工具—〉OLE/COM对象查看器 查看,如图:
可以看到,我们写的控件已经被正确识别为COM组件。现在,我们已经可以像使用其它ActiveX控件一样在网页中显示了。在HelloWorld.Demo点击鼠标右键,如图:
选择Copy HTML <object> Tag to Clipboard,可以将代码拷入剪贴板。
<body bgcolor=’#223344’>
<object id="helloworld"
classid="clsid:9551B223-6188-4387-B293-C7D9D8173E3A" Width="184" Height="96">
</object>
</body>
http://www.iwms.net/upload/2006_05/06053012333489.zip