ActiveX在.NET 2005中的实现

ActiveX在.NET 2005中的实现

 

 

<script type="text/javascript"></script> 在http://homer.cnblogs.com/archive/2005/01/04/86473.aspx 中看到了一篇关于如何名叫“用C#编写ActiveX控件”的文章,受益匪浅。


但该文章可能是在旧版本的.NET开发环境中实现的,在.NET 2005怎么实现也没能成功实现,于是自己从头开始做了一个开发,几经周折终于实现,现在分享给大家。


1、ActiveX在.NET中的实现

单击显示全图,Ctrl+滚轮缩放图片


如上图所示在.NET中使用UserControl来实现ActiveX。代码如下。


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;

// Add in these using clauses for this example

using System.Reflection;
using Microsoft.Win32;


namespace ActiveXDotNet
{
//[ClassInterface(ClassInterfaceType.AutoDual)]

public partial class myControl : UserControl, AxMyControl
{
public myControl()
{
InitializeComponent();
}

private String mStr_UserText;

public String UserText
{
get { return mStr_UserText + " OK" ; }
set
{
mStr_UserText = value;
this .txtUserText.Text = value;
}
}

private void button1_Click(object sender, EventArgs e)
{
this .txtUserText.Text = "Hello World" ;
}

private void button2_Click(object sender, EventArgs e)
{
InputForm frmInput = new InputForm();
frmInput.ShowDialog();
}
}
}

 

2、其中,将在HTML中使用的方法在接口AxMyControl中实现,代码如下:


using System;
using System.Collections.Generic;
using System.Text;

namespace ActiveXDotNet
{
interface AxMyControl
{
String UserText { set ; get ; }
}
}


3、Assembly.cs中的特殊设置:


1 // Setting ComVisible to false makes the types in this assembly not visible 

2 // to COM components.  If you need to access a type in this assembly from 

3 // COM, set the ComVisible attribute to true on that type.

4 [assembly: ComVisible(true)]

 

在HTML中对.NET ActiveX的调用,与交互。

其实和普通的ActiveX调用方式没有太大不同,相信大家看了代码都明白。


< html >
< head >
< script language= "javascript" >
function doScript()
{
myControl1.UserText = frm.txt.value;
alert (myControl1.UserText);
}
</ script >
</ head >
< body color= white >
< hr >
< font face= arial size= 1 >
< object id= "myControl1" name= "myControl1" classid= "clsid:182AEEFE-A6D6-3014-A2FB-672AAB805E18" codebase= 'DownloadActiveXNet.cab#version=1,0,0,0' >
</ object >
</ font >
< form name= "frm" id= "frm" >
< input type= "text" name= "txt" value= "enter text here" >
< input type= button value= "Click me" οnclick= "doScript();" >

</ form >
< hr >
</ body >

</ html >

 

运行后的效果如下图所示:

单击显示全图,Ctrl+滚轮缩放图片

 

 

3、部署

 

我们知道当使用C++, COM和ATL来开发ActiveX的时候得CAB文件里面的INF文件描述了如何注册以及复制到何处的指令。


但是当我们用C#来开发ActiveX的时候,INF文件里面Registerserver=Yes 不起作用。因为这个命令只是调用regsvr32 来注册相应的组件,他对C#开发的ActiveX不起作用。


1、为了发布C#开发的ActiveX我们首先在2005里面建立一个.NET的MSI文件,如下图所示:

单击显示全图,Ctrl+滚轮缩放图片


在Setup的Added output file的属性窗口里面,确认将Register property 设置成vsdrpCOM, 这个设置确保了在安装的时候会

调用regasm 来注册ActiveX控件。


2、CAB文件的建立


[version]
signature = "$CHICAGO$"

AdvancedINF = 2.0

[Setup Hooks]
hook1 = hook1


[hook1]
run = msiexec.exe /i "%EXTRACT_DIR%/ActiveXDotDownload.msi" /qn


单击显示全图,Ctrl+滚轮缩放图片


注意,浏览器出于安全性考虑,会拦截未经数字认证的控件。修改浏览器设置,在Internet选项-〉安全-〉受信任的站点-〉站点 中添加服务器地址,不要选复选框“对该区域中的所有站点要求服务器验证”。解决浏览器拦截问题,而不用更改浏览器的安全级别。


结束语


本文探讨了使用C# 2.0 在。NET2005平台上面开发ActiveX的可行性,虽然和以往的一些技术手段来比,可能有些不足,但也不失可以作为一种特殊情况下的手段。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值