最震撼的战鼓音乐:VS2008创建ActiveX控件

来源:百度文库 编辑:中财网 时间:2024/04/29 11:58:15
VS2008创建ActiveX控件2010年03月15日 星期一 16:12 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代码的中嵌入

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