90 likes | 151 Views
Mouse Event Handling in C++/CLI. Event: MouseEventArgs Button, Clicks, delta, X, Y; Mouse Event Handlers : Form1_MouseDown, Form1_MouseUp, Form1_MouseMove Registration (delegate): this->MouseDown += gcnew MouseEventHandler(…); this->MouseUp += gcnew MouseEventHandler(…);
E N D
Mouse Event Handling in C++/CLI • Event: MouseEventArgs • Button, Clicks, delta, X, Y; • Mouse Event Handlers: Form1_MouseDown, Form1_MouseUp, Form1_MouseMove • Registration (delegate): • this->MouseDown += gcnew MouseEventHandler(…); • this->MouseUp += gcnew MouseEventHandler(…); • this->MouseMove += gcnew MouseEventHandler(…);
Key Event Handling in C++/CLI • Event: KeyEventArgs, (KeyPressEventArgs) • KeyEventArgs: Alt, Control, Handled, KeyCode, KeyData, KeyValue, Modifiers,Shift, SuppressKeyPress • (KeyPressEventArgs: Handled, KeyChar) • Key Event Handlers: Form1_KeyDown, Form1_KeyUp, Form1_KeyPress • Registration (delagate): • this->KeyDown += gcnew KeyEventHandler(…); • this->KeyUp += gcnew KeyEventHandler(…); • this->KeyPress += gcnew KeyPressEventHandler(…);
Examples • Example testKey, demonstrate how to handle key events • Example Tangram, demonstrate combination of key and mouse events
Event Handling Receiver Object: build a event receiver class (1) use the event source class; (2) A method AA to be delegated (public, same signature) (3) Place/combine the method to event handler (e.g. using += constructor) … = gcnew SayHandler ( handle-of-object, address-of-method AA); Source Object: build a event source class (1) the event : event SayHandler^ OnSay; (2) Event trigger method (e.g., mouse, key, or call the delegate) Event H_Func(Event) {} delegate Delegate: a class (1) create: delegate void SayHandler(String ^name); (2) a method AA to be delegated (same signature) (e.g., in other classes) (3) Place/combine the method on the Delegate (using Delegate’s two constructors). (4) multicast Delegate Chain. (5) Invoke a Delegate (two ways)
Event Source Class ref class EventSource { public: event SayHandler^ OnSay; void Say(String ^name) { OnSay(name); } }; delegate void SayHandler(String ^name);
Event Receiver Classes ref class EventReceiver2 { EventSource ^source; public: EventReceiver2(EventSource ^src) { //assume src not null source = src; source ->OnSay+=gcnewSayHandler (this, &EventReceiver2::SayBye); } void SayBye(String ^name) { Console::Write(“good-bye ”); Console::WriteLine(name); } };
Event Receiver Classes ref class EventReceiver1 { EventSource ^source; public: EventReceiver1(EventSource ^src) { //assume src not null source = src; source ->OnSay += gcnew SayHandler (this, &EventReceiver1::SayHello); source ->OnSay += gcnew SayHandler (this, &EventReceiver1::SayStuff); } void RemoveStuff() { source->OnSay -= gcnew SayHandler (this, &EventReceiver1::SayStuff); } void SayHello(String ^name) { Console::Write(“Hello, ”); Console::WriteLine(name); } void SayStuff(String ^name) { Console::Write(“Nice, ”); Console::WriteLine(name); } };
main void main() { EventSource ^source = gcnew EventSource(); EventReceiver1 ^receiver1 = gcnew EventReceiver1(source); EventReceiver2 ^receiver2 = gcnew EventReceiver2(source); source ->Say(“My Fraser”); Console::WriteLine(“----------------------”); receiver1 ->RemoveStuff(); source ->Say(“Stephen”); };