VB6的事件机制实际上是靠接口实现的 注意看下面的“coclass Timer”中的“[default, source] interface TimerEvents” 应该有办法作事件接口 但我也没做过 [ uuid(33AD4F28-6699-11CF-B70C-00AA0060D393), helpstring("A control which can execute code at regular intervals by causing a Timer event."), helpcontext(0x000dfa33), noncreatable ] coclass Timer { [default] interface _Timer; [default, source] interface TimerEvents; }; [ odl, uuid(33AD4F29-6699-11CF-B70C-00AA0060D393), helpstring("A control which can execute code at regular intervals by causing a Timer event."), helpcontext(0x000dfa33), hidden, nonextensible ] interface _Timer : IDispatch { [propget] HRESULT _stdcall _Default([out, retval] VARIANT_BOOL* ); [propput] HRESULT _stdcall _Default([in] VARIANT_BOOL ); [propget, helpstring("Returns the name used in code to identify an object."), helpcontext(0x000dfc75)] HRESULT _stdcall Name([out, retval] BSTR* ); [propget, helpstring("Returns/sets the number identifying a control in a control array."), helpcontext(0x000dfc5c)] HRESULT _stdcall Index([out, retval] short* ); [propget, helpstring("Returns/sets a value that determines whether an object can respond to user-generated events."), helpcontext(0x000dfc40)] HRESULT _stdcall Enabled([out, retval] VARIANT_BOOL* ); [propput, helpstring("Returns/sets a value that determines whether an object can respond to user-generated events."), helpcontext(0x000dfc40)] HRESULT _stdcall Enabled([in] VARIANT_BOOL ); [propget, helpstring("Returns/sets the number of milliseconds between calls to a Timer control's Timer event."), helpcontext(0x000dfc5d)] HRESULT _stdcall Interval([out, retval] long* ); [propput, helpstring("Returns/sets the number of milliseconds between calls to a Timer control's Timer event."), helpcontext(0x000dfc5d)] HRESULT _stdcall Interval([in] long ); [propget, helpstring("Returns the object on which this object is located."), helpcontext(0x000dfc87)] HRESULT _stdcall Parent([out, retval] Form** ); [propget, helpstring("Stores any extra data needed for your program."), helpcontext(0x000dfcab)] HRESULT _stdcall Tag([out, retval] BSTR* ); [propput, helpstring("Stores any extra data needed for your program."), helpcontext(0x000dfcab)] HRESULT _stdcall Tag([in] BSTR ); [hidden, helpstring("Adds an item to a Listbox or ComboBox control or a row to a Grid control."), helpcontext(0x000dfa84)] HRESULT _stdcall AddItem( [in] BSTR , [in] VARIANT ); [hidden, helpstring("Removes an item from a ListBox or ComboBox control or a row from a Grid control."), helpcontext(0x000dfaa6)] HRESULT _stdcall RemoveItem([in] short ); [hidden, helpstring("Clears the contents of a control or the system Clipboard."), helpcontext(0x000dfa87)] HRESULT _stdcall Clear(); }; [ odl, uuid(33AD4F2A-6699-11CF-B70C-00AA0060D393), helpstring("A control which can execute code at regular intervals by causing a Timer event."), helpcontext(0x000dfa33) ] interface TimerEvents : IUnknown { [helpstring("Occurs when a preset interval for a Timer control has elapsed."), helpcontext(0x000dfb6a)] HRESULT _stdcall Timer(); };
//VB用WithEvents关键字声明一个对外接口呵呵,试试绕开WithEvents,至于怎么绕开它,我写了一个简单的例子,vb的,你自己改成vc的吧: 新建一个普通工程,添加一个窗体,3个类模块:类模块IEvents: Option Explicit'interface IEventsPublic Sub Event1() End SubPublic Sub Event2(ByVal Param As Integer) End Sub类模块IYourObject: Option Explicit 'interface IYourObjectPublic Property Get YourProp() As String End PropertyPublic Sub YourSub(ByVal i As Integer) End SubPublic Sub SetEventHandler(EH As IEvents) End Sub类模块CYourObject: 'CYourObject - implements the IYourObject interface Option Explicit Implements IYourObject Dim m_Events As IEvents Private mYourProp As StringPublic Property Get YourProp() As String YourProp = IYourObject.YourProp End Property Public Sub YourSub(ByVal i As Integer) IYourObject_YourSub i End SubPublic Sub SetEventHandler(EH As IEvents) IYourObject.SetEventHandler EH End SubPrivate Sub Class_Initialize() mYourProp = "this is test" End SubPrivate Sub IYourObject_SetEventHandler(EH As IEvents) Set m_Events = EH End SubPrivate Property Get IYourObject_YourProp() As String 'raise an event IYourObject_YourProp = mYourProp m_Events.Event1 End PropertyPrivate Sub IYourObject_YourSub(ByVal i As Integer) ' raise an event m_Events.Event2 i End Sub窗体: Option Explicit Implements IEventsDim MyClassObj As IYourObjectPrivate Sub Command1_Click() MsgBox MyClassObj.YourProp End Sub' IEvents implementation Public Sub IEvents_Event1() MsgBox "Raised Event1 !!" End SubPublic Sub IEvents_Event2(ByVal Param As Integer) MsgBox "Raised Event2 !!" End Sub' form code Private Sub Form_Load() ' create the object Set MyClassObj = New CYourObject MyClassObj.SetEventHandler Me End Sub
注意看下面的“coclass Timer”中的“[default, source] interface TimerEvents”
应该有办法作事件接口
但我也没做过
[
uuid(33AD4F28-6699-11CF-B70C-00AA0060D393),
helpstring("A control which can execute code at regular intervals by causing a Timer event."),
helpcontext(0x000dfa33),
noncreatable
]
coclass Timer {
[default] interface _Timer;
[default, source] interface TimerEvents;
}; [
odl,
uuid(33AD4F29-6699-11CF-B70C-00AA0060D393),
helpstring("A control which can execute code at regular intervals by causing a Timer event."),
helpcontext(0x000dfa33),
hidden,
nonextensible
]
interface _Timer : IDispatch {
[propget]
HRESULT _stdcall _Default([out, retval] VARIANT_BOOL* );
[propput]
HRESULT _stdcall _Default([in] VARIANT_BOOL );
[propget, helpstring("Returns the name used in code to identify an object."), helpcontext(0x000dfc75)]
HRESULT _stdcall Name([out, retval] BSTR* );
[propget, helpstring("Returns/sets the number identifying a control in a control array."), helpcontext(0x000dfc5c)]
HRESULT _stdcall Index([out, retval] short* );
[propget, helpstring("Returns/sets a value that determines whether an object can respond to user-generated events."), helpcontext(0x000dfc40)]
HRESULT _stdcall Enabled([out, retval] VARIANT_BOOL* );
[propput, helpstring("Returns/sets a value that determines whether an object can respond to user-generated events."), helpcontext(0x000dfc40)]
HRESULT _stdcall Enabled([in] VARIANT_BOOL );
[propget, helpstring("Returns/sets the number of milliseconds between calls to a Timer control's Timer event."), helpcontext(0x000dfc5d)]
HRESULT _stdcall Interval([out, retval] long* );
[propput, helpstring("Returns/sets the number of milliseconds between calls to a Timer control's Timer event."), helpcontext(0x000dfc5d)]
HRESULT _stdcall Interval([in] long );
[propget, helpstring("Returns the object on which this object is located."), helpcontext(0x000dfc87)]
HRESULT _stdcall Parent([out, retval] Form** );
[propget, helpstring("Stores any extra data needed for your program."), helpcontext(0x000dfcab)]
HRESULT _stdcall Tag([out, retval] BSTR* );
[propput, helpstring("Stores any extra data needed for your program."), helpcontext(0x000dfcab)]
HRESULT _stdcall Tag([in] BSTR );
[hidden, helpstring("Adds an item to a Listbox or ComboBox control or a row to a Grid control."), helpcontext(0x000dfa84)]
HRESULT _stdcall AddItem(
[in] BSTR ,
[in] VARIANT );
[hidden, helpstring("Removes an item from a ListBox or ComboBox control or a row from a Grid control."), helpcontext(0x000dfaa6)]
HRESULT _stdcall RemoveItem([in] short );
[hidden, helpstring("Clears the contents of a control or the system Clipboard."), helpcontext(0x000dfa87)]
HRESULT _stdcall Clear();
}; [
odl,
uuid(33AD4F2A-6699-11CF-B70C-00AA0060D393),
helpstring("A control which can execute code at regular intervals by causing a Timer event."),
helpcontext(0x000dfa33)
]
interface TimerEvents : IUnknown {
[helpstring("Occurs when a preset interval for a Timer control has elapsed."), helpcontext(0x000dfb6a)]
HRESULT _stdcall Timer();
};
新建一个普通工程,添加一个窗体,3个类模块:类模块IEvents:
Option Explicit'interface IEventsPublic Sub Event1()
End SubPublic Sub Event2(ByVal Param As Integer)
End Sub类模块IYourObject:
Option Explicit
'interface IYourObjectPublic Property Get YourProp() As String
End PropertyPublic Sub YourSub(ByVal i As Integer)
End SubPublic Sub SetEventHandler(EH As IEvents)
End Sub类模块CYourObject:
'CYourObject - implements the IYourObject interface
Option Explicit
Implements IYourObject
Dim m_Events As IEvents
Private mYourProp As StringPublic Property Get YourProp() As String
YourProp = IYourObject.YourProp
End Property
Public Sub YourSub(ByVal i As Integer)
IYourObject_YourSub i
End SubPublic Sub SetEventHandler(EH As IEvents)
IYourObject.SetEventHandler EH
End SubPrivate Sub Class_Initialize()
mYourProp = "this is test"
End SubPrivate Sub IYourObject_SetEventHandler(EH As IEvents)
Set m_Events = EH
End SubPrivate Property Get IYourObject_YourProp() As String
'raise an event
IYourObject_YourProp = mYourProp
m_Events.Event1
End PropertyPrivate Sub IYourObject_YourSub(ByVal i As Integer)
' raise an event
m_Events.Event2 i
End Sub窗体:
Option Explicit
Implements IEventsDim MyClassObj As IYourObjectPrivate Sub Command1_Click()
MsgBox MyClassObj.YourProp
End Sub' IEvents implementation
Public Sub IEvents_Event1()
MsgBox "Raised Event1 !!"
End SubPublic Sub IEvents_Event2(ByVal Param As Integer)
MsgBox "Raised Event2 !!"
End Sub' form code
Private Sub Form_Load()
' create the object
Set MyClassObj = New CYourObject
MyClassObj.SetEventHandler Me
End Sub