麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > C# > 正文

Visual C#創(chuàng)建和使用ActiveX組件

2023-05-18 12:33:13
字體:
供稿:網(wǎng)友

  開發(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)目】對話框

  7.選擇【解決方案資源管理器】窗口,并從中上傳Class1.cs文件,因?yàn)榇宋募诒境绦蛑幸呀?jīng)沒有用途了。

  8.選擇【項(xiàng)目】|【添加組件】后,彈出【添加新項(xiàng)】對話框,在此對話框中設(shè)定【模板】為"組件類",設(shè)定【名稱】值為"MyControl.cs"后,單擊【打開】按鈕。則在項(xiàng)目文件中新增一個(gè)名稱"MyControl.cs"的文件。具體如圖02所示:
 

圖02:在項(xiàng)目中【添加新項(xiàng)】對話框
四.Visual C#中使用剛封裝的Active X組件:

  以下步驟就是通過Web頁面的方式來測試上面創(chuàng)建組件:

  1. 創(chuàng)建一個(gè)名稱為test.htm文件,MyControl就是放在此Web頁面中加以測試的,此文件的內(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 = "請輸入數(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ī)器的虛擬目錄下,一般來說虛擬目錄是"C:/Inetpub/wwwroot"。

  3. 打開瀏覽器,在瀏覽器的地址欄中輸入"http://localhost/test.htm"后,單擊"轉(zhuǎn)到"按鈕,則會得到如下的運(yùn)行界面:
 

圖04:測試用Visual C#產(chǎn)生的Active X組件的運(yùn)行界面

  至此Visual C#產(chǎn)生的Active X組件和測試這個(gè)組件的全部工作就完成了。

  五.總結(jié):

  雖然本文介紹的方法的確能夠方便的解決Web頁面中很多棘手的問題,本文介紹用Visual C#產(chǎn)生的組件的在實(shí)用性上的確非常的類似Active X組件,但從本質(zhì)上說,本文產(chǎn)生的組件并不是真正意義上的Active X組件。如要使用本文所創(chuàng)建的組件,必須在Web頁面所在機(jī)器上安裝.Net框架,客戶端訪問Web頁面時(shí),也不會真正下載本文介紹的組件,從而也不需要設(shè)定計(jì)算機(jī)的安全級別就能夠訪問使用此組件的Web頁面??梢姳疚漠a(chǎn)生的組件其實(shí)質(zhì)也是一個(gè)托管的代碼文件。它只是巧妙的用定義接口的方式來告訴COM/COM+對象,本組件有一個(gè)可供訪問的公用屬性,通過對此屬性的讀寫操作,完成類似Active X組件的工作。
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過程,下列代碼是初始化上述加入的組件:
 
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組件的界面就基本完成了,具體如圖03所示:
 

圖03:【ActiveXDotNet】項(xiàng)目創(chuàng)建的Active X組件的設(shè)計(jì)界面

  11. 在MyControl.cs中的【MyControl 的摘要說明】代碼區(qū)中添加下列代碼,以下代碼是定義一個(gè)公用的接口,此接口是告訴COM/COM+,這兒有一個(gè)公用的屬性可以進(jìn)行讀寫操作:
 
public interface AxMyControl
{
 String UserText { set ; get ; }
}

  12. 在MyControl.cs的【MyControl】class代碼區(qū)中添加下列代碼,以下代碼是首先定義一個(gè)私有的字符串,用此字符串來保存從Web測試頁面中傳遞來的數(shù)值定義一個(gè)公用屬性,在接下來的Web測試頁面中,將通過這個(gè)屬性來傳遞數(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#會自動完成編譯,并在"C:/Class/ActiveXDotNet/bin/Debug"目錄生成一個(gè)名稱為"ActiveXDotNet.dll"文件,這就是產(chǎn)生的組件。

  以下是經(jīng)過上述步驟產(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 的摘要說明。
 /// </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
 }
}
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 羞羞答答影院 | 免费一级肉体全黄毛片 | 亚洲精品成人在线视频 | 成人一级毛片 | 国产一区二区三区视频观看 | 中文字幕精品在线观看 | 日本在线视 | 黄色片在线观看网站 | 国产精品片一区二区三区 | 国产精品探花在线观看 | 西川av在线一区二区三区 | 午夜视频在线免费观看 | 中文字幕免费一区 | 国产一级一国产一级毛片 | 国产精品99一区二区 | www.91在线| 3344永久免费 | 久草成人在线 | 欧美亚洲另类在线 | 亚洲嫩草av | 亚洲xxx视频 | 日本高清电影在线播放 | 免费看成年人视频在线 | 久章草影院 | 亚洲成人在线视频网 | 88xx成人永久免费观看 | 在线天堂中文在线资源网 | 成人啪啪18免费网站 | 成人在线视频免费观看 | 91久久久国产精品 | 在线观看免费精品 | 国产一区二区三区高清 | 久久久久久久免费看 | 香蕉在线播放 | 黄色av网 | 成人mm视频在线观看 | 激情影院在线观看 | 羞羞答答视频 | 国产欧美在线一区二区三区 | 毛片免费视频播放 | 久久精品之 |