GUIBased Programming ECE 417617 Elements of Software Engineering
GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University
Paradigms Compared Application output callbacks draw Widgets Application input output input The User Traditional command-line GUI-based
Event/Message loop
Event loop – pseudocode int main() { return Win. Main(); } Win. Main() { 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(); } } }
Event loop – Win. Main int WINAPI Win. Main (HINSTANCE h. Inst, HINSTANCE h. Prev. Inst, char * cmd. Param, int cmd. Show) { char class. Name [] = "Winnie"; Win. Class win. Class (Window. Procedure, class. Name, h. Inst); win. Class. Register (); Win. Maker win ("Hello Windows!", class. Name, h. Inst); win. Show (cmd. Show); MSG msg; int status; while ((status = : : Get. Message (& msg, 0, 0, 0)) != 0) { if (status == -1) return -1; : : Dispatch. Message (& msg); } return msg. w. Param; }
Event loop – Window. Proc LRESULT CALLBACK Window. Procedure (HWND hwnd, unsigned int message, WPARAM w. Param, LPARAM l. Param) { switch (message) { case WM_DESTROY: : : Post. Quit. Message (0); return 0; } return : : Def. Window. Proc (hwnd, message, w. Param, l. Param ); }
Event loop (cont. ) int WINAPI Win. Main(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(Peek. Message(&msg, hwnd, 0, 0, PM_REMOVE)) { if(msg. message == WM_QUIT) break; Translate. Message(&msg); Dispatch. Message(&msg); //this calls the CALLBACK function Win. Proc() } else { Draw. Scene(); //display the Open. GL/Direct. X scene } } }
Event loop (cont. ) LRESULT CALLBACK Win. Proc(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(Get. Async. Key. State(VK_ESCAPE)) //it was the escape key Post. Quit. Message(0); //quit program return 0; case WM_DESTROY: //windows wants the program to die case WM_CLOSE: //or the user closed the window Post. Quit. Message(0); //quit program return 0; } return Def. Window. Proc(hwnd, message, wparam, lparam); }
GUI Concepts • Widget – graphic object with functionality; e. g. , button, toolbar, . . . • Window – holds widgets • Child/parent – relationships between windows • Event / message – how windows communicate
Anatomy of a Window title bar menu toolbar client area status bar
Microsoft Windows Programming • History: – Win 32 API: core library written in C – MFC: C++ wrappers around most common Win 32 API functions • • Lots of macros Lots of legacy code Not free, not portable But it works (generally speaking)
- Slides: 11