用C#开发ActiveX控件,并使用web调用

入职差不多两个月了,由学生慢慢向职场人做转变,也慢慢的积累知识,不断的更新自己。最近的一个项目里边,涉及到的一些问题,因为SDK提供的只是 winform才能使用了,但是有需求咱们必须得完成啊,所以涉及到的ActiveX控件开发并用web来显示的,正好也总结一些,之前在学校一直没有接 触过,网上是有教程的,但是大多有问题,只有自己亲自测试通过了才放心。

一、开发ActiveX控件

1、新建类库,命名类库名称“user.cs”;

2、在类库中添加自定义用户控件“ UserControl1”,实现各种自定义功能;

3、为了解决浏览器安全设置对控件的影响,必须在组件中加入IObjectSafety接口,所以再添加一个接口类“IObjectSafety.cs”

  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Text; 
  4. using System.Web.UI.WebControls.WebParts; //必须引用该包 
  5. using System.Security; 
  6. using System.Runtime.InteropServices;     //必须引用该包 
  7.  
  8.  
  9. namespace user 
  10.     [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] //GUID这个不需修改,固定的 
  11.  
  12.    
  13.     public interface IObjectSafety 
  14.     { 
  15.  
  16.         // 方法定义 
  17.  
  18.         void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions); 
  19.  
  20.         void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions); 
  21.  
  22.     } 
  23.  

4、继承接口

  1. public partial class UserControl1: UserControl,IObjectSafety 
  2.     { 
  3.         public UserControl1() 
  4.         { 
  5.             InitializeComponent(); 
  6.         }         
  7. public void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions) 
  8.         { 
  9.             pdwSupportedOptions = 1;  //不要修改该代码 
  10.             pdwEnabledOptions = 2;    //不要修改该代码 
  11.             return; 
  12.         } 
  13.  
  14.         public void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions) 
  15.         { 
  16.             return; 
  17.         } 
  18.         public void YourFunc(){} 

5、在UserControl1引入两个命名空间

using System.Security;

using System.Runtime.InteropServices;

6、工具——创建GUID——新建GUID——选择第五项——复制,就可以关闭小窗口,然后在命名空间下粘贴,如下

  1. namespace user 
  2.    [Guid("7F29ACED-AD84-4EEE-9E1A-58BE255F9EF7")] //这个GUID是web引用的时候用到的 
  3.    
  4.     public partial class UserControl1: UserControl,IObjectSafety 
  5.     { 
  6.         …… 

用C#开发ActiveX控件,并使用web调用

7、最后一步,项目——user属性(最后一项),两处需要修改

①应用程序——程序集信息——√ 使程序集COM可见

②生成——√ 为COM互操作注册

8、即可右键项目user——生成

二、web使用ActiveX控件

在web调用很简洁,引用刚刚生成的dll文件,然后在添加

  1. <body> 
  2.     <form id="form1" runat="server"> 
  3.     <div > 
  4.      <object id="VisioDisPlay" 
  5.         classid="clsid:7F29ACED-AD84-4EEE-9E1A-58BE255F9EF7"  
  6.         width="1250" 
  7.         height="600"    
  8.          > 
  9.         </object> 
  10.     </div> 
  11.     </form> 

即可完成。注意的是:这里的classid里边的字符串里边有 “ clsid: ”,别忘啦,后面接的是第六步生成的GUID,必须一致!

posted @ 2016-06-03 18:04  C#winform软件设计  阅读(31)  评论(0编辑  收藏  举报