chapter 1: hello, mfc your first mfc application department of digital contents sang il park

Post on 13-Jan-2016

232 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Chapter 1: Hello, MFCYour first MFC Application

Department of Digital Contents

Sang Il Park

API Windows Programming(or SDK-Style)

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.

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

Event-DrivenProgramming ModelEvent-DrivenProgramming Model

Compile and Run it!

Summary:

WinMain(…) main function{

WNDCLASS … Registration of the programCreateWindows (…) Define and show a window

while( GetMessage (…)) Get Message{

DispatchMessage(…) Going to Message Handler} (Calls WindProc fucntion)

}

WindProc(…){

switch(message){case …: Message(Event) Handler}

}

Win32 Program Structure

Initialization

Message Loop

Introducing MFC

Microsoft Foundation Classes

• C++ Library– an object-oriented wrapper around the windows

API– More than 200 classes

• Application Framework– Defines a structure of an application for ready-to-

use– Encapsulates virtually every aspect of a

program’s operation: (hide or access)– Provides a well-designed way of making

application: ex) Document/View architecture

Benefits of using c++ and MFC

• Inheritance is the key for reusability– A provided class as a good start point– By adding details or more functions, you can

customize what you want easily

– Ex) Making a button having a movie on it

1. Inherit a regular button class

2. Customize it to have a movie

Your first MFC Application

download hello.h and hello.cpp in our homepage and add it into your projectand run it! http://dasan.sejong.ac.kr/~sipark/class2012/wp/code_0313.zip

For set up the project: In TextbookChapter 1-3: Your First MFC Application - Building the Application

Look into the codesclasses

Code Structure

myApp(CMyApp : CWinApp)

m_pMainFrame(CMainWindow : CFrameWnd)

Program itselfRunning message loop

“Look” of the applicationTaking care of the user interactions

Application Object

• CMyApp class is our program itself• It is inherited (derived) from the CWinApp

class– CWinApp has necessary data but they are hidden– CWinApp class has a message loop but it is

hidden– Customization can be done by overriding

virtual functions ! Ex) InitInstance(), ExitInstance()class CMyApp : public CWinApp { public:

virtual BOOL InitInstance (); };

class CMyApp : public CWinApp { public:

virtual BOOL InitInstance (); };

CMyApp myApp;CMyApp myApp;

Hello.h

Hello.cpp

Frame Window Object• CWnd class

– The most basic class for all windows– Object-oriented interface which contains basic functions

and data for windows– Getting messages,

• CFrameWnd is derived from CWnd class– Interface of the application to the user

• Ready for Mouse input, keyboard input, menu, toolbar and so on

• CMainWindow is our window derived from CFrameWnd– Having Customized constructor– Having Customized OnPaint function

CWndCWnd

CFrameWndCFrameWnd

CMainWindowCMainWindow

Code Structure

myApp(CMyApp : CWinApp)

m_pMainFrame(CMainWindow : CFrameWnd)

Program itselfRunning message loop

“Look” of the applicationTaking care of the user interactions

Message Map

• Where is the switch statement for WM_PAINT ?– Massage map is a trick by microsoft– Massage map is a macro for hiding repeating codings– All CWnd derived classes have all possible windows

message handlers. – You can override any message handler when

necessary

• How to use it:1.Declare that there is a message map by saying

“DECLARE_MASSAGE_MAP” in the class declaration2.Implement the message map by saying

“ON_WM_...” such as “ON_WM_PAINT” between “BEGIN_MESSAGE_MAP” and “END_..”

3.Overriding your message handler. The name of the function is already given such as “OnPaint”

Adding a New Message handler

• Add a member function into your CMainWindow

• Modify your message map

void OnLButtonDown(UINT nFlags, CPoint point);void OnLButtonDown(UINT nFlags, CPoint point);Hello.h

void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point){

AfxMessageBox("Haha");}

void CMainWindow::OnLButtonDown(UINT nFlags, CPoint point){

AfxMessageBox("Haha");}

Hello.cpp

BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_PAINT ()

ON_WM_LBUTTONDOWN()END_MESSAGE_MAP ()

BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd) ON_WM_PAINT ()

ON_WM_LBUTTONDOWN()END_MESSAGE_MAP ()

Hello.cpp

In summary: The big picture

• The Main function is hidden in MFC.

• CMyApp is the program which initializes the windows and runs the message loop.

• CMinWindow is included in CMyApp which is the “look” of the window and processes the messages regarding to Input/Output.

• MFC simplifies the coding work.

Chapter 2: Drawing a Window

Before Windows…

• In 1980s, before the birth of the “windows 95”– MS-DOS (Disk Operating System)

– Graphics Card: CGA(4colors)/EGA(16colors)/VGA(256colors)

A game using CGA graphics cardCGA graphics card

DOS did not take care of graphicsDOS did not take care of graphics

A game company took care of the graphics driversA game company took care of the graphics drivers

Before Windows…

• In 1980s, before the birth of the “windows 95”– MS-DOS (Disk Operating System)

– Graphics Card:

DOS does not take care of graphicsDOS does not take care of graphics

A game company took care of the graphics driversA game company took care of the graphics drivers

Too complicated, so people started to think about

“Device-Independent”

22

Two big things of WindowsTM

• Changes after windows– Multi-tasking

• We can run Multiple applications at the same time• Windows controls and distributes its resources to the

application.

– Device-independent• Windows controls the input and outputs.• Applications only communicate with Windows and do

not directly access to the actual hardwares.

23

Issues when drawing

• Because of the multi-tasking– An application takes just a part of the window:

the position and the size of the window can change

– Multiple applications can run at the same time:There will be overlapping. One is hiding the other

Chapter 2: Drawing a Window

The Windows GDI

25

The Windows GDI

• GDI(Graphics Device Interface)– A part of the Operating System. – Translate the applications’ signals into the

hardwares’ signal.– Determine whether it is shown or not.

Applications GDIOutput Hardwares(Monitor or printer)

Devide-Independent

Device-Dependent

26

GDI and Device Context

• Device Context (DC)– Data structure for information about displaying– It determines how to display in Multi-tasking GUI

environment.

CDC: MFC Device Context Class

A package for displaying:(physical hardware information,Many functions for drawing)

CDC: MFC Device Context Class

A package for displaying:(physical hardware information,Many functions for drawing)

≒ A canvas and tools ready to draw

≒ A canvas and tools ready to draw

27

How to draw in Windows

• Procedure (step-by-Step)1. Application: Requires Device Context

Windows(GDI): Creates a DC and returns its handle

2. Application: Draws on the DCWindows(GDI): Checks the availability and displays the drawing if possibleCDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release D

C

CDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release D

C

CDC DC(this); // require DC // Do some drawing CDC DC(this); // require DC // Do some drawing

or

Using pointer

Using class

28

Various kinds of Device Context

Class Name Description

CPaintDC For drawing in a window's client area (OnPaint handlers only)

CClientDC For drawing in a window's client area (anywhere but OnPaint)

CWindowDC For drawing anywhere in a window, including the nonclient area

CMetaFileDC For drawing to a GDI metafileClass Hierarchy

Drawing test using PaintDC

• Where you should put your drawing code?– In the WM_PAINT message handler ==

OnPaint()void CMainWindow::OnPaint() {

CPaintDC dc(this); dc.Rectangle(50,50,250,250);dc.Ellipse(100,100,200,200);

}

void CMainWindow::OnPaint() {

CPaintDC dc(this); dc.Rectangle(50,50,250,250);dc.Ellipse(100,100,200,200);

}

WM_PAINT??

• WM: Windows Message – A set of the basic messages predefined by MFC– Usually regarding to the creation/changing of the

windows, mouse controlling, and keyboard typing

• WM_PAINT– A signal from windows that it is time to update

the screen: an “invalid region” happens– When:

• A windows is popping up (when creating or maximizing)• Another window which blocked the window is moving

out.• The user (or application) demands it

31

WM_PAINT – Invalid Region

• When it is no longer valid:

32

WM_PAINT from yourself

• You may need to update the screen when the contents change

• You can make Windows to send WM_PAINT message to your application

– Make the screen invalid == Invalidate

void CWnd::Invalidate (BOOL bErase = TRUE);void CWnd::Invalidate (BOOL bErase = TRUE);

Code Practice

1. Draw a small rectangle

2. Enlarge the size of the rectangle when the left mouse button is clicked

3. Reduce the size of the rectangle when the right mouse button is clicked

top related