140 likes | 312 Views
Event Handling. Source Object. Receiver Object. H_Func (Event) { }. Event. Registration. Source vs. Receiver. Source/Receiver. Receiver. Source. Source. Mouse Event Handling in C++/CLI. Event: MouseEventArgs Mouse Event Handlers : Form1_MouseDown, Form1_MouseUp, Form1_MouseMove
E N D
Event Handling Source Object Receiver Object H_Func(Event) { } Event Registration
Source vs. Receiver Source/Receiver Receiver Source Source
Mouse Event Handling in C++/CLI • Event:MouseEventArgs • Mouse Event Handlers: Form1_MouseDown, Form1_MouseUp, Form1_MouseMove • Registration (delagate): • this->MouseDown+= gcnewMouseEventHandler(…); • this->MouseUp+= gcnewMouseEventHandler(…); • this->MouseMove+= gcnewMouseEventHandler(…);
Event Handling in an Window-based Application (win32) MSG Msg; while (GetMessage(&Msg, NULL, 0, 0)>0) { TranslateMessage(&Msg); // ignore DispatchMessage(&Msg); }
Event-driven Programming • Programming paradigm • State representation • State presentation : Form1_Paint in C++/CLI • Event handling and state change • Request for presentation to reflect new state:Invalidate in C++/CLI
Double Buffering • Flicking problem with single buffering • Double buffering • Front buffer for display • Back buffer for composition • Triple buffering
Single Buffering Display Frame Buffer Composition
Double Buffering in Computer Graphics Display Front Back Composition Swap Buffers Display Back Front Composition
Double Buffering in C++/CLI Window Image Front Back Draw Composition
Double Buffering in C++/CLI • Step 1: Create the back buffer and its graphics • Bitmap^ backBuffer = gcnew Bitmap(width, height); • Graphics^ gBackBuffer = Graphics::FromImage(backBuffer); • Step 2: Compose the scene in the back buffer • gBackBuffer->Clear(Color::White); • gBackBuffer->FillEllipse(…); • .... • Step 3: copy the back buffer to the front • e->Graphics->DrawImageUnscaled(backBuffer, 0, 0);
An Example • Event-driven programming paradigm • Class • Array/Clollection • Event Handling • Double Buffering