Presentation is loading. Please wait.

Presentation is loading. Please wait.

GUI Socket Application

Similar presentations


Presentation on theme: "GUI Socket Application"— Presentation transcript:

1 GUI Socket Application
Chapter 08. GUI Socket Application

2 Understanding the structure and principle of Windows GUI applications
Goal Understanding the structure and principle of Windows GUI applications Learning how to make Windows based socket applications Understanding the structure and principle of dialog based applications Learning how to make dialog based socket applications

3 Windows GUI Application (1/8)
Features of Windows GUI application Providing easy graphical user interface using various API (Application Programming Interface) Operating by using message-driven architecture Terms API Core library provided by Windows OS Event / message how windows communicate

4 Windows GUI Application (2/8)
Message-driven architecture ① System Message queue event Application message queue #1 message queue #2 message queue #3 ... processing waiting

5 Event/Message loop

6 Windows GUI Application (3/8)
GUI application code Message processing depending on the window procedure written by programmer Unprocessed message is processed by window OS Terms message handler Message handling code window procedure A set of message handler

7 Windows GUI Application (4/8)
Code example #include <windows.h> // ① LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // ② int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow) // ③ { // window class registration ④ WNDCLASS wndclass; wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndclass.hInstance = hInstance; wndclass.lpszClassName = "MyWindowClass"; wndclass.lpszMenuName = NULL; wndclass.style = CS_HREDRAW | CS_VREDRAW; wndclass.lpfnWndProc = (WNDPROC)WndProc; if(!RegisterClass(&wndclass)) return -1;

8 Windows GUI Application (5/8)
Code example (cont’d) // Window creation ⑤ HWND hWnd = CreateWindow("MyWindowClass", "WinApp", WS_OVERLAPPEDWINDOW, 0, 0, 600, 300, NULL, (HMENU)NULL, hInstance, NULL); if(hWnd == NULL) return -1; ShowWindow(hWnd, nCmdShow); UpdateWindow(hWnd); // message loop ⑥ MSG msg; while(GetMessage(&msg, 0, 0, 0) > 0){ TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;

9 Windows GUI Application (6/8)
Code example (cont’d) // window procedure ⑦ LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg){ case WM_CREATE: return 0; case WM_SIZE: case WM_DESTROY: PostQuitMessage(0); } return DefWindowProc(hWnd, uMsg, wParam, lParam);

10 Windows GUI Application (7/8)
Message-driven architecture ② System Message queue event Application Message queue #1 Message queue #2 Message queue #3 ... application #1 Message loop Message handler #1 Message handler #2 Message handler #3 ••• DefWindowProc() Window procedure

11 Windows GUI Application (8/8)
control A kind of window provided by windows OS Input/output and display information WinApp.cpp

12 windows based socket application
Structure GUITCPServer.cpp Thread 1 Message loop Message handler #1 Message handler #2 Message handler #3 ••• DefWindowProc() Window procedure thread2 thread3 ... Thread n Window message handling Socket comm. and data processing

13 Dialog based application (1/4)
A kind of window containing various control A part of application Processing user input and output

14 Dialog based application (2/4)
Independent dialog based application

15 Dialog based application (3/4)
Code example #include <windows.h> #include "resource.h" // ① BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM); // ② int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR lpCmdLine, int nCmdShow) { DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc); // ③ return 0; } BOOL CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) // ④

16 Dialog based application (4/4)
Code example (cont’d) DlgApp.cpp switch(uMsg){ case WM_INITDIALOG: // ⑤ return TRUE; case WM_COMMAND: // ⑥ switch(LOWORD(wParam)){ case IDOK: EndDialog(hDlg, 0); // ⑦ case IDCANCEL: EndDialog(hDlg, 0); } return FALSE;

17 Dialog based socket application (1/2)
Structure Same as general GUI socket application dialog message handling Socket comm. and data processing Thread 1 thread2 thread3 ... Thread n Message loop Window procedure Message handler #1 Message handler #2 Message handler #3 •••

18 Dialog based socket application (2/2)
Thread synchronization is needed GUITCPclient.cpp Application buffer network thread 1 thread 2


Download ppt "GUI Socket Application"

Similar presentations


Ads by Google