Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park.

Slides:



Advertisements
Similar presentations
Prof. Muhammad Saeed. Procedure-Driven Programming Event-Driven Programming Events Messages Event Handlers GUI Windows and Multitasking Queues ( System.
Advertisements

1 Windows Programming CIS 577 Bruce R. Maxim UM-Dearborn.
Computer Graphics1 Windows NT Graphics Interface.
QT – Introduction C++ GUI Programming with Qt 4 Blanchette and Summerfield, Ch. 1 Miller, 2004.
Introduction to Windows Programming. First Windows Program This program simply displays a blank window. The following code is the minimum necessary to.
Introduction to MFC. Motivation Abstract the Windows API Provides additional GUI options Insert Open Inventor into existing MFC application Document /
IN-LAB # 1 - GETTING STARTED - BUT FIRST: 1.Project ideas - watch the scope!!! 2.Check accounts 3. Either myself or the TA need to check you off BEFORE.
Simple Gui in C++. Project Selecting So Far Registering the window class Creating the window.
Simple Gui in C++. Project Selecting So Far Registering the window class Creating the window.
First Windows Program Hello Windows. 2 HELLOWIN.C #include LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ; int WINAPI WinMain (HINSTANCE hInstance,
Intro to Windows Programming Basic Ideas. Program Entry Point Int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
Building a Win32API Application Raw access to the API.
GVE, Hallym University, Song, Chang Geun, Ph.D. 컴퓨터 그래픽스 응용 한림대학교 컴퓨터공학부 송 창근.
Win32 API Programming Event-driven, graphics oriented Example: User clicks mouse over a program’s window area (an event) -- – Windows decodes HW signals.
Further Programming for 3D applications CE Introduction to Further Programming for 3D application Bob Hobbs Faculty of Computing, Engineering and.
6. 마우스.
CHAPTER 2 OpenGL vs. DirectX as API © 2008 Cengage Learning EMEA.
Visual C++ Lecture 11 Friday, 29 Aug Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix,
Prepared by Fareeha Lecturer DCS IIUI 1 Windows API.
Overview of Previous Lesson(s) Over View  Visual C++ provides us with 3 basic ways of creating an interactive Windows application  Using the Windows.
Lecture 5: Interaction 1  Principles of Interactive Graphics  CMSCD2012  Dr David England, Room 711,  ex 2271 
Chapter 1: Hello, MFC Your first MFC Application Department of Digital Contents Sang Il Park.
Lecture Two Event Handling Keyboard and Mouse Input.
QT – Introduction C++ GUI Programming with Qt 4
Winsock Programming Blocking and Asynchronous Sockets for Windows.
Further games and graphics concepts COSE50581 Introduction to Module and Recap Bob Hobbs Faculty of Computing, Engineering and Technology Staffordshire.
BZUPAGES.COM Visual Programming Lecture – 2 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.
Direct3D Workshop November 17, 2005 Workshop by Geoff Cagle Presented by Players 2 Professionals.
GAM666 – Introduction To Game Programming You must use special libraries (aka APIs – application programming interfaces) to make something other than a.
CNS 1410 Graphical User Interfaces. Obectives Students should understand the difference between a procedural program and an Event Driven Program. Students.
Lecture 7 Menu, controls, scroll bars. Menu Menu item sends WM_COMMAND message to the application window Menu is attached to window when the window is.
Presentation Outline Introduction Painting and Repainting GDI.
GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University.
Dialog boxes Modal and modeless dialog boxes Displaying about dialog box: case WM_COMMAND : switch (LOWORD (wParam)) { case IDM_APP_ABOUT : DialogBox (hInstance,
Quanta Confidential QUANTA WBU Study Report 1 昭正 2008/08/01.
Programming Input Devices. Getting the device state Schemes for processing input – Polling – Callbacks Ways to intercept messages from input devices –
Creating a DirectX Project A DirectPLay Chat Program.
1 Programming and Software Engineering. 2 Software Development We can define two main classes of software: User-written software solve a particular problem,
The WM_NCHITTEST Message  This is an internal message used by Windows for generating all the other mouse messages.  Though it almost never needs to be.
Menus  Menus is a feature which is common to almost every windows applications. It is the one of the most common user interface elements around.  Windows.
9. 메뉴와 기타 자원. 1. 자원 – 아이콘, 커서, 메뉴, 대화상자는 모두 Windows 의 자원이다. – 자원은 데이터로 생각할 수 있으며, 프로그램의 EXE 파일에 저장된 다. – 실행 가능한 프로그램의 데이터 영역에는 존재하지 않는다. 자원은 프로그램 소스코드에.
Introduction to OpenGL
Learning Programming Windows API Morpheus Feb-28, 2008.
Overview of Previous Lesson(s) Over View  Windows Programming  WinMain()  Where execution of the program begins and basic program initialization is.
Our good old first Win32 programme of lecture 8 #include int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
Introduction To GDI GDI Definition : It is a interface present in windows which provide function and related structures that an application can use to.
W indows Programming Lecture 08. Brief History of Win Windows is announced 1984 Work begins on Word for Windows 1.0 November 1985: Windows 1.0.
Lecture 8: Discussion of papers OpenGL programming Lecturer: Simon Winberg Attribution-ShareAlike 4.0 International (CC BY-SA 4.0)
Windows Programming Lecture 14. WM_PAINT message Whenever an application receives the WM_PAINT message, whether the entire window needs repainting? WM_PAINT.
Presentation Outline Introduction Painting and Repainting GDI.
Windows Programming Lecture 10.
Windows Programming Lecture 09.
Window.
Building a Win32API Application
18 & 19.
Chapter 1 Hello, MFC.
Windows Programming Model
Aspect-Oriented Programming
Windows Programming using Dev C++
Queued and Nonqueued Messages
The program in traditional OS
Windows Controls & Concepts
Windows Programming Lecture 12
Windows Programming Lecture 13
Building a Win32API Application
CS 1253 Visual Programming Unit I Windows Environment
Windows Programming Lecture 15
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/10.
Graphics Laboratory Korea University
GUI Socket Application
Presentation transcript:

Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park

Programming Models 1.Procedural Programming model 2.Event-Driven Programming model (windows programming model)

Procedural Programming Model int main() { funcA(); funcB(); return 0; } int main() { funcA(); funcB(); return 0; } { …… return; } { …… return; } { funcC(); return; } { funcC(); return; } { ….. return; } { ….. return; }

Procedural Programming Model –Starts with “Main()” function –Ends when main returns –Calls functions sequentially –Basically main function calls other functions (it determines what is called and when)

Event-driven Programming Model EVENT!!! Help me! Message Event Handling

Event-driven Programming Model int WinMain() { return 0; } int WinMain() { return 0; } Message Loop Message 1Message 2Message 3 Window Procedure

–Begins with “WinMain()” function –Starts a massage loop in the WinMain for waiting messages –Gets messages from operating system, a user or the program. –Messages are processed by windows procedure –Ends when Quit message is given Event-driven Programming Model

Event-Driven Programming Model with more details:

Messages, messages and more Messages MessageSent When WM_CHARA character is input from the keyboard. WM_COMMANDThe user selects an item from a menu, or a control sends a notification to its parent. WM_CREATEA window is created. WM_DESTROYA window is destroyed. WM_LBUTTONDOWNThe left mouse button is pressed. WM_LBUTTONUPThe left mouse button is released. WM_MOUSEMOVEThe mouse pointer is moved. WM_PAINTA window needs repainting. WM_QUITThe application is about to terminate. WM_SIZEA window is resized. Common Windows Messages

A Simple Event-driven Coding Practice

Event-driven example Using C++, Take an integer number as an input from the user and do as follows: –If the input is ‘1’, then print “Sejong University”, –If the input is ‘2’, then print “Digital Contents”, –If the input is ‘3’, then print “Bye~” and quit, –Take a new input and repeat.

Code: #include using namespace std; int main() { int i; while(true) { cout<<"Input: "; cin>>i; switch(i) { case 1: cout<<"Sejong University"<<endl; break; case 2: cout<<"Digital Contents"<<endl; break; case 3: cout<<"Bye!"<<endl; return 0; break; default: break; } return 0; } #include using namespace std; int main() { int i; while(true) { cout<<"Input: "; cin>>i; switch(i) { case 1: cout<<"Sejong University"<<endl; break; case 2: cout<<"Digital Contents"<<endl; break; case 3: cout<<"Bye!"<<endl; return 0; break; default: break; } return 0; }

Event-driven example Using C++, Takes an integer number as an input from the user –If the input is ‘1’, then print “Sejong University”, –If the input is ‘2’, then print “Digital Contents”, –If the input is ‘3’, then print “Bye~” and quit, –Take a new input and repeat. Windows Procedure Message (Event)Message (Event) Handler Message Loop

Code: #include using namespace std; int main() { int i; while(true) { cout<<"Input: "; cin>>i; switch(i) { case 1: cout<<"Sejong University"<<endl; break; case 2: cout<<"Digital Contents"<<endl; break; case 3: cout<<"Bye!"<<endl; return 0; break; default: break; } return 0; } #include using namespace std; int main() { int i; while(true) { cout<<"Input: "; cin>>i; switch(i) { case 1: cout<<"Sejong University"<<endl; break; case 2: cout<<"Digital Contents"<<endl; break; case 3: cout<<"Bye!"<<endl; return 0; break; default: break; } return 0; } Message Message Handler Message Loop

A fancier code: int main() { int i; while(true) { cout<<"Input: "; cin>>i; procedure(i); } return 0; } int main() { int i; while(true) { cout<<"Input: "; cin>>i; procedure(i); } return 0; } void procedure(int msg) { switch(msg) { case 1: cout<<"Sejong University"<<endl; break; case 2: cout<<"Digital Contents"<<endl; break; case 3: cout<<"Bye!"<<endl; exit(0); break; default: break; } void procedure(int msg) { switch(msg) { case 1: cout<<"Sejong University"<<endl; break; case 2: cout<<"Digital Contents"<<endl; break; case 3: cout<<"Bye!"<<endl; exit(0); break; default: break; }

API Windows Programming (or SDK-Style)

Win32 ? (= Windows API) Programming is not making everything from nothing Programming is rather assembling existing functions and data types Extended Functions and data types are distributed as a form of library –Ex.) 2D drawing functions (OpenCV library), 3D drawing functions (OpenGL library), Playing music functions (DirectShow library) and so on

Win32 ? ( = Windows API) API (Application Programming Interface) –A library contains functions for controlling and using operating system. –Mostly C functions. Win32 –Name of the Windows API –Collection of C functions for making windows programming (library) –Ex.) Functions for “creating new windows”, “adding a button”, “adding a new menu” and so on.

Begin Win32 Project File  New  Project  Win32 Project

Begin Win32 Project Application Setting

Begin Win32 Project Set “Character Set ” as “Not Set ”

Adding “Main.cpp” #include LONG WINAPI WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { WNDCLASS wc; HWND hwnd; MSG msg; wc.style = 0; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon (NULL, IDI_WINLOGO); wc.hCursor = LoadCursor (NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = "MyWndClass"; RegisterClass (&wc); hwnd = CreateWindow ( "MyWndClass", // WNDCLASS name "SDK Application", // Window title WS_OVERLAPPEDWINDOW, // Window style CW_USEDEFAULT, // Horizontal position CW_USEDEFAULT, // Vertical position CW_USEDEFAULT, // Initial width CW_USEDEFAULT, // Initial height HWND_DESKTOP, // Handle of parent window NULL, // Menu handle hInstance, // Application's instance handle NULL // Window-creation data ); ShowWindow (hwnd, nCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } #include LONG WINAPI WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { WNDCLASS wc; HWND hwnd; MSG msg; wc.style = 0; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon (NULL, IDI_WINLOGO); wc.hCursor = LoadCursor (NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = "MyWndClass"; RegisterClass (&wc); hwnd = CreateWindow ( "MyWndClass", // WNDCLASS name "SDK Application", // Window title WS_OVERLAPPEDWINDOW, // Window style CW_USEDEFAULT, // Horizontal position CW_USEDEFAULT, // Vertical position CW_USEDEFAULT, // Initial width CW_USEDEFAULT, // Initial height HWND_DESKTOP, // Handle of parent window NULL, // Menu handle hInstance, // Application's instance handle NULL // Window-creation data ); ShowWindow (hwnd, nCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint (hwnd, &ps); Ellipse (hdc, 0, 0, 200, 100); EndPaint (hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint (hwnd, &ps); Ellipse (hdc, 0, 0, 200, 100); EndPaint (hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); }

Compile and Run it!

Code looks complex, but… #include LONG WINAPI WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { WNDCLASS wc; HWND hwnd; MSG msg; wc.style = 0; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon (NULL, IDI_WINLOGO); wc.hCursor = LoadCursor (NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = "MyWndClass"; RegisterClass (&wc); hwnd = CreateWindow ( "MyWndClass", // WNDCLASS name "SDK Application", // Window title WS_OVERLAPPEDWINDOW, // Window style CW_USEDEFAULT, // Horizontal position CW_USEDEFAULT, // Vertical position CW_USEDEFAULT, // Initial width CW_USEDEFAULT, // Initial height HWND_DESKTOP, // Handle of parent window NULL, // Menu handle hInstance, // Application's instance handle NULL // Window-creation data ); ShowWindow (hwnd, nCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } #include LONG WINAPI WndProc (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) { WNDCLASS wc; HWND hwnd; MSG msg; wc.style = 0; wc.lpfnWndProc = (WNDPROC) WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon (NULL, IDI_WINLOGO); wc.hCursor = LoadCursor (NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1); wc.lpszMenuName = NULL; wc.lpszClassName = "MyWndClass"; RegisterClass (&wc); hwnd = CreateWindow ( "MyWndClass", // WNDCLASS name "SDK Application", // Window title WS_OVERLAPPEDWINDOW, // Window style CW_USEDEFAULT, // Horizontal position CW_USEDEFAULT, // Vertical position CW_USEDEFAULT, // Initial width CW_USEDEFAULT, // Initial height HWND_DESKTOP, // Handle of parent window NULL, // Menu handle hInstance, // Application's instance handle NULL // Window-creation data ); ShowWindow (hwnd, nCmdShow); UpdateWindow (hwnd); while (GetMessage (&msg, NULL, 0, 0)) { TranslateMessage (&msg); DispatchMessage (&msg); } return msg.wParam; } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint (hwnd, &ps); Ellipse (hdc, 0, 0, 200, 100); EndPaint (hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); } LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; switch (message) { case WM_PAINT: hdc = BeginPaint (hwnd, &ps); Ellipse (hdc, 0, 0, 200, 100); EndPaint (hwnd, &ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; } return DefWindowProc (hwnd, message, wParam, lParam); } SAME STRUCTURE!

Little change in WinProc In the switch statements of WinProc: case WM_PAINT: hdc = BeginPaint(hWnd, &ps); Ellipse (hdc, 0, 0, 200, 100); RECT rect; GetClientRect(hwnd, &rect); DrawText(hdc, "hello, Windows", -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); EndPaint(hWnd, &ps); return 0; case WM_PAINT: hdc = BeginPaint(hWnd, &ps); Ellipse (hdc, 0, 0, 200, 100); RECT rect; GetClientRect(hwnd, &rect); DrawText(hdc, "hello, Windows", -1, &rect, DT_SINGLELINE|DT_CENTER|DT_VCENTER); EndPaint(hWnd, &ps); return 0; Add this!

A Little more change in WinProc In the switch statement, add more event handler: case WM_LBUTTONDOWN: MessageBox(hwnd, "haha", "Test!"), MB_OK); break; case WM_LBUTTONDOWN: MessageBox(hwnd, "haha", "Test!"), MB_OK); break; Add this

Result:

Summary: WinMain(…)  main function { WNDCLASS …  Define a new program CreateWindows (…)  Create a window while( GetMessage (…))  Message Loop { DispatchMessage(…)  Message Handler } (Windows Procedure) } Win32 Program Structure

Then, What is MFC ? To be continued…

Back in your home… Read and try: Chapter1 – The Windows Programming model Chapter1 – Introducing MFC Chapter1 – Your First MFC Application