c# 编写Active控件------框架篇

步骤1:

新建工程

第二步:勾选Make assembly com-visible

第三步:勾选Register for com interop

 

第四步:定义接口,注意接口的属性需要为public。(右键工程,添加一个接口Item即可)

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace WindowsControlLibrary1
{
    [ComVisible(true)]
    [Guid("C2918110-A1C4-4188-ACE8-5031EE2115EB")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface Interface1
    {
        [DispId(0)]
        void Notify(string str);
    }
}

 

第五步:定义需要触发的事件。添加了一个button按钮,button按钮触发事件Notify,该事件可以被容器捕获。

using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


namespace WindowsControlLibrary1
{
    [Guid("809213D5-6646-4c87-AA41-DD0FC74F93ED")]
    [ComSourceInterfaces(typeof(Interface1))]
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }
        public delegate void test(string str);
        public event test Notify;

        private void button1_Click(object sender, EventArgs e)
        {
            Notify("Notify event");
        }
    }
}

最后:编译运行,生成Active控件。可以使用html文件测试。但是这种ActiveX控件在网页上运行时会提示不够安全,这在实现一个名为IObjectSafety的接口后提示将消失,实现IObjectSafety接口在下篇中介绍。

转载于:https://www.cnblogs.com/tclikang/archive/2012/06/13/2547169.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值