C# 編寫的ActiveX控件


  以前從未接觸過ActiveX,今天做個整理。

首先ActiveX是用來擴充豐富在web上的應用,即使自己在winform下編寫的用戶控件,它也能夠在網頁上發揮作用(好吧,ActiveX毀了我的世界觀)。

  接着,我們創建一個windows窗體控件庫,代碼設計如下

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

namespace ActiveXSample1
{
    [Guid("0e8c5166-c7f6-48dc-8519-a9fb8f964a7f")]
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public string ShowMsg()
        {
            return "HelloWorld:I am Heaven";
        }
    }
}

至於為什么使用GUID,我個人認為那是為了在網頁中能准確地找到程序集。編譯之后,我們會得到一個ActiveXSample1.dll,將之拷貝到一個asp.net項目下,在該項目下創建一個網頁webform.aspx,網頁代碼如下

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="button" onclick="alert(helloworld.ShowMsg());" />
        <object id="helloworld" classid="clsid:0e8c5166-c7f6-48dc-8519-a9fb8f964a7f"/>
    </div>
    </form>
</body>
</html>

注意object標記,其中classid就是剛才我們使用的GUID。程序寫到這里可以運行一下,能看到用戶控件的外觀。但是,當想要用html元素調用ActiveX內的方法是,這兒網頁要彈出一些提示,這兒必須上圖,應為這里我卡了很久,相信有很多同學也卡在這里

要能夠使用button,那么在IE,Internet選項中,安全選項卡,在本地Intranet中如圖設置,

這還不夠,可信站點中添加本站點,,如此便可運行!

如果不能用input調用ActiveX的方法,那么主要還是在internet選項的設置上,如果沒能了解清楚的同學多在這上面琢磨琢磨!這是一種比較拙劣的方法,測試一下ActiveX控件可以,但是實際運用中不這樣,因為這樣很不安全,較為安全的做法,那就是給ActiveX簽名加證書。如何給ActiveX加證書請看最后!

還有注意:win764位機上你可能裝了IE64位,64位目前是不能夠現實ActiveX的,現在我的電腦上有64位和32位版本的IE,64位的無法顯示,詳細請看http://www.iefans.net/activex-kongjian-wufa-jiazai/

 

另外,為了安全起見,必須實現IObjectSafety接口

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

namespace ActiveXSample1
{
    [Guid("c44c9c1a-e905-43d0-8792-b1c1e80b869d")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IObjectSafety
    {
        [PreserveSig]
        void GetInterfacceSafyOptions(
        int riid,
        out int pdwSupportedOptions,
        out int pdwEnabledOptions);

        [PreserveSig]
        void SetInterfaceSafetyOptions(
        int riid,
        int dwOptionsSetMask,
        int dwEnabledOptions);
    }
}

首先說打包成cab文件,我們需要一套工具,工具在這里http://files.cnblogs.com/HelloMyWorld/Cab%E5%88%B6%E4%BD%9C%E5%B7%A5%E5%85%B7.rar

具體教程在這里http://blog.csdn.net/logenliqiang/article/details/5897204

根據上面的教程可以制作cab文件

現在我們來簽名,運行工具包里的signcode.exe,選中我們的cab文件

然后簽名完成。

在放到web服務器上運行,不用修改安全選項,它會提示你ActiveX已被阻止安裝,那是因為沒有安裝證書(小可是在虛擬機里測試的,要在虛擬機里加入證書),將證書導入到2個區域

一個是

另一個是

導入后再重新加載頁面,發現不用修改安全選項也能顯示ActiveX控件了

 

 參考網頁

http://yusy1116.blog.163.com/blog/static/6467259220103120151695/

http://www.cnblogs.com/prowyh/archive/2012/12/04/2802275.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM