.NET流水账

一个真正的开明进步的国家,不是一群奴才造成的,是要有独立个性,有自由思考的人造成的。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
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控件就开发完成了,下一次讲解具体的调用方法。
posted on 2007-08-01 11:49  DalianGary  阅读(5867)  评论(9编辑  收藏  举报