SuperBug

博客园 首页 联系 订阅 管理

步骤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接口在下篇中介绍。

posted on 2012-06-13 01:18  SuperBug  阅读(1599)  评论(0编辑  收藏  举报