Chapter 1 hello mfc windows programming model
This presentation is the property of its rightful owner.
Sponsored Links
1 / 30

Chapter 1: Hello, MFC Windows Programming Model PowerPoint PPT Presentation


  • 110 Views
  • Uploaded on
  • Presentation posted in: General

Chapter 1: Hello, MFC Windows Programming Model. Department of Digital Contents Sang Il Park. Programming Models. Procedural Programming model Event-Driven Programming model (windows programming model). Procedural Programming Model. int main() { funcA(); funcB(); return 0; }.

Download Presentation

Chapter 1: Hello, MFC Windows Programming Model

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Chapter 1 hello mfc windows programming model

Chapter 1: Hello, MFCWindows Programming Model

Department of Digital Contents

Sang Il Park


Programming models

Programming Models

  • Procedural Programming model

  • Event-Driven Programming model(windows programming model)


Procedural programming model

Procedural Programming Model

int main()

{

funcA();

funcB();

return 0;

}

{

……

return;

}

{

…..

return;

}

{

funcC();

return;

}


Procedural programming model1

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-driven Programming Model

EVENT!!!

Help me!

Event Handling

Message


Event driven programming model1

Event-driven Programming Model

int WinMain()

{

return 0;

}

Message Loop

Window Procedure

Message 2

Message 1

Message 3


Event driven programming model2

Event-driven Programming Model

  • 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


Chapter 1 hello mfc windows programming model

  • Event-Driven Programming Model with more details:


Messages messages and more messages

Messages, messages and more Messages

Common Windows Messages


A simple event driven coding practice

A Simple Event-driven Coding Practice


Event driven example

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.


Chapter 1 hello mfc windows programming model

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;

}


Event driven example1

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.

Message (Event)

Message (Event) Handler

Windows Procedure

Message Loop


Chapter 1 hello mfc windows programming model

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;

}

Message

MessageLoop

Message Handler


A fancier code

A fancier code:

int main()

{

inti;

while(true)

{

cout<<"Input: ";

cin>>i;

procedure(i);

}

return 0;

}

void procedure(intmsg)

{

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

API Windows Programming(or SDK-Style)


Win32 windows api

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 api1

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

Begin Win32 Project

  • FileNewProjectWin32 Project


Begin win32 project1

Begin Win32 Project

  • Application Setting


Begin win32 project2

Begin Win32 Project

  • Set “Character Set ” as “Not Set ”


Adding main cpp

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;

}

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

Compile and Run it!


Code looks complex but

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;

}

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

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;

Add this!


A little more change in winproc

A Little more change in WinProc

  • In the switch statement, add more event handler:

Add this

case WM_LBUTTONDOWN:

MessageBox(hwnd, "haha", "Test!"), MB_OK);

break;


Result

Result:


Summary

Summary:

Win32 Program Structure

WinMain(…) main function

{

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

while( GetMessage (…)) Message Loop

{

DispatchMessage(…)  Message Handler} (Windows Procedure)

}


Then what is mfc

Then, What is MFC ?

To be continued…


Back in your home

Back in your home…

  • Read and try:Chapter1 – The Windows Programming modelChapter1 – Introducing MFCChapter1 – Your First MFC Application


  • Login