{VS2010C#}{WinForm}{ActiveX}VS2010C#开发基于WinForm的ActiveX控件

在VS2010中使用C#开发基于WinForm的ActiveX控件

常见的一些ActiveX大部分是使用VB、Delphi、C++开发,使用C#开发ActiveX要解决下面三个问题:
html

  • 使.NET组件能够被COM调用
  • 在客户机上注册后,ActiveX控件能经过IE的安全认证
  • 已在客户机上注册时,安装包能经过IE的签名认证

配置:win 7,VS2010
安全

步骤第一步,建立控件

1.建立WinForm控件,命名为WebForm,如图1:服务器

 

2.设置项目的Assembly属性,如图2所示,并对Make Assembly Com-Visible选项划钩测试

而且设置项目的编译选项,如图3所示,对Register for COM Interop选中对COM组件进行注册。(注意,此处若是实在debug状态下修改的,那在调到release状态下还须要再设置一次。)ui

 

3.打开AssemblyInfo文件添加引用 using System.Security; 而且添加 [assembly: AllowPartiallyTrustedCallers()] 语句url

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Security;   //note!

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WebForm")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WebForm")]
[assembly: AssemblyCopyright("Copyright ©  2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: AllowPartiallyTrustedCallers()]   // note!

 

4.为控件类建立一个惟一的GUID,注意这里的GUID不能和AssemblyInf中的GUID相同,生成GUID的方法以下spa

使用Microsoft Windows SDK Tools中的GUID Generator生成新的GUID,如图debug

COPY生成的GUID到记事本,再拷贝GUID的字符串到控件类,并添加引用 using System.Runtime.InteropServices;3d

using System.Runtime.InteropServices;    // note !

namespace
WebForm { [Guid("575483BC-7224-44F5-A77C-78AB7FC4E606")] // note! public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); } } }

 

第二部分,打包发布ActiveX控件

5.ActiveX控件能够使用VS 2010的安装项目进行部署,使用VS 2010建立Windows Form的安装工程就能够将ActiveXdll进行打包。在打包时注意将ActiveX控件项目做为主输出项目,并设置其Register属性为vsdrpCOM,建立打包项目以下图5所示:code

以后添加项目输出,以下图

 

6.设置项目属性,以下图,文件名就是打包后安装文件.MSI的文件名。设置包文件、压缩方式,CAB size,这三项均选择默认值便可。

最后设置安装URL,这里的安装URL是用来发布或者测试ActiveXURL地址的,咱们设置为本地url:K:/VSProject/WebForm/Setup/Debug/

 

7. 将以前的WebForm.dll打包进ActiveX控件。

将WebForm.dll放入包中,以下图,build solution以后,便会出现Setup.exe和Setup.msi两个文件,其中Setup.exe就是咱们在使用ActiveX时的codebase文件。

 

8.测试基于WinForm的ActiveX控件。新建一个htm文件,输入以下代码并保存:

其中id为创建的控件类名,classid为步骤中生成的Guid,codebase为本地的ActiveX安装源,若是作服务器能够修改成ip源等,这里暂不赘述。

<object id="UserControl1" 
        classid="clsid:575483BC-7224-44F5-A77C-78AB7FC4E606" 
        width="500"
        height="500" 
        codebase="K:/VSProject/WebForm/Setup/Debug/Setup.exe">
</object>

 

测试结果

如图,由C#建立的基于WinForm的控件能够正确的显示在IE Explorer中

 

参考资料

http://www.cnblogs.com/yungboy/archive/2011/01/10/1932433.html

http://homer.cnblogs.com/archive/2005/01/26/97822.aspx

http://www.cnblogs.com/Charles2008/archive/2010/04/11/1709844.html

http://www.cnblogs.com/homer/archive/2005/01/04/86473.html

http://www.cnblogs.com/homer/archive/2005/01/08/88780.html

http://www.cnblogs.com/homer/archive/2005/01/08/88780.html

相关文章
相关标签/搜索