#include <stdio.h>
class CSource
{
public:
__event void MyEvent(int nValue);
};
class CReceiver
{
public:
void MyHandler(int nValue)
{
printf_s("MyHandler1 was called with value %d./n", nValue);
}
void hookEvent(CSource* pSource)
{
__hook(&CSource::MyEvent, pSource, &CReceiver::MyHandler);
}
void unhookEvent(CSource* pSource)
{
__unhook(&CSource::MyEvent, pSource, &CReceiver::MyHandler);
}
};
int main() {
CSource source;
CReceiver receiver;
receiver.hookEvent(&source);
__raise source.MyEvent(123);
receiver.unhookEvent(&source);
}
当程序执行到__raise source.MyEvent(123)时,程序就会跳到 void MyHandler(int nValue)
执行printf_s("MyHandler1 was called with value %d./n", nValue)。简单的用法!