码迷,mamicode.com
首页 > Windows程序 > 详细

VS 2012 C#ActiveX控件开发总结

时间:2016-04-19 12:16:01      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:

       ActiveX 控件以前也叫做OLE 控件或OCX 控件,它是一些软件组件或对象,可以将其插入到WEB 网页或其它应用程序中。使用ActiveX 插件,可以轻松方便的在Web 页中插入多媒体效果、交互式对象以及复杂程序等等。通常使用C++或VB 开发ActiveX 控件,本文探讨一下在Visual Studio 2012 环境中使用C#开发ActiveX 控件的技术实现。

一、ActiveX控件的开发

1、新建一个空白的解决方案

技术分享

2、在解决方案上右击→添加→新建项目→Visual C#→Windows→Windows窗体控件库,输入名称为ActiveX。

技术分享

       点击确定,生成一个名为UserControl1的控件,继承自UserControl类,在上面拖一个按钮,并写入事件响应代码:

技术分享 技术分享

3、在新建的ActiveX控件库上右键→属性→应用程序→程序集信息→使程序集COM可见(M)。再切换到“生成”标签→勾选“为COM互操作注册”。

技术分享


技术分享


技术分享

4、修改ActiveX的AssemblyInof.cs文件,添加如下代码:

技术分享

5、为Control1.cs添加内容

添加命名空间using System.Runtime.InteropServices;

为控件创建GUID:工具→创建GUID,选个5,点击复制!

技术分享

将GUID号粘贴到ActiveX.cs中,如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ActiveX
{
    [Guid("D4176A17-2A33-4903-8F37-9EBDD7CAFFD3")]
    public partial class UserControl1: UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello, GeoStorm!");
        }
    }
}
6、为了让ActiveX 控件获取客户端的信任, 控件类还需要实现一个名为“IObjectSafety”的接口。(注意:不能修改该接口的GUID 值)。

       创建接口:右键ActiveX 工程—>添加—>新建项→Visual C#项→接口

技术分享

代码如下:

using System;
using System.Runtime.InteropServices;
namespace ActiveXDemo
{
    

    [ComImport, GuidAttribute("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]//固定不变的
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectSafety
    {
        [PreserveSig]
        int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);

        [PreserveSig()]
        int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
    }
}
然后在控件类中集成并实现接口:

using System;
using System.Windows.Forms;

namespace ActiveXTest
{
    using System.Runtime.InteropServices;

    [Guid("D4176A17-2A33-4903-8F37-9EBDD7CAFFD3"), ProgId("ActiveXDemo.UserControl1"), ComVisible(true)]
    public partial class UserControl1: UserControl,IObjectSafety
    {
        
        #region IObjectSafety 成员 格式固定

        private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
        private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
        private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
        private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
        private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";

        private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
        private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
        private const int S_OK = 0;
        private const int E_FAIL = unchecked((int)0x80004005);
        private const int E_NOINTERFACE = unchecked((int)0x80004002);

        private bool _fSafeForScripting = true;
        private bool _fSafeForInitializing = true;

        public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
        {
            int Rslt = E_FAIL;

            string strGUID = riid.ToString("B");
            pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
            switch (strGUID)
            {
                case _IID_IDispatch:
                case _IID_IDispatchEx:
                    Rslt = S_OK;
                    pdwEnabledOptions = 0;
                    if (_fSafeForScripting == true)
                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
                    break;
                case _IID_IPersistStorage:
                case _IID_IPersistStream:
                case _IID_IPersistPropertyBag:
                    Rslt = S_OK;
                    pdwEnabledOptions = 0;
                    if (_fSafeForInitializing == true)
                        pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
                    break;
                default:
                    Rslt = E_NOINTERFACE;
                    break;
            }

            return Rslt;
        }

        public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
        {
            int Rslt = E_FAIL;
            string strGUID = riid.ToString("B");
            switch (strGUID)
            {
                case _IID_IDispatch:
                case _IID_IDispatchEx:
                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
                        Rslt = S_OK;
                    break;
                case _IID_IPersistStorage:
                case _IID_IPersistStream:
                case _IID_IPersistPropertyBag:
                    if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
                        Rslt = S_OK;
                    break;
                default:
                    Rslt = E_NOINTERFACE;
                    break;
            }

            return Rslt;
        }

        #endregion
        

        public UserControl1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hello, GeoStorm");
        }
    }
}
这样,一个ActiveX控件就开发完了!

二、ActiveX控件的部署

添加打包部署项目(VS2012需要单独安装InstallShield 2015 Limited Edition,并输入序列号),添加主输出,将其Register 属性设置为vsdrpCOM,完成后生成,本过程略过。

三、测试

1、新建web应用:

       文件→新建→项目→Visual C#→Web→ASP.NET空Web应用程序→WebApplication1→添加到解决方案。

技术分享

2、添加Web页面

       右键→添加→Web窗体

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <div>
    <object id="ActiveX" classid="clsid:0973CD43-BED3-4468-86E9-B0D2344F54D6" codebase="test.cab"></object>
    <input type="button" onclick="alert(ActiveX.ForDefault());" value=" 提交( 前台调用控件方法)" />
    </div>
</body>
</html>

3、在IE浏览器中查看效果

技术分享

四、ActiveX控件打包(.cab)与客户端自动安装











VS 2012 C#ActiveX控件开发总结

标签:

原文地址:http://blog.csdn.net/lucky51222/article/details/51187530

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!