HOW TO:引發由 COM 接收所處理的事件
更新:2007 年 11 月
如果您不熟悉 .NET Framework 所提供的委派架構事件模型,請參閱處理和引發事件。如需適用於這個主題的特定詳細資訊,請參閱同一節中的引發事件。
.NET Framework 提供了委派架構事件系統,來將事件傳送者 (來源) 連接到事件接收者 (接收)。如果接收是 COM 用戶端,那麼來源必須包括額外的項目以模擬連接點。經過這些修改,COM 用戶端可以呼叫 IConnectionPoint::Advise 方法,以傳統方式註冊其事件接收 (Event Sink) 介面 (Visual Basic 會隱藏連接點詳細資料,所以您不需要直接呼叫這些方法)。
若要與 COM 事件互通
定義 Managed 程式碼中的事件接收介面。這個介面可能含有來自 Managed 類別的事件子集。介面方法名稱必須和事件名稱相同。
套用 ComSourceInterfacesAttribute,將事件接收 (Event Sink) 介面連接到 Managed 類別。
將含有該類別之組件匯出至型別程式庫。使用型別程式庫匯出工具 (Tlbexp.exe) 或相等的 API 匯出組件。
在 COM 中實作事件接收介面。
對於接收事件的 COM 用戶端,請實作事件來源在其型別程式庫中所定義的事件接收介面。然後使用連接點機制,將接收介面連接至事件的來源。
範例
下列範例會示範做為事件來源的 Managed 伺服器,以及做為事件接收 (Event Sink) 的 COM 用戶端。Managed 伺服器宣告了 ButtonEvents 做為事件接收介面,並且將這個介面連接到 Button 類別。Unmanaged 用戶端會建立一個 Button 類別的執行個體,並且實作這個事件接收介面。
' Managed server (event source)
Option Explicit
Option Strict
Imports System
Imports System.Runtime.InteropServices
Namespace EventSource
Public Delegate Sub ClickDelegate(x As Integer, y As Integer)
Public Delegate Sub ResizeDelegate()
Public Delegate Sub PulseDelegate()
' Step 1: Defines an event sink interface (ButtonEvents) to be
' implemented by the COM sink.
<GuidAttribute("1A585C4D-3371-48dc-AF8A-AFFECC1B0967"), _
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)> _
Public Interface ButtonEvents
Sub Click(x As Integer, y As Integer)
Sub Resize()
Sub Pulse()
End Interface
' Step 2: Connects the event sink interface to a class
' by passing the namespace and event sink interface
' ("EventSource.ButtonEvents, EventSrc").
<ComSourceInterfaces(GetType(ButtonEvents))> _
Public Class Button
Public Event Click As ClickDelegate
Public Event Resize As ResizeDelegate
Public Event Pulse As PulseDelegate
Public Sub CauseClickEvent(x As Integer, y As Integer)
RaiseEvent Click(x, y)
End Sub
Public Sub CauseResizeEvent()
RaiseEvent Resize()
End Sub
Public Sub CausePulse()
RaiseEvent Pulse()
End Sub
End Class
End Namespace
using System;
using System.Runtime.InteropServices;
namespace EventSource
{
public delegate void ClickDelegate(int x, int y);
public delegate void ResizeDelegate();
public delegate void PulseDelegate();
// Step 1: Defines an event sink interface (ButtonEvents) to be
// implemented by the COM sink.
[GuidAttribute("1A585C4D-3371-48dc-AF8A-AFFECC1B0967") ]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIDispatch)]
public interface ButtonEvents
{
void Click(int x, int y);
void Resize();
void Pulse();
}
// Step 2: Connects the event sink interface to a class
// by passing the namespace and event sink interface
// ("EventSource.ButtonEvents, EventSrc").
[ComSourceInterfaces(typeof(ButtonEvents))]
public class Button
{
public event ClickDelegate Click;
public event ResizeDelegate Resize;
public event PulseDelegate Pulse;
public Button()
{
}
public void CauseClickEvent(int x, int y)
{
Click(x, y);
}
public void CauseResizeEvent()
{
Resize();
}
public void CausePulse()
{
Pulse();
}
}
}
' COM client (event sink)
' This Visual Basic 6.0 client creates an instance of the Button class and
' implements the event sink interface. The WithEvents directive
' registers the sink interface pointer with the source.
Public WithEvents myButton As Button
Private Sub Class_Initialize()
Dim o As Object
Set o = New Button
Set myButton = o
End Sub
' Events and methods are matched by name and signature.
Private Sub myButton_Click(ByVal x As Long, ByVal y As Long)
MsgBox "Click event"
End Sub
Private Sub myButton_Resize()
MsgBox "Resize event"
End Sub
Private Sub myButton_Pulse()
End Sub