VS 2012 C#ActiveX控制項開發總結

來源:互聯網
上載者:User

標籤:

       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控制項開發總結

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.