使用C#構建帶事件的簽名ActiveX組件具體辦法:
第一步 創建ActiveX項目
使用
創建ActiveX項目
第二步 編寫ActiveX主體
ActiveX的主體包括方法定義接口
首先
///<summary>
/// 該接口定義了ActiveX的方法
///</summary>
[
Guid(
InterfaceType(ComInterfaceType
ComVisible(true)]
publicinterfaceIBosnMaActiveX
{
[DispId(
void Start();
[DispId(
void Stop();
}
該接口內的成員會暴露給外部調用
接下來定義事件接口:
///<summary>
/// 該接口定義了ActiveX的事件
///</summary>
[ComVisible(true)]
[Guid(
[InterfaceType(ComInterfaceType
publicinterfaceBosnMaActiveXEvents
{
[DispId(
void OnRecorderStarted();
[DispId(
void OnRecorderStopped();
[DispId(
void OnRecorderVolumeChanged(int value);
}
這裡我們為ActiveX定義三個事件
最後我們編寫集成方法接口和事件接口的ActiveX主體類:
[
Guid(
ProgId(
ClassInterface(ClassInterfaceType
ComDefaultInterface(typeof(IBosnMaActiveX))
ComSourceInterfaces(typeof(BosnMaActiveXEvents))
ComVisible(true)
]
publicclassBosnAcX : IBosnMaActiveX
{
#region Events
publicdelegatevoidVolumeChangedHandler(int value);
publicdelegatevoidSimpleHandler();
publiceventVolumeChangedHandler OnRecorderVolumeChanged;
publiceventSimpleHandler OnRecorderStarted;
publiceventSimpleHandler OnRecorderStopped;
#endregion
#region Implementation of IBosnMaActiveX
///<summary>
/// 調用該方法將引發OnRecorderStarted事件
///</summary>
publicvoid Start()
{
OnRecorderStarted();
SimpleHandler d = Work;
d
}
publicvoid Work()
{
Thread
OnRecorderVolumeChanged(
}
///<summary>
/// 調用該方法將引發OnRecorderStopped事件
///</summary>
publicvoid Stop()
{
OnRecorderStopped();
}
#endregion
}
這裡要注意主體類的事件名稱要與事件接口(在上例中為BosnMaActiveXEvents)中的方法名相同
編譯並注冊ActiveX
編譯整個項目將輸出dll
圖
然後我們啟動命令行CMD(如果是Vista/Win
C:\>D: //轉到
D:\>regasm activexofbosnma
*regasm命令在%systemroot%\Microsoft
*使用regasm activexofbosnma
圖
測試ActiveX
最後我們創建一個html頁面來測試該ActiveX
<html>
<headrunat=
<title></title>
<objectid=
</object>
<scriptlanguage=
MyDiv
</script>
<scriptlanguage=
MyDiv
</script>
<scriptlanguage=
MyDiv
</script>
</head>
<body>
<form>
<scriptlanguage=
function Button
myAcX
}
function Button
myAcX
}
function RecorderVolumeChanged(v) {
alert(
}
</script>
<divid=
<p>
<inputid=
<inputid=
</form>
</body>
</html>
測試效果
首先使用IE打開測試頁面
允許ActiveX交互後進入主界面
三秒過後收到Volume事件
最後點擊Stop按鈕會收到OnRecorderStopped事件
安全性
為了標記ActiveX控件為安全的(避免彈出
using System;
using System
using System
using System
using System
namespace ActiveXOfBosnMa
{
[
Serializable
ComVisible(true)
]
publicenumObjectSafetyOptions
{
INTERFACESAFE_FOR_UNTRUSTED_CALLER =
INTERFACESAFE_FOR_UNTRUSTED_DATA =
INTERFACE_USES_DISPEX =
INTERFACE_USES_SECURITY_MANAGER =
};
//
// MS IObjectSafety Interface definition
//
[
ComImport()
Guid(
InterfaceType(ComInterfaceType
]
publicinterfaceIObjectSafety
{
[PreserveSig]
long GetInterfaceSafetyOptions(refGuid iid
[PreserveSig]
long SetInterfaceSafetyOptions(refGuid iid
};
//
// Provides a default Implementation for
// safe scripting
// This basically means IE won
// ActiveX object not being safe
//
publicclassIObjectSafetyImpl : IObjectSafety
{
privateObjectSafetyOptions m_options =
ObjectSafetyOptions
ObjectSafetyOptions
#region [IObjectSafety implementation]
publiclong GetInterfaceSafetyOptions(refGuid iid
{
pdwSupportedOptions = (int)m_options;
pdwEnabledOptions = (int)m_options;
return
}
publiclong SetInterfaceSafetyOptions(refGuid iid
{
return
}
#endregion
};
}
並實現以下兩個方法:
#region Implementation of IObjectSafety
privateObjectSafetyOptions m_options =
ObjectSafetyOptions
ObjectSafetyOptions
publiclong GetInterfaceSafetyOptions(refGuid iid
{
pdwSupportedOptions = (int)m_options;
pdwEnabledOptions = (int)m_options;
return
}
publiclong SetInterfaceSafetyOptions(refGuid iid
{
return
}
#endregion
From:http://tw.wingwit.com/Article/program/net/201311/13191.html