国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > 綜合 > 正文

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

2024-07-21 02:29:56
字體:
供稿:網(wǎng)友
  開發(fā)基于.net平臺上的程序員是很難從本質(zhì)上把visual c#和activex組件聯(lián)起來,雖然在使用visual c#開發(fā)應用程序時,有時為了快速開發(fā)或者由于.net framework sdk的不完整,還需要借助activex。但即使如此,也很難把二者聯(lián)系起來。其中的原因就是能夠被visual c#直接使用文件和通過visual c#生成的可執(zhí)行程序只可能是托管的文件。而active x組件卻都是非托管文件。這種文件的差異決定了二者本質(zhì)"對立"。于是這就引出了本文第一個問題,activex和visual c#到底是何種關(guān)系。
 
  一.visual c#和active x組件

  此時可能有些朋友會說,既然能夠被visual c#直接使用只能是托管代碼文件,那在visual c#中提供的可直接通過引用調(diào)用activex又是怎么回事?的確visual c#提供了引用activex組件的操作,這種操作有效的利用了很多以前資源,使得這些資源并沒有隨著微軟推出.net平臺而由于平臺的差異被"拋棄",但這種在visual c#中引入activex組件的操作其實并不被微軟公司所倡導,也不符合微軟推出.net的最終目的。這是因為微軟之所以推出.net是為了實現(xiàn)跨平臺,為了實現(xiàn)"write once and run anywhere",寫一遍代碼,可以在任何平臺上運行的目的。如果程序中使用了active x組件,這也就從另一方面決定了此程序只能在windows平臺上使用,也就無法實現(xiàn)微軟的"write once and run anywhere"最終目標了。

  再者visual c#提供的引用activex組件的操作,其實active x組件被加入visual c#的"工具箱"時,visual stuio .net其實對activex組件進行了很多操作,而這些操作又都被visual c#隱藏了,使用者往往并不完全清楚。這些操作的作用就是把非托管的activex組件轉(zhuǎn)換成托管的組件,這些操作統(tǒng)稱"互操作",細心的程序員可能就會發(fā)現(xiàn),當往程序窗體中拖入activex組件后,源程序所在目錄的"bin"目錄中就會新增若干個"dll"文件,這些文件就是active x組件進行互操作轉(zhuǎn)換后生成的。此時在visual c#使用的并不是activex組件,而是由activex組件進行互操作得到可供.net平臺使用的、功能和原先activex組件相同的類庫了。

  既然在visual c#中不能直接使用activex組件,那種看似在visual c#中使用的activex組件其實使用的是經(jīng)過了互操作后轉(zhuǎn)換的類庫。那么visual c#是否能夠生成active x組件?本文就來探討一下visual c#中生成activex組件的實現(xiàn)方法。制作的方法就是首先通過visual c#創(chuàng)建一個windows組件,然后把其接口以com形式發(fā)布即可。

  二.本文中介紹的程序設(shè)計及運行環(huán)境

  (1).微軟視窗2000 服務器版。
 
  (2).visual studio .net 2003企業(yè)結(jié)構(gòu)版,.net framework sdk 4322。

  三.使用visual c#創(chuàng)建windows組件

  以下是使用visual c#創(chuàng)建一個windows組件的實現(xiàn)步驟:

  1.啟動visual studio .net。

  2.選擇菜單【文件】|【新建】|【項目】后,彈出【新建項目】對話框。

  3.將【項目類型】設(shè)置為【visual c#項目】。

  4.將【模板】設(shè)置為【類庫】。

  5.在【名稱】文本框中輸入【activexdotnet】。

  6.在【位置】的文本框中輸入【c:/class】,然后單擊【確定】按鈕,則visual c#則在"c:/class"目錄中創(chuàng)建"activexdotnet"文件夾,里面存放的是activexdotnet項目文件,具體如圖01所示:


圖01:創(chuàng)建類庫的【新建項目】對話框

  7.選擇【解決方案資源管理器】窗口,并從中上傳class1.cs文件,因為此文件在本程序中已經(jīng)沒有用途了。

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


圖02:在項目中【添加新項】對話框
  9. 把visual studio .net的當前窗口切換到【mycontrol.cs(設(shè)計)】窗口,并從【工具箱】中的【windows窗體組件】選項卡中往設(shè)計窗體中按順序拖入下列組件:

  一個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創(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】項目創(chuàng)建的active x組件的界面就基本完成了,具體如圖03所示:


圖03:【activexdotnet】項目創(chuàng)建的active x組件的設(shè)計界面

  11. 在mycontrol.cs中的【mycontrol 的摘要說明】代碼區(qū)中添加下列代碼,以下代碼是定義一個公用的接口,此接口是告訴com/com+,這兒有一個公用的屬性可以進行讀寫操作:

public interface axmycontrol
{
 string usertext { set ; get ; }
}


  12. 在mycontrol.cs的【mycontrol】class代碼區(qū)中添加下列代碼,以下代碼是首先定義一個私有的字符串,用此字符串來保存從web測試頁面中傳遞來的數(shù)值定義一個公用屬性,在接下來的web測試頁面中,將通過這個屬性來傳遞數(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)建了一個名稱為mycontrol的class,這也就是用visual c#封裝的、酷似active x組件的組件。

  14. 單擊快捷鍵【ctrl+f5】,則visual c#會自動完成編譯,并在"c:/class/activexdotnet/bin/debug"目錄生成一個名稱為"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è)計器變量。
  /// </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è)計器所必需的。
   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è)計器生成的代碼
  /// <summary>
  /// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器
  /// 修改此方法的內(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組件:

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

  1. 創(chuàng)建一個名稱為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"文件全部都拷貝到機器的虛擬目錄下,一般來說虛擬目錄是"c:/inetpub/wwwroot"。

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


圖04:測試用visual c#產(chǎn)生的active x組件的運行界面

  至此visual c#產(chǎn)生的active x組件和測試這個組件的全部工作就完成了。

  五.總結(jié):

  雖然本文介紹的方法的確能夠方便的解決web頁面中很多棘手的問題,本文介紹用visual c#產(chǎn)生的組件的在實用性上的確非常的類似active x組件,但從本質(zhì)上說,本文產(chǎn)生的組件并不是真正意義上的active x組件。如要使用本文所創(chuàng)建的組件,必須在web頁面所在機器上安裝.net框架,客戶端訪問web頁面時,也不會真正下載本文介紹的組件,從而也不需要設(shè)定計算機的安全級別就能夠訪問使用此組件的web頁面。可見本文產(chǎn)生的組件其實質(zhì)也是一個托管的代碼文件。它只是巧妙的用定義接口的方式來告訴com/com+對象,本組件有一個可供訪問的公用屬性,通過對此屬性的讀寫操作,完成類似active x組件的工作。菜鳥學堂:
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 南通市| 伊金霍洛旗| 民权县| 五指山市| 博客| 四子王旗| 临潭县| 临湘市| 普安县| 大田县| 义马市| 修武县| 建阳市| 通河县| 琼中| 申扎县| 永新县| 克东县| 中山市| 馆陶县| 洪雅县| 承德县| 简阳市| 滦南县| 海城市| 南宁市| 海南省| 临武县| 资阳市| 彭州市| 云南省| 海丰县| 岑巩县| 岱山县| 清河县| 娄底市| 嘉义市| 阳原县| 西华县| 将乐县| 惠州市|