開發基于.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#直接使用只能是托管代碼文件,那在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項目文件
7.選擇【解決方案資源管理器】窗口,并從中上傳Class1.cs文件,因為此文件在本程序中已經沒有用途了。
8.選擇【項目】|【添加組件】后,彈出【添加新項】對話框,在此對話框中設定【模板】為"組件類",設定【名稱】值為"MyControl.cs"后,單擊【打開】按鈕。則在項目文件中新增一個名稱"MyControl.cs"的文件。
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組件的界面就基本完成了
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"后,單擊"轉到"按鈕
至此Visual C#產生的Active X組件和測試這個組件的全部工作就完成了。
五.總結:
雖然本文介紹的方法的確能夠方便的解決Web頁面中很多棘手的問題,本文介紹用Visual C#產生的組件的在實用性上的確非常的類似Active X組件,但從本質上說,本文產生的組件并不是真正意義上的Active X組件。如要使用本文所創建的組件,必須在Web頁面所在機器上安裝.Net框架,客戶端訪問Web頁面時,也不會真正下載本文介紹的組件,從而也不需要設定計算機的安全級別就能夠訪問使用此組件的Web頁面。可見本文產生的組件其實質也是一個托管的代碼文件。它只是巧妙的用定義接口的方式來告訴COM/COM+對象,本組件有一個可供訪問的公用屬性,通過對此屬性的讀寫操作,完成類似Active X組件的工作。
http://www.companysz.com/tanghuawei/archive/2006/11/02/547666.html
新聞熱點
疑難解答