1 / 8

Mouse Event Handling in C++/CLI

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(…);

pholland
Download Presentation

Mouse Event Handling in C++/CLI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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(…);

  2. 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(…);

  3. Examples • Example testKey, demonstrate how to handle key events • Example Tangram, demonstrate combination of key and mouse events

  4. 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)

  5. Event Source Class ref class EventSource { public: event SayHandler^ OnSay; void Say(String ^name) { OnSay(name); } }; delegate void SayHandler(String ^name);

  6. 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); } };

  7. 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); } };

  8. 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”); };

More Related