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

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

一、开发ActiveX控件

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

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

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

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts; //必须引用该包
using System.Security;
using System.Runtime.InteropServices;     //必须引用该包


namespace user
{
    [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] //GUID这个不需修改,固定的

  
    public interface IObjectSafety
    {

        // 方法定义

        void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions);

        void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions);

    }

}

4、继承接口

    public partial class UserControl1: UserControl,IObjectSafety
    {
        public UserControl1()
        {
            InitializeComponent();
        }        
public void GetInterfacceSafyOptions(System.Int32 riid, out System.Int32 pdwSupportedOptions, out System.Int32 pdwEnabledOptions)
        {
            pdwSupportedOptions = 1;  //不要修改该代码
            pdwEnabledOptions = 2;    //不要修改该代码
            return;
        }

        public void SetInterfaceSafetyOptions(System.Int32 riid, System.Int32 dwOptionsSetMask, System.Int32 dwEnabledOptions)
        {
            return;
        }
        public void YourFunc(){}
}

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

  using System.Security;

  using System.Runtime.InteropServices;  

并在AssemblyInfo.cs文件中添加代码  

[assembly: AllowPartiallyTrustedCallers()]

 

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

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

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

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

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

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

二、web使用ActiveX控件

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

<body>
    <form id="form1" runat="server">
    <div >
     <object id="VisioDisPlay"
        classid="clsid:7F29ACED-AD84-4EEE-9E1A-58BE255F9EF7" 
        width="1250"
        height="600"   
         >
        </object>
    </div>
    </form>

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


好了,该吃午饭了,有什么不足的,忘园子大咖们批评指出。

 

数字签名 时间戳
http://timestamp.verisign.com/scripts/timestamp.dll

posted @ 2015-05-12 12:03  山治先生  阅读(6730)  评论(12编辑  收藏  举报