【VC开源代码栏目提醒】:网学会员在VC开源代码频道为大家收集整理了Event1.cpp提供大家参考,希望对大家有所帮助!
// 这是使用应用程序向导生成的 VC++
// 应用程序项目的主项目文件。
#include "stdafx.h"
#using <mscorlib.dll>
#include <tchar.h>
using namespace System;
//delegate
__delegate void Event1_1handler(String*);
__delegate void Event1_2handler(String*);
//Event1 source class
__gc class Event1Source
{
public:
//declare the Events
__event Event1_1handler* OnEvent1_1;
__event Event1_2handler* OnEvent1_2;
//Event raising function
void RaiseE1_1(String* pMessage)
{
OnEvent1_1(pMessage);
}
void RaiseE1_2(String* pMessage)
{
OnEvent1_2(pMessage);
}
//constructor
Event1Source(){}
};
//Event1 receiver class
__gc class Event1Receiver
{
public:
Event1Source* pEvent1Source;
//constructor
Event1Receiver(Event1Source* pE1Source)
{
if(pE1Source==0)
throw new ArgumentNullException(S"You must have event source");
pEvent1Source=pE1Source;
pEvent1Source->OnEvent1_1 +=
new Event1_1handler(this, &Event1Receiver::Event1_1Function);
pEvent1Source->OnEvent1_2 +=
new Event1_2handler(this, &Event1Receiver::Event1_2Function);
//pEvent1Source->OnEvent1_2 -=
//new Event1_2handler(this, &Event1Receiver::Event1_2Function);
}
//Define Event Function
void Event1_1Function(String* pMessage)
{
Console::WriteLine("Event1Receiver First Message : {0}",pMessage);
}
void Event1_2Function(String* pMessage)
{
Console::WriteLine("Event1Receiver Second Message : {0}",pMessage);
}
};
// 这是此应用程序的入口点
int _tmain(void)
{
// TODO: 请用您自己的代码替换下面的示例代码。
Console::WriteLine(S"Event1 example");
//Create Source Object
Event1Source* pE1Source=new Event1Source;
//Create a receiver Object,and bind the object to the source
Event1Receiver* pE1Receiver=new Event1Receiver(pE1Source);
//Fire Event1
Console::WriteLine(S"Fire Event1 two Events:");
pE1Source->RaiseE1_1(S"Hello Event1_1!");
pE1Source->RaiseE1_2(S"Hello Event1_2!");
return 0;
}