1 / 11

Understanding GUI-Based Programming: Event Loops and Windows in Software Engineering

This guide covers the essentials of GUI-based programming in the context of software engineering, particularly for ECE 417/617. It explores the event/message loop mechanism in GUI applications using pseudocode, focusing on the WinMain function, window procedures, and event handling such as key presses and window resizing. The document also delves into fundamental GUI concepts including widgets, windows, and event communication to provide a comprehensive understanding for students and software developers. Practical applications and code snippets are included for clarity.

Télécharger la présentation

Understanding GUI-Based Programming: Event Loops and Windows in Software Engineering

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. GUI-Based Programming ECE 417/617:Elements of Software Engineering Stan Birchfield Clemson University

  2. Paradigms Compared Application callbacks draw output Widgets Application output input input The User Traditional command-line GUI-based

  3. Event/Message loop

  4. Event loop – pseudocode int main() { return WinMain(); } WinMain() { while (1) { // loop forever, waiting for an event if (event_exists) { //there is an event, figure out what to do if (event == keydown_a) display(‘user pressed the A key’); else if (event == window_resize) display(‘window resized’); else if (event == repaint) display(‘need to repaint window’); else if (event == keydown_escape) exit_program(); } } }

  5. Event loop – WinMain int WINAPI WinMain (HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow) { char className [] = "Winnie"; WinClass winClass (WindowProcedure, className, hInst); winClass.Register (); WinMaker win ("Hello Windows!", className, hInst); win.Show (cmdShow); MSG msg; int status; while ((status = ::GetMessage (& msg, 0, 0, 0)) != 0) { if (status == -1) return -1; ::DispatchMessage (& msg); } return msg.wParam; }

  6. Event loop – WindowProc LRESULT CALLBACK WindowProcedure (HWND hwnd, unsigned int message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: ::PostQuitMessage (0); return 0; } return ::DefWindowProc (hwnd, message, wParam, lParam ); }

  7. Event loop (cont.) int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow) { HWND hwnd; MSG msg; //initialization code goes here while(1) { // Get message(s) if there is one if(PeekMessage(&msg,hwnd,0,0,PM_REMOVE)) { if(msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); //this calls the CALLBACK function WinProc() } else { DrawScene(); //display the OpenGL/DirectX scene } } }

  8. Event loop (cont.) LRESULT CALLBACK WinProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) { PAINTSTRUCT ps; // Depending on the message -- we'll do different stuff switch(message) { case WM_PAINT: Draw(); return 0; // ESC will quit program case WM_KEYDOWN: //user pressed a key if(GetAsyncKeyState(VK_ESCAPE)) //it was the escape key PostQuitMessage(0); //quit program return 0; case WM_DESTROY: //windows wants the program to die case WM_CLOSE: //or the user closed the window PostQuitMessage(0); //quit program return 0; } return DefWindowProc(hwnd, message, wparam, lparam); }

  9. GUI Concepts • Widget – graphic object with functionality; e.g., button, toolbar, ... • Window – holds widgets • Child/parent – relationships between windows • Event / message – how windows communicate

  10. Anatomy of a Window title bar menu toolbar client area status bar

  11. Microsoft Windows Programming • History: • Win32 API: core library written in C • MFC: C++ wrappers around most common Win32 API functions • Lots of macros • Lots of legacy code • Not free, not portable • But it works (generally speaking)

More Related