ActiveX在.NET 2005中的实现

http://homer.cnblogs.com/archive/2005/01/04/86473.aspx 中看到了一篇关于如何名叫“用C#编写ActiveX控件”的文章,受益匪浅。
但该文章可能是在旧版本的.NET开发环境中实现的,在.NET 2005怎么实现也没能成功实现,于是自己从头开始做了一个开发,几经周折终于实现,现在分享给大家。
1、ActiveX在.NET中的实现
 

如上图所示在.NET中使用UserControl来实现ActiveX。代码如下。
 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Drawing;
 5 using System.Data;
 6 using System.Text;
 7 using System.Windows.Forms;
 8 using System.Runtime.InteropServices;
 9 
10 // Add in these    using clauses for this example
11 using System.Reflection;
12 using Microsoft.Win32;
13 
14 
15 namespace ActiveXDotNet
16 {
17      //[ClassInterface(ClassInterfaceType.AutoDual)]
18     public partial class myControl : UserControl, AxMyControl
19     {
20         public myControl()
21         {
22             InitializeComponent();
23         }
24 
25         private String mStr_UserText;
26 
27         public String UserText
28         {          
29             get { return mStr_UserText + " OK"; }
30             set {
31                 mStr_UserText = value;
32                 this.txtUserText.Text = value;
33             }
34         }
35 
36         private void button1_Click(object sender, EventArgs e)
37         {
38             this.txtUserText.Text = "Hello World";
39         }
40 
41         private void button2_Click(object sender, EventArgs e)
42         {
43             InputForm frmInput = new InputForm();
44             frmInput.ShowDialog();
45         }
46 
47     }
48 }
49 

2、其中,将在HTML中使用的方法在接口AxMyControl中实现,代码如下:
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Text;
 4 
 5 namespace ActiveXDotNet
 6 {
 7     interface AxMyControl
 8     {
 9         String UserText { set; get; }
10     }
11 }
12 

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)]

这样,一个简单的基于.NET的AticveX控件就开发完成了,下一次讲解具体的调用方法。
在HTML中对.NET ActiveX的调用,与交互。
其实和普通的ActiveX调用方式没有太大不同,相信大家看了代码都明白。

 1 < html >
 2      < head >
 3          < script  language ="javascript" >
 4      function doScript()
 5       {
 6        myControl1.UserText = frm.txt.value;
 7        alert(myControl1.UserText);
 8       }

 9      
10
</ script >
11 </ head >
12   < body  color =white >
13    < hr >   
14        < font  face =arial  size =1 >
15         < object  id ="myControl1"  name ="myControl1"  classid ="clsid:182AEEFE-A6D6-3014-A2FB-672AAB805E18"   codebase ='DownloadActiveXNet.cab#version=1,0,0,0' >
16         </ object >
17       </ font >   
18       < form  name ="frm"  id ="frm" >
19         < input  type ="text"  name ="txt"  value ="enter text here" >
20         < input  type =button  value ="Click me"  onclick ="doScript();" >
21       
22        </ form >
23    < hr >
24   </ body >   
25
26 </ html >
27

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

下一次再说说cab包的制作问题,即如何实现客户端下载安装的实现。
我们知道当使用C++, COM和ATL来开发ActiveX的时候得CAB文件里面的INF文件描述了如何注册以及复制到何处的指令。
但是当我们用C#来开发ActiveX的时候,INF文件里面Registerserver=Yes 不起作用。因为这个命令只是调用regsvr32 来注册相应的组件,他对C#开发的ActiveX不起作用。

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



在Setup的Added output file的属性窗口里面,确认将Register property 设置成vsdrpCOM, 这个设置确保了在安装的时候会
调用regasm 来注册ActiveX控件。

2、CAB文件的建立

1 [version]
2 signature="$CHICAGO$"
3 AdvancedINF=2.0
4 [Setup Hooks]
5 hook1=hook1
6
7 [hook1]
8 run=msiexec.exe /i "%EXTRACT_DIR%/ActiveXDotDownload.msi" /qn




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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值