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>

转载于:https://www.cnblogs.com/CPFlying/archive/2010/03/14/1685740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值