開發(fā)基于.Net平臺(tái)上的程序員是很難從本質(zhì)上把Visual C#和ActiveX組件聯(lián)起來(lái),雖然在使用Visual C#開發(fā)應(yīng)用程序時(shí),有時(shí)為了快速開發(fā)或者由于.Net FrameWork SDK的不完整,還需要借助ActiveX。但即使如此,也很難把二者聯(lián)系起來(lái)。
其中的原因就是能夠被Visual C#直接使用文件和通過(guò)Visual C#生成的可執(zhí)行程序只可能是托管的文件。而Active X組件卻都是非托管文件。這種文件的差異決定了二者本質(zhì)"對(duì)立"。于是這就引出了本文第一個(gè)問(wèn)題,ActiveX和Visual C#到底是何種關(guān)系。
一.Visual C#和Active X組件:
此時(shí)可能有些朋友會(huì)說(shuō),既然能夠被Visual C#直接使用只能是托管代碼文件,那在Visual C#中提供的可直接通過(guò)引用調(diào)用ActiveX又是怎么回事?的確Visual C#提供了引用ActiveX組件的操作,這種操作有效的利用了很多以前資源,使得這些資源并沒(méi)有隨著微軟推出.Net平臺(tái)而由于平臺(tái)的差異被"拋棄",但這種在Visual C#中引入ActiveX組件的操作其實(shí)并不被微軟公司所倡導(dǎo),也不符合微軟推出.Net的最終目的。這是因?yàn)槲④浿酝瞥?Net是為了實(shí)現(xiàn)跨平臺(tái),為了實(shí)現(xiàn)"Write Once and Run Anywhere",寫一遍代碼,可以在任何平臺(tái)上運(yùn)行的目的。如果程序中使用了Active X組件,這也就從另一方面決定了此程序只能在Windows平臺(tái)上使用,也就無(wú)法實(shí)現(xiàn)微軟的"Write Once and Run Anywhere"最終目標(biāo)了。
再者Visual C#提供的引用ActiveX組件的操作,其實(shí)Active X組件被加入Visual C#的"工具箱"時(shí),Visual Stuio .Net其實(shí)對(duì)ActiveX組件進(jìn)行了很多操作,而這些操作又都被Visual C#隱藏了,使用者往往并不完全清楚。這些操作的作用就是把非托管的ActiveX組件轉(zhuǎn)換成托管的組件,這些操作統(tǒng)稱"互操作",細(xì)心的程序員可能就會(huì)發(fā)現(xiàn),當(dāng)往程序窗體中拖入ActiveX組件后,源程序所在目錄的"Bin"目錄中就會(huì)新增若干個(gè)"Dll"文件,這些文件就是Active X組件進(jìn)行互操作轉(zhuǎn)換后生成的。此時(shí)在Visual C#使用的并不是ActiveX組件,而是由ActiveX組件進(jìn)行互操作得到可供.Net平臺(tái)使用的、功能和原先ActiveX組件相同的類庫(kù)了。
既然在Visual C#中不能直接使用ActiveX組件,那種看似在Visual C#中使用的ActiveX組件其實(shí)使用的是經(jīng)過(guò)了互操作后轉(zhuǎn)換的類庫(kù)。那么Visual C#是否能夠生成Active X組件?本文就來(lái)探討一下Visual C#中生成ActiveX組件的實(shí)現(xiàn)方法。制作的方法就是首先通過(guò)Visual C#創(chuàng)建一個(gè)Windows組件,然后把其接口以COM形式發(fā)布即可。
二.本文中介紹的程序設(shè)計(jì)及運(yùn)行環(huán)境:
(1).微軟視窗2000 服務(wù)器版。
(2).Visual Studio .Net 2003企業(yè)結(jié)構(gòu)版,.Net Framework SDK 4322。
三.使用Visual C#創(chuàng)建Windows組件:
以下是使用Visual C#創(chuàng)建一個(gè)Windows組件的實(shí)現(xiàn)步驟:
1.啟動(dòng)Visual Studio .Net。
2.選擇菜單【文件】|【新建】|【項(xiàng)目】后,彈出【新建項(xiàng)目】對(duì)話框。
3.將【項(xiàng)目類型】設(shè)置為【Visual C#項(xiàng)目】。
4.將【模板】設(shè)置為【類庫(kù)】。
5.在【名稱】文本框中輸入【ActiveXDotNet】。
6.在【位置】的文本框中輸入【C:/Class】,然后單擊【確定】按鈕,則Visual C#則在"C:/Class"目錄中創(chuàng)建"ActiveXDotNet"文件夾,里面存放的是ActiveXDotNet項(xiàng)目文件
7.選擇【解決方案資源管理器】窗口,并從中上傳Class1.cs文件,因?yàn)榇宋募诒境绦蛑幸呀?jīng)沒(méi)有用途了。
8.選擇【項(xiàng)目】|【添加組件】后,彈出【添加新項(xiàng)】對(duì)話框,在此對(duì)話框中設(shè)定【模板】為"組件類",設(shè)定【名稱】值為"MyControl.cs"后,單擊【打開】按鈕。則在項(xiàng)目文件中新增一個(gè)名稱"MyControl.cs"的文件。
9. 把Visual Studio .Net的當(dāng)前窗口切換到【MyControl.cs(設(shè)計(jì))】窗口,并從【工具箱】中的【W(wǎng)indows窗體組件】選項(xiàng)卡中往設(shè)計(jì)窗體中按順序拖入下列組件:
一個(gè)GroupBox組件,并向此組件中再拖入,
一個(gè)TextBox組件和一個(gè)Lable組件。
10. 把Visual Studio .Net的當(dāng)前窗口切換到【MyControl.cs】代碼編輯窗口,并用下列代碼替換MyControl.cs中的InitializeComponent過(guò)程,下列代碼是初始化上述加入的組件:
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創(chuàng)建的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】項(xiàng)目創(chuàng)建的Active X組件的界面就基本完成了
11. 在MyControl.cs中的【MyControl 的摘要說(shuō)明】代碼區(qū)中添加下列代碼,以下代碼是定義一個(gè)公用的接口,此接口是告訴COM/COM+,這兒有一個(gè)公用的屬性可以進(jìn)行讀寫操作:
public interface AxMyControl
{
String UserText { set ; get ; }
}
12. 在MyControl.cs的【MyControl】class代碼區(qū)中添加下列代碼,以下代碼是首先定義一個(gè)私有的字符串,用此字符串來(lái)保存從Web測(cè)試頁(yè)面中傳遞來(lái)的數(shù)值定義一個(gè)公用屬性,在接下來(lái)的Web測(cè)試頁(yè)面中,將通過(guò)這個(gè)屬性來(lái)傳遞數(shù)值,此屬性是可讀寫:
private String mStr_UserText ;
public String UserText
{
get { return mStr_UserText ; }
set
{
mStr_UserText = value ;
//修改組件的數(shù)值
txtUserText.Text = value ;
}
}
13. 保存上面的修改步驟,至此我們就利用Visual C#創(chuàng)建了一個(gè)名稱為MyControl的class,這也就是用Visual C#封裝的、酷似Active X組件的組件。
14. 單擊快捷鍵【Ctrl+F5】,則Visual C#會(huì)自動(dòng)完成編譯,并在"C:/Class/ActiveXDotNet/bin/Debug"目錄生成一個(gè)名稱為"ActiveXDotNet.dll"文件,這就是產(chǎn)生的組件。
以下是經(jīng)過(guò)上述步驟產(chǎn)生的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 的摘要說(shuō)明。
/// </summary>
public class MyControl : System.Windows.Forms.UserControl , AxMyControl
{
/// <summary>
/// 必需的設(shè)計(jì)器變量。
/// </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 ;
//修改組件的數(shù)值
txtUserText.Text = value ;
}
}
public MyControl ( )
{
// 該調(diào)用是 Windows.Forms 窗體設(shè)計(jì)器所必需的。
InitializeComponent ( ) ;
// TODO: 在 InitializeComponent 調(diào)用后添加任何初始化
}
/// <summary>
/// 清理所有正在使用的資源。
/// </summary>
protected override void Dispose ( bool disposing )
{
if ( disposing )
{
if ( components != null )
{
components.Dispose ( ) ;
}
}
base.Dispose ( disposing ) ;
}
#region 組件設(shè)計(jì)器生成的代碼
/// <summary>
/// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器
/// 修改此方法的內(nèi)容。
/// </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#創(chuàng)建的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組件:
以下步驟就是通過(guò)Web頁(yè)面的方式來(lái)測(cè)試上面創(chuàng)建組件:
1. 創(chuàng)建一個(gè)名稱為test.htm文件,MyControl就是放在此Web頁(yè)面中加以測(cè)試的,此文件的內(nèi)容如下:
<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 = "請(qǐng)輸入數(shù)據(jù):" ><input type = button value = "確定" onClick = "doScript ( ) ; ">
</form>
<hr>
</body>
<script language = "javascript">
function doScript ( )
{
MyControl1.UserText = frm.txt.value ;
}
</script>
</html>
2. 把產(chǎn)生的"test.htm"和"ActiveXDotNet.dll"文件全部都拷貝到機(jī)器的虛擬目錄下,一般來(lái)說(shuō)虛擬目錄是"C:/Inetpub/wwwroot"。
3. 打開瀏覽器,在瀏覽器的地址欄中輸入"http://localhost/test.htm"后,單擊"轉(zhuǎn)到"按鈕
至此Visual C#產(chǎn)生的Active X組件和測(cè)試這個(gè)組件的全部工作就完成了。
五.總結(jié):
雖然本文介紹的方法的確能夠方便的解決Web頁(yè)面中很多棘手的問(wèn)題,本文介紹用Visual C#產(chǎn)生的組件的在實(shí)用性上的確非常的類似Active X組件,但從本質(zhì)上說(shuō),本文產(chǎn)生的組件并不是真正意義上的Active X組件。如要使用本文所創(chuàng)建的組件,必須在Web頁(yè)面所在機(jī)器上安裝.Net框架,客戶端訪問(wèn)Web頁(yè)面時(shí),也不會(huì)真正下載本文介紹的組件,從而也不需要設(shè)定計(jì)算機(jī)的安全級(jí)別就能夠訪問(wèn)使用此組件的Web頁(yè)面。可見本文產(chǎn)生的組件其實(shí)質(zhì)也是一個(gè)托管的代碼文件。它只是巧妙的用定義接口的方式來(lái)告訴COM/COM+對(duì)象,本組件有一個(gè)可供訪問(wèn)的公用屬性,通過(guò)對(duì)此屬性的讀寫操作,完成類似Active X組件的工作。
http://www.survivalescaperooms.com/tanghuawei/archive/2006/11/02/547666.html
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注