用C#編寫ActiveX控制項的一些操作

來源:互聯網
上載者:User
文章目錄
  • Handling Events
本文是接紅馬天下  用C#編寫ActiveX控制項(二)http://www.cnblogs.com/homer/archive/2005/01/08/88780.html

我做了一個控制項裡可以調用頁面的JS的樣本。步驟如下:

1、在HelloWorld項目裡添加Com引用。 檔案是MSHTML.TLB ,

2、添加一個方法Init
public void InitFCO(object obj)

        {

            mshtml.HTMLWindow2Class html = (mshtml.HTMLWindow2Class)obj;

            html.execScript("test();", "javascript");

        }

3、頁面上的操作是:
<HTML>

<HEAD>

<TITLE> New Document </TITLE>

<META NAME="Generator" CONTENT="EditPlus">

<META NAME="Author" CONTENT="">

<META NAME="Keywords" CONTENT="">

<META NAME="Description" CONTENT="">

<SCRIPT LANGUAGE="JavaScript">

<!--

function test()

{

    alert("成功了!!!");

}

//-->

</SCRIPT>

</HEAD>


<BODY>

<OBJECT ID="helloworld"  name="helloworld"

WIDTH="900" HEIGHT="120" 

CLASSID="CLSID:05B2AE76-0666-4abd-8D68-3E79A67D3F90" 

codebase="http://10.168.188.5/debug/HelloWorld.dll#Version=5,0,0,0"

align="baseline"

style="position:absolute; left:0; top:0; "

VIEWASTEXT>

    <SPAN STYLE="color:red">ActiveX 控制項下載失敗!-- 請檢查瀏覽器的安全設定。<br><br>

                ActiveX control failed to load! -- Please check browser security settings.</SPAN>

</OBJECT>

<BR><BR><BR><BR><BR><BR><BR>

<input type='button' onclick='helloworld.ShowMessage("Hello World!")' value='Hello World!'> <BR>

<INPUT TYPE="button" onclick='helloworld.InitFCO(window)' value='InitFCO' >

</BODY>

</HTML>


這樣就可以實現控制項調用JS或VBS了。很方便。

下面講一下發布的問題:

1、數位簽章。使用SignCode.exe進行數位簽章,步驟如下:

首先要有工具包,包括以下幾個軟體:(c:\Program Files\Microsoft Visual Studio .NET 2003\Common7\Tools\Bin目錄下都有)

makecert.exe  製作cer格式的認證,即X.509認證,同時可以建立私密金鑰

cert2spc.exe  將cer格式認證轉換成spc格式認證,即PKCS #7認證

signcode.exe  將認證簽署到ocx上去

chktrust.exe  檢查簽署認證後的ocx是否正確

還有一個certmgr.exe,是管理憑證用的。可以從這裡面匯出root.cer來,

網上很多文章寫到這個認證,但是在VC的安裝盤中卻找不到。其實,沒

有也沒關係的。這幾個軟體可以從VC的安裝盤中找到。

下面是具體的步驟:

1、建立一個自己的認證檔案:

makecert /sv "Record.PVK" /n "CN=test" test.cer

這裡,Record.PVK表示新建立的私人密鑰儲存檔案名稱

      DreamCaptial是你想顯示的公司名

      test.cer是你建立最後的認證檔案名稱

這些根據你自己的要求填寫,最後得到Record.PVK和test.cer兩個檔案。

其中,運行過程中需要輸入私人密鑰的保護密碼,一定要輸入一致,不要

出錯。

2、轉換cer格式為spc格式(可以省略)

cert2spc test.cer test.spc

得到test.spc檔案。

3、給ocx進行簽名

運行signcode,命令列的我沒有實驗通過,我是通過介面實現的。

signcode運行後會出現數位簽章嚮導,首先選擇你要簽名的ocx,

下一步後會出現簽名選項,一種是典型,一種是自訂。選擇自訂,

這樣才能從檔案選擇認證,選擇前面製作的dream.spc,再下一步是

選擇私密金鑰檔案,選擇Record.PVK,輸入私人密鑰的保護密碼,選擇散

列演算法,一般用md5就可以了,下一步是選擇其他認證,直接下一步,

填寫一下這個控制項的聲明,使用者用ie瀏覽的時候,會彈出認證說明,

再下一步是加蓋時間戳記,如果需要,用以下地址:

http://timestamp.verisign.com/scripts/timstamp.dll

要求已經上網並能出國,然後直接下一步就完成了。

4、用chktrust檢查是否正確

chktrust -v RecordProj.ocx

就這樣,得到了一個測試認證,恩,雖然只是一個測試認證,但至

少保證這個ocx在ie瀏覽的時候能夠彈出來一個視窗,問你是否安裝,

而不是直接禁止了。

增加控制項的事件

Handling Events

      The last hurdle is to add event notifications to my Windows Forms control, so that it can inform its container when downloading has started, stopped, or aborted due to an internal error.

      In the COM/ActiveX world, you add events by defining an event interface (also called a source interface). Controls that want to throw these events implement the interface, and their callers sink the interface—to register themselves as event handlers—using the Advise method of the IConnectionPoint corresponding to the control's default source interface. Regular .NET classes simplify this task greatly. Events no longer need to be grouped into interfaces, and listeners are provided for individual events using a type-safe function pointer called a delegate.

      As of version 6.0, Internet Explorer provides no seamless translation between these two mechanisms. The best you can do is to use COM Interoperability to define a default source interface. Internet Explorer will sink a control's default source interface and expose event handlers through an IDispatch interface based on a dispid assigned to the events. This requires you to define, COM-style, an interface that implements the events:

[Guid("A59B958D-B363-454b-88AA-BE8626A131FB")][InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]public interface IMultiUploadCtrlCOMEvents{    [DispId(0x60020000)]    void UploadComplete();    [DispId(0x60020001)]    void BeginUpload();}

      It also requires you to define all of the control's public methods and properties on a separate interface, otherwise, Internet Explorer will attempt to resolve all method and property calls made by script against the source interface, with obvious bad consequences.

public interface IMultiUploadCtrlCOMIncoming{    void UploadFiles();    bool FilesPending {get;}    int MaxSessionUpload {get; set; }    int BytesUploaded {get;}    string FileUploadURL {get; set; }}

From there, you define and raise an event just as you would any regular .NET event. To sink, you must use the DHTML <SCRIPT> tag with the FOR attribute:

<SCRIPT FOR="upload1" EVENT="BeginUpload">    window.status = "Uploading files...please wait";</SCRIPT><SCRIPT FOR="upload1" EVENT="UploadComplete">    window.alert("Upload complete");    window.status = "";</SCRIPT>

Hence the reason for requiring unmanaged code execution permissions for my control: when I raise an event, I am calling unmanaged listeners written in JScript or VBScript.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.