+ All Categories
Home > Documents > Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park.

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

Date post: 13-Jan-2016
Category:
Upload: eugene-fisher
View: 231 times
Download: 1 times
Share this document with a friend
Popular Tags:
30
Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park
Transcript
Page 1: Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park.

Chapter 1: Hello, MFCWindows Programming Model

Department of Digital Contents

Sang Il Park

Page 2: 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)

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

Procedural Programming Model

int main(){ funcA();

funcB();

return 0;}

int main(){ funcA();

funcB();

return 0;}

{ …… return;}

{ …… return;}

{ funcC(); return;}

{ funcC(); return;}

{ ….. return;}

{ ….. return;}

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

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)

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

Event-driven Programming Model

EVENT!!!

Help me!

Message

Event Handlin

g

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

Event-driven Programming Model

int WinMain(){

return 0;}

int WinMain(){

return 0;}

Message Loop

Messag

e 1

Messag

e 2

Messag

e 3

Window ProcedureWindow Procedure

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

– 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

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

• Event-Driven Programming Model with more details:

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

Messages, messages and more Messages

Message Sent When

WM_CHAR A character is input from the keyboard.

WM_COMMAND The user selects an item from a menu, or a control sends a notification to its parent.

WM_CREATE A window is created.

WM_DESTROY A window is destroyed.

WM_LBUTTONDOWN

The left mouse button is pressed.

WM_LBUTTONUP The left mouse button is released.

WM_MOUSEMOVE The mouse pointer is moved.

WM_PAINT A window needs repainting.

WM_QUIT The application is about to terminate.

WM_SIZE A window is resized.

Common Windows Messages

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

A Simple Event-driven Coding Practice

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

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.

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

Code: #include <iostream>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 <iostream>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;

}

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

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

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

Code: #include <iostream>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 <iostream>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;

}

MessageMessage

Message Handler

Message Handler

MessageLoop

MessageLoop

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

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;

}}

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

API Windows Programming(or SDK-Style)

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

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

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

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.

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

Begin Win32 Project

• FileNewProjectWin32 Project

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

Begin Win32 Project

• Application Setting

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

Begin Win32 Project

• Set “Character Set ” as “Not Set ”

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

Adding “Main.cpp” #include <windows.h>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 <windows.h>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);}

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

Compile and Run it!

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

Code looks complex, but…#include <windows.h>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 <windows.h>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!SAME STRUCTURE!

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

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!Add this!

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

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 thisAdd this

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

Result:

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

Summary:

WinMain(…) main function{

WNDCLASS … Define a new programCreateWindows (…) Create a window

while( GetMessage (…)) Message Loop{

DispatchMessage(…) Message Handler} (Windows Procedure)

}

Win32 Program Structure

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

Then, What is MFC ?

To be continued…

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

Back in your home…

• Read and try:Chapter1 – The Windows Programming

modelChapter1 – Introducing MFCChapter1 – Your First MFC Application


Recommended