.NET流水账

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

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  17 随笔 :: 0 文章 :: 30 评论 :: 29610 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
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   DalianGary  阅读(5870)  评论(9编辑  收藏  举报
编辑推荐:
· 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 重磅开源!
点击右上角即可分享
微信分享提示