当前位置:编程学习 > asp >>

C#.NET ActiveX控件的制作

第一步:新建项目,如下图,选择windows下的类库项目。

第二步:在项目中添加一个类:IObjectSafety.cs 如下图:

IObjectSafety.cs代码如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace MyActiveX
{
//Guid唯一,不可变更,否则将无法通过IE浏览器的ActiveX控件的安全认证  
 [ComImport, Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectSafety
    {
        [PreserveSig]
        void GetInterfacceSafyOptions(
        int riid,
        out int pdwSupportedOptions,
        out int pdwEnabledOptions);

第三步:添加一个用户控件 MyActiveXControl.cs 如下图

修改 MyActiveXControl.cs 代码,让其继承IObjectSafety,定义相应的Guid,该Guid就是ActiveX的classid
using 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;

namespace MyActiveX
{
    [Guid("218849AF-1B2C-457B-ACD5-B42AC8D17EB7"), ComVisible(true)]
    public partial class MyActiveXControl : UserControl,IObjectSafety
    {
        public MyActiveXControl()
        {
            InitializeComponent();
        }

        #region IObjectSafety 成员 用于ActiveX控件安全信任
        public void GetInterfacceSafyOptions(int riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
        {
            pdwSupportedOptions = 1;
            pdwEnabledOptions = 2;
        }

        public void SetInterfaceSafetyOptions(int riid, int dwOptionsSetMask, int dwEnabledOptions)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}

第四步:添加一个按钮,用于测试ActiveX控件,如下图:

为该按钮添加事件:
private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("powered by yyzq.net 叶知秋 yq@yyzq.net");
        }
至此,代码编写部分完成。
第五步:我们来制作一个安装包,新建一个安装项目,如下图:

第六步:在安装项目的文件系统中添加刚才之前我们制作的ActiveX的DLL:MyActiveX.dll
(特别注意:在文件添加进来后,右击文件选择属性,设置其属性Register值为:vsdraCOM),如下图:

第七步:生成安装程序,在项目MyActiveX\Setup1\Debug下找到Setup1.msi,双击安装它。
然后在该目录下新建一个html文件(test.html)用于测试我们的ActiceX控件。HTML代码如下:
<html>
<title>Powered by yyzq.net Email:yq@yyzq.net</title>
<head>
</head>
<body>
<div>
<object id="yyzq" classid="clsid:218849AF-1B2C-457B-ACD5-B42AC8D17EB7"
        width="320"
        height="240"
        codebase="Setup1.msi">
</object>
</div>
</body>
</html>
在IE浏览器下打开test.html,点击按钮


补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,