码迷,mamicode.com
首页 > Windows程序 > 详细

vs2010 下使用C#开发activeX控件

时间:2016-06-05 00:55:41      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:

1.创建一个类库

2.项目属性-应用程序-程序集信息-"使程序集COM可见"勾上;

3.项目属性-生成-"为COM互操作注册"勾上.(这个折腾一天,否则注册事件不可用);

4.创建接口: IObjectSafety (注意GUID不能变);

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace my6
{
[Guid("E3A49460-230D-4727-9EFD-A1E9303D4EE2")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IObjectSafety
{
[PreserveSig]
int GetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] ref int pdwSupportedOptions, [MarshalAs(UnmanagedType.U4)] ref int pdwEnabledOptions);

[PreserveSig()]
int SetInterfaceSafetyOptions(ref Guid riid, [MarshalAs(UnmanagedType.U4)] int dwOptionSetMask, [MarshalAs(UnmanagedType.U4)] int dwEnabledOptions);
}
}

5.创建类库,代码如下

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 my6
{
//定义事件委托
public delegate void ShowInputEvent(string str);
[Guid("BB9DF6C9-0C30-41C8-A568-91484C67B03C")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface ControlEvents
{
[DispIdAttribute(0x001)]
void OnShowInputEvent(string str);
}
[Guid("ABB26C85-5521-4D68-A70A-9D7050980A1C")]
[ComVisible(true)]
[ComSourceInterfacesAttribute(typeof(ControlEvents))]
public partial class UserControl1 : UserControl, IObjectSafety
{
#region
public event ShowInputEvent OnShowInputEvent;
public void ShowRandomNum()
{
Random r = new Random();
int num = r.Next(0, 100);
label1.Text = "随机数:" + num.ToString();
}
public void ShowInput(string str)
{
label1.Text = str;
OnShowInputEvent(str);
}

#endregion
public UserControl1()
{
InitializeComponent();
label1.Text = "";
label2.Text = "";
}
#region IObjectSafety 成员

private const string _IID_IDispatch = "{00020400-0000-0000-C000-000000000046}";
private const string _IID_IDispatchEx = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";
private const string _IID_IPersistStorage = "{0000010A-0000-0000-C000-000000000046}";
private const string _IID_IPersistStream = "{00000109-0000-0000-C000-000000000046}";
private const string _IID_IPersistPropertyBag = "{37D84F60-42CB-11CE-8135-00AA004BB851}";

private const int INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001;
private const int INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002;
private const int S_OK = 0;
private const int E_FAIL = unchecked((int)0x80004005);
private const int E_NOINTERFACE = unchecked((int)0x80004002);

private bool _fSafeForScripting = true;
private bool _fSafeForInitializing = true;

public int GetInterfaceSafetyOptions(ref Guid riid, ref int pdwSupportedOptions, ref int pdwEnabledOptions)
{
int Rslt = E_FAIL;

string strGUID = riid.ToString("B");
pdwSupportedOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER | INTERFACESAFE_FOR_UNTRUSTED_DATA;
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
Rslt = S_OK;
pdwEnabledOptions = 0;
if (_fSafeForScripting == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_CALLER;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
Rslt = S_OK;
pdwEnabledOptions = 0;
if (_fSafeForInitializing == true)
pdwEnabledOptions = INTERFACESAFE_FOR_UNTRUSTED_DATA;
break;
default:
Rslt = E_NOINTERFACE;
break;
}

return Rslt;
}

public int SetInterfaceSafetyOptions(ref Guid riid, int dwOptionSetMask, int dwEnabledOptions)
{
int Rslt = E_FAIL;
string strGUID = riid.ToString("B");
switch (strGUID)
{
case _IID_IDispatch:
case _IID_IDispatchEx:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_CALLER) && (_fSafeForScripting == true))
Rslt = S_OK;
break;
case _IID_IPersistStorage:
case _IID_IPersistStream:
case _IID_IPersistPropertyBag:
if (((dwEnabledOptions & dwOptionSetMask) == INTERFACESAFE_FOR_UNTRUSTED_DATA) && (_fSafeForInitializing == true))
Rslt = S_OK;
break;
default:
Rslt = E_NOINTERFACE;
break;
}

return Rslt;
}

#endregion

}

}

6.网页调用事件方法

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
</head>
<body>
<object id="activeX" classid="clsid:ABB26C85-5521-4D68-A70A-9D7050980A1C"></object>
<br />
<input type="text" id="userval" />
<input type="button" value="调用插件的方法" onclick="callMethod()" />
<input type="button" value="调用插件的事件" onclick="callEvent()" />
<!--事件的注册-->
<script language="JavaScript" for="activeX" event="OnShowInputEvent(value)">alert(value);</script>
<!--调用插件的方法-->
<script type="text/javascript">
var callMethod = function () {
activeX.ShowRandomNum();
},
callEvent = function () {
var str = document.getElementById("userval").value;
activeX.ShowInput(str);
}
</script>
</body>
</html>

vs2010 下使用C#开发activeX控件

标签:

原文地址:http://www.cnblogs.com/xiaohuihui123/p/5559832.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!