ActiveX在.NET 2005中的实现(一)

本文介绍如何使用C#在.NET环境下开发ActiveX控件。通过UserControl实现了ActiveX的基本功能,并详细展示了代码实现过程及关键步骤。此外,还介绍了如何在HTML中调用这些方法。

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

Areasusing 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中实现,代码如下:

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

转载于:https://www.cnblogs.com/timy/archive/2010/03/03/1676983.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值