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

如上图所示在.NET中使用UserControl来实现ActiveX。代码如下。
2、其中,将在HTML中使用的方法在接口AxMyControl中实现,代码如下:
3、Assembly.cs中的特殊设置:
这样,一个简单的基于.NET的AticveX控件就开发完成了,下一次讲解具体的调用方法。
但该文章可能是在旧版本的.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 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
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)]
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控件就开发完成了,下一次讲解具体的调用方法。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!