Visual C#建立和使用ActiveX組件(圖)

來源:互聯網
上載者:User

轉:http://tech.sina.com.cn/s/2005-12-26/1030802375.shtml
開發基於.Net平台上的程式員是很難從本質上把Visual C#和ActiveX組件聯起來,雖然在使用Visual C#開發應用程式時,有時為了快速開發或者由於.Net FrameWork SDK的不完整,還需要藉助ActiveX。但即使如此,也很難把二者聯絡起來。其中的原因就是能夠被Visual C#直接使用檔案和通過Visual C#產生的可執行程式只可能是託管的檔案。而Active X組件卻都是非託管檔案。這種檔案的差異決定了二者本質"對立"。於是這就引出了本文第一個問題,ActiveX和Visual C#到底是何種關係。
 
  一.Visual C#和Active X組件

  此時可能有些朋友會說,既然能夠被Visual C#直接使用只能是Managed 程式碼檔案,那在Visual C#中提供的可直接通過引用調用ActiveX又是怎麼回事?的確Visual C#提供了引用ActiveX組件的操作,這種操作有效利用了很多以前資源,使得這些資源並沒有隨著微軟推出.Net平台而由於平台的差異被"拋棄",但這種在Visual C#中引入ActiveX組件的操作其實並不被微軟公司所倡導,也不符合微軟推出.Net的最終目的。這是因為微軟之所以推出.Net是為了實現跨平台,為了實現"Write Once and Run Anywhere",寫一遍代碼,可以在任何平台上啟動並執行目的。如果程式中使用了Active X組件,這也就從另一方面決定了此程式只能在Windows平台上使用,也就無法實現微軟的"Write Once and Run Anywhere"最終目標了。

  再者Visual C#提供的引用ActiveX組件的操作,其實Active X組件被加入Visual C#的"工具箱"時,Visual Stuio .Net其實對ActiveX組件進行了很多操作,而這些操作又都被Visual C#隱藏了,使用者往往並不完全清楚。這些操作的作用就是把非託管的ActiveX組件轉換成託管的組件,這些操作統稱"互操作",細心的程式員可能就會發現,當往程式表單中拖入ActiveX組件後,來源程式所在目錄的"Bin"目錄中就會新增若干個"Dll"檔案,這些檔案就是Active X組件進行互操作轉換後產生的。此時在Visual C#使用的並不是ActiveX組件,而是由ActiveX組件進行互操作得到可供.Net平台使用的、功能和原先ActiveX組件相同的類庫了。

  既然在Visual C#中不能直接使用ActiveX組件,那種看似在Visual C#中使用的ActiveX組件其實使用的是經過了互操作後轉換的類庫。那麼Visual C#是否能夠產生Active X組件?本文就來探討一下Visual C#中產生ActiveX組件的實現方法。製作的方法就是首先通過Visual C#建立一個Windows組件,然後把其介面以COM形式發布即可。

  二.本文中介紹的程式設計及運行環境

  (1).微軟視窗2000 伺服器版。
 
  (2).Visual Studio .Net 2003企業結構版,.Net Framework SDK 4322。

  三.使用Visual C#建立Windows組件

  以下是使用Visual C#建立一個Windows組件的實現步驟:

  1.啟動Visual Studio .Net。

  2.選擇菜單【檔案】|【建立】|【項目】後,彈出【建立項目】對話方塊。

  3.將【項目類型】設定為【Visual C#項目】。

  4.將【模板】設定為【類庫】。

  5.在【名稱】文字框中輸入【ActiveXDotNet】。

  6.在【位置】的文字框中輸入【C:\Class】,然後單擊【確定】按鈕,則Visual C#則在"C:\Class"目錄中建立"ActiveXDotNet"檔案夾,裡面存放的是ActiveXDotNet專案檔,具體01所示:


圖01:建立類庫的【建立項目】對話方塊

  7.選擇【方案總管】視窗,並從中上傳Class1.cs檔案,因為此檔案在本程式中已經沒有用途了。

  8.選擇【項目】|【添加組件】後,彈出【添加新項】對話方塊,在此對話方塊中設定【模板】為"組件類",設定【名稱】值為"MyControl.cs"後,單擊【開啟】按鈕。則在專案檔中新增一個名稱"MyControl.cs"的檔案。具體02所示:


圖02:在項目中【添加新項】對話方塊

9. 把Visual Studio .Net的當前視窗切換到【MyControl.cs(設計)】視窗,並從【工具箱】中的【Windows表單組件】選項卡中往設計表單中按順序拖入下列組件:

  一個GroupBox組件,並向此組件中再拖入,

  一個TextBox組件和一個Lable組件。

  10. 把Visual Studio .Net的當前視窗切換到【MyControl.cs】代碼編輯視窗,並用下列代碼替換MyControl.cs中的InitializeComponent過程,下列代碼是初始化上述加入的組件:

private void InitializeComponent ( )
{
 this.groupBox1 = new System.Windows.Forms.GroupBox ( ) ;
 this.txtUserText = new System.Windows.Forms.TextBox ( ) ;
 this.label1 = new System.Windows.Forms.Label ( ) ;
 this.groupBox1.SuspendLayout ( ) ;
 this.SuspendLayout ( ) ;
 this.groupBox1.Controls.Add ( this.txtUserText ) ;
 this.groupBox1.Controls.Add ( this.label1 ) ;
 this.groupBox1.Location = new System.Drawing.Point ( 8 , 8 ) ;
 this.groupBox1.Name = "groupBox1" ;
 this.groupBox1.Size = new System.Drawing.Size ( 272 , 56 ) ;
 this.groupBox1.TabIndex = 0 ;
 this.groupBox1.TabStop = false ;
 this.groupBox1.Text = "Visual Studio .Net建立的Active X組件" ;
 this.txtUserText.Enabled = false ;
 this.txtUserText.Location = new System.Drawing.Point ( 84 , 20 ) ;
 this.txtUserText.Name = "txtUserText" ;
 this.txtUserText.Size = new System.Drawing.Size ( 180 , 21 ) ;
 this.txtUserText.TabIndex = 1 ;
 this.txtUserText.Text = "" ;
 this.label1.Location = new System.Drawing.Point ( 8 , 24 ) ;
 this.label1.Name = "label1" ;
 this.label1.Size = new System.Drawing.Size ( 66 , 16 ) ;
 this.label1.TabIndex = 0 ;
 this.label1.Text = "輸入資訊:" ;
 this.Controls.Add ( this.groupBox1 ) ;
 this.Name = "MyControl" ;
 this.Size = new System.Drawing.Size ( 288 , 72 ) ;
 this.groupBox1.ResumeLayout ( false ) ;
 this.ResumeLayout ( false ) ;
}

  至此【ActiveXDotNet】項目建立的Active X組件的介面就基本完成了,具體03所示:


圖03:【ActiveXDotNet】項目建立的Active X組件的設計介面

  11. 在MyControl.cs中的【MyControl 的摘要說明】代碼區中添加下列代碼,以下代碼是定義一個公用的介面,此介面是告訴COM/COM+,這兒有一個公用的屬性可以進行讀寫操作:

public interface AxMyControl
{
 String UserText { set ; get ; }
}

  12. 在MyControl.cs的【MyControl】class代碼區中添加下列代碼,以下代碼是首先定義一個私人的字串,用此字串來儲存從Web測試頁面中傳遞來的數值定義一個公用屬性,在接下來的Web測試頁面中,將通過這個屬性來傳遞數值,此屬性是可讀寫:

private String mStr_UserText ;
public String UserText
{
 get { return mStr_UserText ; }
 set
 {
  mStr_UserText = value ;
  //修改組件的數值
  txtUserText.Text = value ;
 }
}

  13. 儲存上面的修改步驟,至此我們就利用Visual C#建立了一個名稱為MyControl的class,這也就是用Visual C#封裝的、酷似Active X組件的組件。

  14. 單擊快速鍵【Ctrl+F5】,則Visual C#會自動完成編譯,並在"C:\Class\ActiveXDotNet\bin\Debug"目錄產生一個名稱為"ActiveXDotNet.dll"檔案,這就是產生的組件。

  以下是經過上述步驟產生的MyControl.cs的全部代碼:

using System ;
using System.Collections ;
using System.ComponentModel ;
using System.Drawing ;
using System.Data ;
using System.Windows.Forms ;
namespace ActiveXDotNet
{
 public interface AxMyControl
 {
  String UserText { set ; get ; }
 }
 /// <summary>
 /// MyControl 的摘要說明。
 /// </summary>
 public class MyControl : System.Windows.Forms.UserControl , AxMyControl
 {
  /// <summary>
  /// 必需的設計器變數。
  /// </summary>
  private System.ComponentModel.Container components = null ;
  private System.Windows.Forms.GroupBox groupBox1 ;
  private System.Windows.Forms.Label label1 ;
  private System.Windows.Forms.TextBox txtUserText ;
  private String mStr_UserText ;
  public String UserText
  {
   get { return mStr_UserText ; }
   set
   {
    mStr_UserText = value ;
    //修改組件的數值
    txtUserText.Text = value ;
   }
  }
  public MyControl ( )
  {
   // 該調用是 Windows.Forms 表單設計器所必需的。
   InitializeComponent ( ) ;

   // TODO: 在 InitializeComponent 調用後添加任何初始化
  }
  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose ( bool disposing )
  {
   if ( disposing )
   {
    if ( components != null )
    {
     components.Dispose ( ) ;
    }
   }
   base.Dispose ( disposing ) ;
  }
  #region 組件設計器產生的程式碼
  /// <summary>
  /// 設計器支援所需的方法 - 不要使用代碼編輯器
  /// 修改此方法的內容。
  /// </summary>
  private void InitializeComponent ( )
  {
   this.groupBox1 = new System.Windows.Forms.GroupBox ( ) ;
   this.txtUserText = new System.Windows.Forms.TextBox ( ) ;
   this.label1 = new System.Windows.Forms.Label ( ) ;
   this.groupBox1.SuspendLayout ( ) ;
   this.SuspendLayout ( ) ;
   this.groupBox1.Controls.Add ( this.txtUserText ) ;
   this.groupBox1.Controls.Add ( this.label1 ) ;
   this.groupBox1.Location = new System.Drawing.Point ( 8 , 8 ) ;
   this.groupBox1.Name = "groupBox1" ;
   this.groupBox1.Size = new System.Drawing.Size ( 272 , 56 ) ;
   this.groupBox1.TabIndex = 0 ;
   this.groupBox1.TabStop = false ;
   this.groupBox1.Text = "Visual C#建立的Active X組件" ;
   this.txtUserText.Enabled = false ;
   this.txtUserText.Location = new System.Drawing.Point ( 84 , 20 ) ;
   this.txtUserText.Name = "txtUserText" ;
   this.txtUserText.Size = new System.Drawing.Size ( 180 , 21 ) ;
   this.txtUserText.TabIndex = 1 ;
   this.txtUserText.Text = "" ;
   this.label1.Location = new System.Drawing.Point ( 8 , 24 ) ;
   this.label1.Name = "label1" ;
   this.label1.Size = new System.Drawing.Size ( 66 , 16 ) ;
   this.label1.TabIndex = 0 ;
   this.label1.Text = "輸入資訊:" ;
   this.Controls.Add ( this.groupBox1 ) ;
   this.Name = "MyControl" ;
   this.Size = new System.Drawing.Size ( 288 , 72 ) ;
   this.groupBox1.ResumeLayout ( false ) ;
   this.ResumeLayout ( false ) ;
  }
  #endregion
 }
}

四.Visual C#中使用剛封裝的Active X組件:

  以下步驟就是通過Web頁面的方式來測試上面建立組件:

  1. 建立一個名稱為test.htm檔案,MyControl就是放在此Web頁面中加以測試的,此檔案的內容如下:

<html>
<body color = white>
<hr>

<font face = arial size = 1>
<OBJECT id = "MyControl1" name = "MyControl1" classid = "ActiveXDotNet.dll#ActiveXDotNet.MyControl" width = 288 height = 72 >
</OBJECT>
</font>

<form name = "frm" id = "frm" >
<input type = "text" name = "txt" value = "請輸入資料:" ><input type = button value = "確定" onClick = "doScript ( ) ; ">
</form>
<hr>
</body>

<script language = "javascript">
function doScript ( )
{
 MyControl1.UserText = frm.txt.value ;
}
</script>
</html>

  2. 把產生的"test.htm"和"ActiveXDotNet.dll"檔案全部都拷貝到機器的虛擬目錄下,一般來說虛擬目錄是"C:\Inetpub\wwwroot"。

  3. 開啟瀏覽器,在瀏覽器的地址欄中輸入"http://localhost/test.htm"後,單擊"轉到"按鈕,則會得到如下的運行介面:


圖04:測試用Visual C#產生的Active X組件的運行介面

  至此Visual C#產生的Active X組件和測試這個組件的全部工作就完成了。

  五.總結:

  雖然本文介紹的方法的確能夠方便的解決Web頁面中很多棘手的問題,本文介紹用Visual C#產生的組件的在實用性上的確非常的類似Active X組件,但從本質上說,本文產生的組件並不是真正意義上的Active X組件。如要使用本文所建立的組件,必須在Web頁面所在機器上安裝.Net架構,用戶端訪問Web頁面時,也不會真正下載本文介紹的組件,從而也不需要設定電腦的安全層級就能夠訪問使用此組件的Web頁面。可見本文產生的組件其實質也是一個託管的代碼檔案。它只是巧妙的用定義介面的方式來告訴COM/COM+對象,本組件有一個可供訪問的公用屬性,通過對此屬性的讀寫操作,完成類似Active X組件的工作

相關文章

聯繫我們

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