您的位置:首页 > 其它

Microsoft Visual Studio .NET 2005 编写自己的ActiveX

2006-04-06 17:52 549 查看
由于最近一个Web项目要用到视频图像采集,没有很合适的成型控件使用,就只能硬着头皮自己写了,当然VC++或者VB都可以做ActiveX,可是自己又不会VC++,VB也仅停留在读懂的基础上,所以就只能用C#来实现了,虽然这样一来客户端需要安装.net framework,但是项目不等人,现在就动手。

第一步:新建一个解决方案 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!我想你也成功了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐