如何用.NET编写ActiveX 控件
2010-08-27 13:13
567 查看
Plato 标签: .net,ActiveX,ActiveX控件,脚本,互操作,程序集 第一步:新建一个解决方案demoSolution,添加一个Windows控件库项目,我们起名为demoActiveX。为便于阅读,我们更改控件名字为demoControl.cs,然后拖动一个textBox控件到窗体上。
第二步:在demoControl类下新建公共方法Test,用于实现控件与客户端脚本的互操作,
public void Test()
{
MessageBox.Show("你输入的内容为:" + this.textBox1.Text);
}
第三步:打开AssemblyInfo.cs修改程序集信息。引用System.Security命名空间,并添加[assembly : AllowPartiallyTrustedCallers()]安全声明,修改[assembly: ComVisible(false)]为[assembly: ComVisible(true)]使程序集Com可见。
第四步:为Com Interop注册。右键demoActiveX项目属性,在“生成”选项卡里将“为Com Interop注册”打上勾即可。
第五步:在demoActiveX中添加命名空间System.Runtime.InteropServices,使用工具->创建 Guid工具创建一个新的Guid{E5FD041B-8250-4cbc-B662-A73FC7988FB5},用该Guid初始化demoActiveX,该Guid即是我们要在Web页面下引用该ActiveX的classid 。
第六步:实现IObjectSafety接口,把ActiveX控件标记为安全的。
接口:
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
// methods
void GetInterfacceSafyOptions(
System.Int32 riid,
out System.Int32 pdwSupportedOptions,
out System.Int32 pdwEnabledOptions);
void SetInterfaceSafetyOptions(
System.Int32 riid,
System.Int32 dwOptionsSetMask,
System.Int32 dwEnabledOptions);
}
实现:
public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
{
// TODO: 添加 demoControl.GetInterfacceSafyOptions 实现
pdwSupportedOptions = 1;
pdwEnabledOptions = 2;
}
public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
{
// TODO: 添加 demoControl.SetInterfaceSafetyOptions 实现
}
到现在为止整个程序已经完成,下一步我们需要做的就是如何发布这个ActiveX
第七步:发布。添加新Windows安装项目 winsetup 添加项目主输出,然后,改动ActiveX控件的主输出文件,将其Register属性改为vsdrpCOM,很遗憾的是,安装URL中必须使用绝对路径,不能使用相对路径。这意味着生成安装程序的时候就必须确定路径,不是很方便。在示例中,我使用了http://localhost/demoActiveX在发布中需要改为实际的域名。 然后生成安装程序,并把setup.exe及winsetup.msi拷贝到虚拟目录demoActiveX下。/
第八步:测试。编写一个htm页面以供测试,需要注意一点,我们在object块中加入了codebase属性代码,用于指定下载控件的位置。参考下面代码文件。
第九步:签名,这个就不多说了,到网上申请一个即可。
demoActiveX.cs代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace demoActiveX
{
[Guid("E5FD041B-8250-4cbc-B662-A73FC7988FB5")]
public partial class demoControl : UserControl, IObjectSafety
{
public demoControl()
{
InitializeComponent();
}
public void Test()
{
MessageBox.Show("你输入的内容为:" + this.textBox1.Text);
}
public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
{
// TODO: 添加 demoControl.GetInterfacceSafyOptions 实现
pdwSupportedOptions = 1;
pdwEnabledOptions = 2;
}
public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
{
// TODO: 添加 demoControl.SetInterfaceSafetyOptions 实现
}
}
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
// methods
void GetInterfacceSafyOptions(
System.Int32 riid,
out System.Int32 pdwSupportedOptions,
out System.Int32 pdwEnabledOptions);
void SetInterfaceSafetyOptions(
System.Int32 riid,
System.Int32 dwOptionsSetMask,
System.Int32 dwEnabledOptions);
}
}
test.htm代码:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<form id="form1">
<table>
<tr>
<td align="center">
<object id="demoActiveX"
classid="clsid:E5FD041B-8250-4cbc-B662-A73FC7988FB5" codebase="Setup.exe" Width="200" Height="100">
</object>
</td>
</tr>
<tr>
<td align="center">
<input type=button id="Button1" value="测试" onclick="javascript:doTest()">
</td>
</tr>
</table>
<script>
function doTest()
{
var obj = document.getElementById("demoActiveX");
obj.Test();
}
</script>
</form>
</body>
</html>
测试一下,http://localhost/demoActiveX/test.htm OK!我想你也成功了。
第二步:在demoControl类下新建公共方法Test,用于实现控件与客户端脚本的互操作,
public void Test()
{
MessageBox.Show("你输入的内容为:" + this.textBox1.Text);
}
第三步:打开AssemblyInfo.cs修改程序集信息。引用System.Security命名空间,并添加[assembly : AllowPartiallyTrustedCallers()]安全声明,修改[assembly: ComVisible(false)]为[assembly: ComVisible(true)]使程序集Com可见。
第四步:为Com Interop注册。右键demoActiveX项目属性,在“生成”选项卡里将“为Com Interop注册”打上勾即可。
第五步:在demoActiveX中添加命名空间System.Runtime.InteropServices,使用工具->创建 Guid工具创建一个新的Guid{E5FD041B-8250-4cbc-B662-A73FC7988FB5},用该Guid初始化demoActiveX,该Guid即是我们要在Web页面下引用该ActiveX的classid 。
第六步:实现IObjectSafety接口,把ActiveX控件标记为安全的。
接口:
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
// methods
void GetInterfacceSafyOptions(
System.Int32 riid,
out System.Int32 pdwSupportedOptions,
out System.Int32 pdwEnabledOptions);
void SetInterfaceSafetyOptions(
System.Int32 riid,
System.Int32 dwOptionsSetMask,
System.Int32 dwEnabledOptions);
}
实现:
public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
{
// TODO: 添加 demoControl.GetInterfacceSafyOptions 实现
pdwSupportedOptions = 1;
pdwEnabledOptions = 2;
}
public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
{
// TODO: 添加 demoControl.SetInterfaceSafetyOptions 实现
}
到现在为止整个程序已经完成,下一步我们需要做的就是如何发布这个ActiveX
第七步:发布。添加新Windows安装项目 winsetup 添加项目主输出,然后,改动ActiveX控件的主输出文件,将其Register属性改为vsdrpCOM,很遗憾的是,安装URL中必须使用绝对路径,不能使用相对路径。这意味着生成安装程序的时候就必须确定路径,不是很方便。在示例中,我使用了http://localhost/demoActiveX在发布中需要改为实际的域名。 然后生成安装程序,并把setup.exe及winsetup.msi拷贝到虚拟目录demoActiveX下。/
第八步:测试。编写一个htm页面以供测试,需要注意一点,我们在object块中加入了codebase属性代码,用于指定下载控件的位置。参考下面代码文件。
第九步:签名,这个就不多说了,到网上申请一个即可。
demoActiveX.cs代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace demoActiveX
{
[Guid("E5FD041B-8250-4cbc-B662-A73FC7988FB5")]
public partial class demoControl : UserControl, IObjectSafety
{
public demoControl()
{
InitializeComponent();
}
public void Test()
{
MessageBox.Show("你输入的内容为:" + this.textBox1.Text);
}
public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
{
// TODO: 添加 demoControl.GetInterfacceSafyOptions 实现
pdwSupportedOptions = 1;
pdwEnabledOptions = 2;
}
public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
{
// TODO: 添加 demoControl.SetInterfaceSafetyOptions 实现
}
}
[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
// methods
void GetInterfacceSafyOptions(
System.Int32 riid,
out System.Int32 pdwSupportedOptions,
out System.Int32 pdwEnabledOptions);
void SetInterfaceSafetyOptions(
System.Int32 riid,
System.Int32 dwOptionsSetMask,
System.Int32 dwEnabledOptions);
}
}
test.htm代码:
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Untitled Page</title>
</head>
<body>
<form id="form1">
<table>
<tr>
<td align="center">
<object id="demoActiveX"
classid="clsid:E5FD041B-8250-4cbc-B662-A73FC7988FB5" codebase="Setup.exe" Width="200" Height="100">
</object>
</td>
</tr>
<tr>
<td align="center">
<input type=button id="Button1" value="测试" onclick="javascript:doTest()">
</td>
</tr>
</table>
<script>
function doTest()
{
var obj = document.getElementById("demoActiveX");
obj.Test();
}
</script>
</form>
</body>
</html>
测试一下,http://localhost/demoActiveX/test.htm OK!我想你也成功了。
相关文章推荐
- 如何用.NET编写ActiveX 控件
- 用.NET编写自己的ActiveX 控件
- 如何将MFC ActiveX 控件标记为可安全编写脚本和初始化
- 如何将.NET中编写控件作为MFC的视图调用?(From MSDN)
- 如何将MFC ActiveX 控件标记为可安全编写脚本和初始化
- 编写的activeX 控件如何被IE调用?
- 【求勿删】【求助!!!】如何破解.net的viewerstate??如何将隐藏的服务器控件显示出来
- 在Web中如何使用Windows控件(ActiveX)
- 如何使用 .net 的TreeView控件
- 又一个用VB编写ActiveX自定义控件的例子——进度条控件
- .net编写服务器控件时,向页面写入脚本的方法
- 如何编写Silverlight动画效果控件
- 在.NET上如何根据字符串动态创建控件
- .NET中如何将LABEL(标签控件)背景设为透明
- 在.NET上如何根据字符串动态创建控件
- 如何把其他项目中编写好的 自定义控件 放到自己的项目里面使用 (Multi-ListBox ASP.NET控件)
- (使用.Net)在绘制控件时如何消除闪烁现象
- 如何标记为安全 MFCActiveX 控件对于脚本和初始化
- .Net中DataGridView控件如何设置行高
- VC2005从开发MFC ActiveX ocx控件到发布到.net网站的全部过程