GUI-Based Programming

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

description

GUI-Based Programming. ECE 417/617: Elements of Software Engineering. Stan Birchfield Clemson University. Paradigms Compared. Application. callbacks. draw. output. Widgets. Application. output. input. input. The User. Traditional command-line. GUI-based. Event/Message loop. - PowerPoint PPT Presentation

Transcript of GUI-Based Programming

Page 1: GUI-Based Programming

GUI-Based Programming

ECE 417/617:Elements of Software Engineering

Stan BirchfieldClemson University

Page 2: GUI-Based Programming

Paradigms Compared

Application

Application

Widgets

The User

draw

output input

callbacksoutput

input

Traditional command-line GUI-based

Page 3: GUI-Based Programming

Event/Message loop

Page 4: GUI-Based Programming

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

}

Page 5: GUI-Based Programming

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

Page 6: GUI-Based Programming

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

Page 7: GUI-Based Programming

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

}}

}

Page 8: GUI-Based Programming

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 programcase WM_KEYDOWN: //user pressed a key

if(GetAsyncKeyState(VK_ESCAPE)) //it was the escape keyPostQuitMessage(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);}

Page 9: GUI-Based Programming

GUI Concepts

• Widget – graphic object with functionality; e.g., button, toolbar, ...

• Window – holds widgets

• Child/parent – relationships between windows

• Event / message – how windows communicate

Page 10: GUI-Based Programming

Anatomy of a Window

title bar

menu

toolbar

client area

status bar

Page 11: GUI-Based Programming

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)