文档库 最新最全的文档下载
当前位置:文档库 › NET COM

NET COM

将.net组件注册为com组件

切换到MS-DOS命令行下面,运行:
C:\>regasm ClassLibrary1.DLL /regfile:ClassLibrary1.reg

上面的命令行是注册我们的.NET组件,并且产生了一个备用的注册表文件。大家一定会记得在以前我们的win9x/NT/2000下面注册COM组件使用的命令是:
regsvr32 c:\test.dll

在.NET下面,注册.NET组件就需要上面的regasm命令了,值得注意的是,这个方法仅仅是为了可供COM组件来调用,.NET本身之间相互调用组件是不需要任何注册的!









-0------------------------------------------------------------------------------------------------------------------------

Visual C#创建和使用ActiveX组件






开发基于.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:创建类库的【新建项目】对话框
7.选择【解决方案资源管理器】窗口,并从中上传Class1.cs文件,因为此文件在本程序中已经没有用途了.
8.选择【项目】|【添加组件】后,弹出【添加新项】对话框,在此对话框中设定【模板】为"组件类",设定【名称】值为"MyControl.cs"后,单击【打开】按钮.则在项目文件中新增一个名称"MyControl.cs"的文件?咛迦缤?2所示:
图02:在项目中【添加新项】对话框
9. 把Visual Studio .Net的当前窗口切换到【MyControl.cs(设计)】窗口,并从【工具箱】中的【Windows窗体组件】选项卡中往设计窗体中按顺序拖入下列组件:
一个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 ( ) ;

https://www.wendangku.net/doc/707322185.html,bel1 = new https://www.wendangku.net/doc/707322185.html,bel ( ) ;

this.groupBox1.SuspendLayout ( ) ;

this.SuspendLayout ( ) ;

this.groupBox1.Controls.Add (
this.txtUserText) ;

this.groupBox1.Controls.Add (
https://www.wendangku.net/doc/707322185.html,bel1) ;

this.groupBox1.Location = new System.Drawing.Po
int (8 , 8) ;

https://www.wendangku.net/doc/707322185.html, = "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.Po
int (84 , 20) ;

https://www.wendangku.net/doc/707322185.html, = "txtUserText" ;

this.txtUserText.Size = new System.Drawing.Size (180 , 21) ;

this.txtUserText.TabIndex = 1 ;

this.txtUserText.Text = "" ;

https://www.wendangku.net/doc/707322185.html,bel1.Location = new System.Drawing.Po
int (8 , 24) ;

https://www.wendangku.net/doc/707322185.html, = "label1" ;

https://www.wendangku.net/doc/707322185.html,bel1.Size = new System.Drawing.Size (66 , 16) ;

https://www.wendangku.net/doc/707322185.html,bel1.TabIndex = 0 ;

https://www.wendangku.net/doc/707322185.html,bel1.Text = "输入信息:" ;

this.Controls.Add (
this.groupBox1) ;

https://www.wendangku.net/doc/707322185.html, = "MyControl" ;

this.Size = new System.Drawing.Size (288 , 72) ;

this.groupBox1.ResumeLayout (false) ;

this.ResumeLayout (false) ;
}
至此【ActiveXDotNet】项目创建的Active X组件的界面就基本完成了,具体如图03所示:
图03:【ActiveXDotNet】项目创建的Active X组件的设计界面
11. 在MyControl.cs中的【MyControl 的摘要说明】代码区中添加下列代码,以下代码是定义一个公用的接口,此接口是告诉COM/COM+,这儿有一个公用的属性可以进行读写操作:
public
interface AxMyControl
{
String UserText
{
set ;
get ;
}
}
12. 在MyControl.cs的【MyControl】class代码区中添加下列代码,以下代码是首先定义一个私有的字符串,用此字符串来保存从Web测试页面中传递来的数值定义一个公用属性,在接下来的Web测试页面中,将通过这个属性来传递数值,此属性是可读写:
private String mStr_UserText ;

public String UserText
{
get
{
return mStr_UserText ;
}
set
{
mStr_UserText = value ;
//修改组件的数值
txtUserText.Text = value ;
}
}
13. 保存上面的修改步骤,至此我们就利用Visual C#创建了一个名称为MyControl的class,这也就是用Visual C#封装的、酷似Active X组件的组件.
14. 单击快捷键【Ctrl+F5】,则Visual C#会自动完成编译,并在"C:ClassActiveXDotNetinDebug"目录生成一个名称为"ActiveXDotNet.dll"文件,这就是产生的组件.
以下是经过上述步骤产生的MyControl.cs的全部代码:
using System ;
using System.Collections ;
using https://www.wendangku.net/doc/707322185.html,ponentModel ;
using System.Drawing ;
using System.Data ;
using System.Windows.Forms ;
namespace ActiveXDotNet
{
public
interface AxMyControl
{
String UserText
{
set ;
get ;
}
}

///


/// MyControl 的摘要说明.
///

public class MyControl : https://www.wendangku.net/doc/707322185.html,erControl , AxMyControl
{
///
/// 必需的设计器变量.
///

private https://www.wendangku.net/doc/707322185.html,ponentModel.Container components = null ;


private System.Windows.Forms.GroupBox groupBox1 ;

private https://www.wendangku.net/doc/707322185.html,bel 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 调用后添加任何初始化
}

///


/// 清理所有正在使用的资源.
///

protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose ( ) ;
}
}
base.Dispose (disposing) ;
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要使用代码编辑器
/// 修改此方法的内容.
///

private void
InitializeComponent ( )
{
this.groupBox1 = new System.Windows.Forms.GroupBox ( ) ;

this.txtUserText = new System.Windows.Forms.TextBox ( ) ;

https://www.wendangku.net/doc/707322185.html,bel1 = new https://www.wendangku.net/doc/707322185.html,bel ( ) ;

this.groupBox1.SuspendLayout ( ) ;

this.SuspendLayout ( ) ;

this.groupBox1.Controls.Add (
this.txtUserText) ;

this.groupBox1.Controls.Add (
https://www.wendangku.net/doc/707322185.html,bel1) ;

this.groupBox1.Location = new System.Drawing.Po
int (8 , 8) ;

https://www.wendangku.net/doc/707322185.html, = "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.Po
int (84 , 20) ;

https://www.wendangku.net/doc/707322185.html, = "txtUserText" ;

this.txtUserText.Size = new System.Drawing.Size (180 , 21) ;

this.txtUserText.TabIndex = 1 ;

this.txtUserText.Text = "" ;

https://www.wendangku.net/doc/707322185.html,bel1.Location = new System.Dr

awing.Po
int (8 , 24) ;

https://www.wendangku.net/doc/707322185.html, = "label1" ;

https://www.wendangku.net/doc/707322185.html,bel1.Size = new System.Drawing.Size (66 , 16) ;

https://www.wendangku.net/doc/707322185.html,bel1.TabIndex = 0 ;

https://www.wendangku.net/doc/707322185.html,bel1.Text = "输入信息:" ;

this.Controls.Add (
this.groupBox1) ;

https://www.wendangku.net/doc/707322185.html, = "MyControl" ;

this.Size = new System.Drawing.Size (288 , 72) ;

this.groupBox1.ResumeLayout (false) ;

this.ResumeLayout (false) ;
}
#endregion
}
}
四.Visual C#中使用刚封装的Active X组件:
以下步骤就是通过Web页面的方式来测试上面创建组件:
1. 创建一个名称为test.htm文件,MyControl就是放在此Web页面中加以测试的,此文件的内容如下:











2. 把产生的"test.htm"和"ActiveXDotNet.dll"文件全部都拷贝到机器的虚拟目录下,一般来说虚拟目录是"C:Inetpubwwwroot".
3. 打开浏览器,在浏览器的地址栏中输入"http://localhost/test.htm"后,单击"转到"按钮,则会得到如下的运行界面:
图04:测试用Visual C#产生的Active X组件的运行界面
至此Visual C#产生的Active X组件和测试这个组件的全部工作就完成了.
五.总结:
虽然本文介绍的方法的确能够方便的解决Web页面中很多棘手的问题,本文介绍用Visual C#产生的组件的在实用性上的确非常的类似Active X组件,但从本质上说,本文产生的组件并不是真正意义上的Active X组件.如要使用本文所创建的组件,必须在Web页面所在机器上安装.Net框架,客户端访问Web页面时,也不会真正下载本文介绍的组件,从而也不需要设定计算机的安全级别就能够访问使用此组件的Web页面.可见本文产生的组件其实质也是一个托管的代码文件.它只是巧妙的用定义接口的方式来告诉COM/COM+对象,本组件有一个可供访问的公用属性,通过对此属性的读写操作,完成类似Active X组件的工作.








在.net中操作Word


阅读正文 字体大小:加大 减少 双击自动滚屏 普通模式





要操作Word,我们就需要Word的对象库文件“MSWORD.OLB”( word 2000为MSWORD9.OLB ),通常安装了Office Word后,你就可以在office安装目录的Office10文件夹

下面找到这个文件,当我们将这个文件引入到项目后,我们就可以在源码中使用各种操作函数来操作Word?咛遄龇ㄊ谴蚩说ダ钢械南钅?添加引用>浏览,在打开的“选择组件”对话框中找到MSWORD.OLB后按确定即可引入此对象库文件,https://www.wendangku.net/doc/707322185.html,将会自动将 库文件转化为DLL组件,这样我们只要在源码中创建该组件对象即可达到操作Word的目的!
在CS代码文件中对命名空间的应用,如:using Word;
范例如下:
using System;
using System.Drawing;
using System.Collections;
using https://www.wendangku.net/doc/707322185.html,ponentModel;
using System.Windows.Forms;
using Word;
namespace ExamSecure
{
///
/// ItemToDoc 的摘要说明.
///
public class ItemToDoc : System.Windows.Forms.Form
{
object strFileName;
Object Nothing;
Word.ApplicationClass myWordApp=new Word.ApplicationClass( );
Word.Document myWordDoc;

string strContent="";

private https://www.wendangku.net/doc/707322185.html,ponentModel.Container components = null;

public ItemToDoc( )
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent( );
//
// TODO: 在
InitializeComponent 调用后添加任何构造函数代码
//
}
[STAThread]
static void Main( )
{
System.Windows.Forms.Application.Run( new ItemToDoc( ) );
}

///
/// 清理所有正在使用的资源.
///
protected override void Dispose(bool disposing)
{
if(disposing)
{
if( components != null )
{
components.Dispose( );
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容.
///
private void
InitializeComponent( )
{
//
// ItemToDoc
//
this.AutoScaleBaseSize = new System.Drawing.Size( 6, 14 );

this.ClientSize = new System.Drawing.Size( 292, 273 );

https://www.wendangku.net/doc/707322185.html, = "ItemToDoc";

this.Text = "ItemToDoc";

this.Load += new System.EventHandler( this.ItemToDoc_Load );
}
#endregion
private void ItemToDoc_Load( object sender, System.EventArgs e )
{
WriteFile( );
}

private void WriteFile( )
{
strFileName=System.Windows.Forms.Application.StartupPath+"\试题库【"+GetRandomString( )+"】.doc";
Object Nothing=System.Reflection.Missing.Value;
myWordDoc=myWordApp.Docu

ments.Add( ref Nothing,ref Nothing,ref Nothing,ref Nothing );
#region 将数据库中读取得数据写入到word文件中
strContent="试题库

";
WriteFile( strContent );
strContent="试题库";
WriteFile( strContent );

#endregion
//将WordDoc文档对象的内容保存为DOC文档
myWordDoc.SaveAs( ref strFileName,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing );
//关闭WordDoc文档对象
myWordDoc.Close( ref Nothing, ref Nothing, ref Nothing );
//关闭WordApp组件对象
myWordApp.Quit( ref Nothing, ref Nothing, ref Nothing );
}

///
/// 获取一个随即字符串
///
///
private
string GetRandomString( )
{
DateTime iNow=DateTime.Now;

string strDate=iNow.ToString( "yyyyMMddHHmmffff" );
Random ran=new Random( );

int iRan=Convert.ToInt32( 10000*ran.NextDouble( ) );

string strRan=iRan.ToString( );
//位数不足则补0
int iRanlen=strRan.Length;
for( int i=0;
i<4-iRanlen;
i++ )
{
strRan="0"+strRan;
}
return strDate+strRan;
}

///
/// 将字符串写入到Word文件中
///
/// 要写入的字符串
private void WriteFile( string str )
{
https://www.wendangku.net/doc/707322185.html,st.Range.Text=str;
}
}
}





相关文档
相关文档 最新文档