Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University.

Similar presentations


Presentation on theme: "GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University."— Presentation transcript:

1 GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University

2 Paradigms Compared Application Widgets The User draw outputinput callbacks output input Traditional command-lineGUI-based

3 Event/Message loop

4 Event loop – pseudocode 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(); } int main() { return WinMain(); }

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)


Download ppt "GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University."

Similar presentations


Ads by Google