• VS2008创建ActiveX控件


    编写代码前的设置

    打开AssemblyInfo.cs修改程序集信息。

    引用System.Security命名空间,

    并添加[assembly : AllowPartiallyTrustedCallers()]安全声明

    修改[assembly: ComVisible(false)]为[assembly: ComVisible(true)]使程序集Com可见。为Com Interop注册。右键demoActiveX项目属性,在“生成”选项卡里将“为Com Interop注册”打上勾即可。

    编写代码

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Drawing;

    using System.Data;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Runtime.InteropServices; // 后添加的名称空间

    namespace demoActiveX

    {

     

    //用该Guid初始化demoActiveX,该Guid即是我们要在Web页面下引用该ActiveX的CLSID

    [Guid("E5FD041B-8250-4cbc-B662-A73FC7988FB5")]

     

    public partial class demoControl : UserControl

    {

    public demoControl()

    {

        InitializeComponent();

    }

    public void Test()

    {

        MessageBox.Show("你输入的内容为:" + this.textBox1.Text);

    }

     

    // 实现IObjectSafety接口,把ActiveX控件标记为安全的

    public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)

    {

        // TODO:  添加 demoControl.GetInterfacceSafyOptions 实现

        pdwSupportedOptions = 1;

        pdwEnabledOptions = 2;

    }

     

    public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)

    {

        // TODO:  添加 demoControl.SetInterfaceSafetyOptions 实现            

    }

     

    //IObjectSafety 是一个接口,它可将其功能显露给 Internet Explorer的“设置脚本安全性”和“设置初始化安全性”安全特性

    [Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"),

    InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

    public interface IObjectSafety

    {

    // methods

    void GetInterfacceSafyOptions(

        System.Int32 riid,

        out System.Int32 pdwSupportedOptions,

        out System.Int32 pdwEnabledOptions);

    void SetInterfaceSafetyOptions(

        System.Int32 riid,

        System.Int32 dwOptionsSetMask,

        System.Int32 dwEnabledOptions);

    }

     

    private void button1_Click(object sender, EventArgs e)

    {

        textBox1.Text = "Test Only!";

    }

    }

    }

    编译,便可以得到一个dll文件。此时就可以用上面的GUID测试了

    "E5FD041B-8250-4cbc-B662-A73FC7988FB5"

    只须在html代码的<body></body>中嵌入

    <object

         classid="clsid:E5FD041B-8250-4cbc-B662-A73FC7988FB5"  Width="200" Height="100">

     </object>

  • 相关阅读:
    if __name__ == 'main': 的作用和原理
    第四篇、Python文件处理
    第二篇*2、Python字符串格式化
    第三篇、Python函数
    第二篇*1、Python基本数据类型
    ping包,支持ip录入
    layui之弹出层--从父窗口传递数据到子窗口
    动态调用WebService
    c# 类的反射实例 (GetType().Invoke().GetMethod().CreateInstance())
    ASP.Net UpdatePanel控件 局部刷新 && 弹出提示信息
  • 原文地址:https://www.cnblogs.com/CPFlying/p/1685740.html
Copyright © 2020-2023  润新知