开发基于.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项目文件,具体如图01所示:
![]() 图01:创建类库的【新建项目】对话框 |
![]() 图02:在项目中【添加新项】对话框 |
<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> |
![]() 图04:测试用Visual C#产生的Active X组件的运行界面 |
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 ) ; } |
![]() 图03:【ActiveXDotNet】项目创建的Active X组件的设计界面 |
public interface AxMyControl { String UserText { set ; get ; } } |
private String mStr_UserText ; public String UserText { get { return mStr_UserText ; } set { mStr_UserText = value ; //修改组件的数值 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> /// 必需的设计器变量。 /// </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 } } |
新闻热点
疑难解答
图片精选