标签:
一、开发ActiveX控件
1、新建类库,命名类库名称“user.cs”;
2、在类库中添加自定义用户控件“ UserControl1”,实现各种自定义功能;
3、为了解决浏览器安全设置对控件的影响,必须在组件中加入IObjectSafety接口,所以再添加一个接口类“IObjectSafety.cs”
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts; //必须引用该包
using System.Security;
using System.Runtime.InteropServices; //必须引用该包
namespace user
{
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] //GUID这个不需修改,固定的
public interface IObjectSafety
{
// 方法定义
void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions);
void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions);
}
}
4、继承接口
public partial class UserControl1: UserControl,IObjectSafety
{
public UserControl1()
{
InitializeComponent();
}
public void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions)
{
pdwSupportedOptions = 1; //不要修改该代码
pdwEnabledOptions = 2; //不要修改该代码
return;
}
public void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions)
{
return;
}
public void YourFunc(){}
}
5、在UserControl1引入两个命名空间
using System.Security;
using System.Runtime.InteropServices;
6、工具——创建GUID——新建GUID——选择第五项——复制,就可以关闭小窗口,然后在命名空间下粘贴,如下
namespace user
{
[Guid("7F29ACED-AD84-4EEE-9E1A-58BE255F9EF7")] //这个GUID是web引用的时候用到的
public partial class UserControl1: UserControl,IObjectSafety
{
……

7、最后一步,项目——user属性(最后一项),两处需要修改
①应用程序——程序集信息——√ 使程序集COM可见
②生成——√ 为COM互操作注册
8、即可右键项目user——生成
二、web使用ActiveX控件
<body>
<form id="form1" runat="server">
<div >
<object id="VisioDisPlay"
classid="clsid:7F29ACED-AD84-4EEE-9E1A-58BE255F9EF7"
width="1250"
height="600"
>
</object>
</div>
</form>
即可完成。注意的是:这里的classid里边的字符串里边有 “ clsid: ”,别忘啦,后面接的是第六步生成的GUID,必须一致!
标签:
原文地址:http://www.cnblogs.com/liuqichun/p/4498196.html