開發(fā)基于.Net平臺上的程序員是很難從本質(zhì)上把Visual C#和ActiveX組件聯(lián)起來,雖然在使用Visual C#開發(fā)應(yīng)用程序時(shí),有時(shí)為了快速開發(fā)或者由于.Net FrameWork SDK的不完整,還需要借助ActiveX。但即使如此,也很難把二者聯(lián)系起來。其中的原因就是能夠被Visual C#直接使用文件和通過Visual C#生成的可執(zhí)行程序只可能是托管的文件。而Active X組件卻都是非托管文件。這種文件的差異決定了二者本質(zhì)"對立"。于是這就引出了本文第一個(gè)問題,ActiveX和Visual C#到底是何種關(guān)系。
一.Visual C#和Active X組件:
此時(shí)可能有些朋友會說,既然能夠被Visual C#直接使用只能是托管代碼文件,那在Visual C#中提供的可直接通過引用調(diào)用ActiveX又是怎么回事?的確Visual C#提供了引用ActiveX組件的操作,這種操作有效的利用了很多以前資源,使得這些資源并沒有隨著微軟推出.Net平臺而由于平臺的差異被"拋棄",但這種在Visual C#中引入ActiveX組件的操作其實(shí)并不被微軟公司所倡導(dǎo),也不符合微軟推出.Net的最終目的。這是因?yàn)槲④浿酝瞥?Net是為了實(shí)現(xiàn)跨平臺,為了實(shí)現(xiàn)"Write Once and Run Anywhere",寫一遍代碼,可以在任何平臺上運(yùn)行的目的。如果程序中使用了Active X組件,這也就從另一方面決定了此程序只能在Windows平臺上使用,也就無法實(shí)現(xiàn)微軟的"Write Once and Run Anywhere"最終目標(biāo)了。
再者Visual C#提供的引用ActiveX組件的操作,其實(shí)Active X組件被加入Visual C#的"工具箱"時(shí),Visual Stuio .Net其實(shí)對ActiveX組件進(jìn)行了很多操作,而這些操作又都被Visual C#隱藏了,使用者往往并不完全清楚。這些操作的作用就是把非托管的ActiveX組件轉(zhuǎn)換成托管的組件,這些操作統(tǒng)稱"互操作",細(xì)心的程序員可能就會發(fā)現(xiàn),當(dāng)往程序窗體中拖入ActiveX組件后,源程序所在目錄的"Bin"目錄中就會新增若干個(gè)"Dll"文件,這些文件就是Active X組件進(jìn)行互操作轉(zhuǎn)換后生成的。此時(shí)在Visual C#使用的并不是ActiveX組件,而是由ActiveX組件進(jìn)行互操作得到可供.Net平臺使用的、功能和原先ActiveX組件相同的類庫了。
既然在Visual C#中不能直接使用ActiveX組件,那種看似在Visual C#中使用的ActiveX組件其實(shí)使用的是經(jīng)過了互操作后轉(zhuǎn)換的類庫。那么Visual C#是否能夠生成Active X組件?本文就來探討一下Visual C#中生成ActiveX組件的實(shí)現(xiàn)方法。制作的方法就是首先通過Visual C#創(chuàng)建一個(gè)Windows組件,然后把其接口以COM形式發(fā)布即可。
二.本文中介紹的程序設(shè)計(jì)及運(yùn)行環(huán)境:
?。?).微軟視窗2000 服務(wù)器版?!?br /> ?。?).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.啟動Visual Studio .Net。
2.選擇菜單【文件】|【新建】|【項(xiàng)目】后,彈出【新建項(xiàng)目】對話框。
3.將【項(xiàng)目類型】設(shè)置為【Visual C#項(xiàng)目】。
4.將【模板】設(shè)置為【類庫】。
5.在【名稱】文本框中輸入【ActiveXDotNet】。
6.在【位置】的文本框中輸入【C:/Class】,然后單擊【確定】按鈕,則Visual C#則在"C:/Class"目錄中創(chuàng)建"ActiveXDotNet"文件夾,里面存放的是ActiveXDotNet項(xiàng)目文件,具體如圖01所示:
![]() 圖01:創(chuàng)建類庫的【新建項(xiàng)目】對話框 |
![]() 圖02:在項(xiàng)目中【添加新項(xiàng)】對話框 |
<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 = "請輸入數(shù)據(jù):" ><input type = button value = "確定" onClick = "doScript ( ) ; "> </form> <hr> </body> <script language = "javascript"> function doScript ( ) { MyControl1.UserText = frm.txt.value ; } </script> </html> |
![]() 圖04:測試用Visual C#產(chǎn)生的Active X組件的運(yùn)行界面 |
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 ) ; } |
![]() 圖03:【ActiveXDotNet】項(xiàng)目創(chuàng)建的Active X組件的設(shè)計(jì)界面 |
public interface AxMyControl { String UserText { set ; get ; } } |
private String mStr_UserText ; public String UserText { get { return mStr_UserText ; } set { mStr_UserText = value ; //修改組件的數(shù)值 txtUserText.Text = value ; } } |
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> /// 必需的設(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 } } |
新聞熱點(diǎn)
疑難解答
圖片精選