MSDN的使用说明:
Event Handling in Native C++:
http://msdn.microsoft.com/en-US/library/ee2k0a7d(v=vs.80).aspx
Event Handling in COM
http://msdn.microsoft.com/en-US/library/hdcxwbd5(v=vs.100).aspx
以下转自网络,用法说明简洁明了,原文链接:http://blog.csdn.net/dalek/article/details/5502624。
#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. ", 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);
//
class CSource
{
public:
__event void MyEvent(int nValue);
};
//
class CReceiver
{
public:
void MyHandler(int nValue)
{
printf_s("MyHandler1 was called with value %d. ", 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)。简单的用法!