Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 1: Hello, MFC Your first MFC Application Department of Digital Contents Sang Il Park.

Similar presentations


Presentation on theme: "Chapter 1: Hello, MFC Your first MFC Application Department of Digital Contents Sang Il Park."— Presentation transcript:

1 Chapter 1: Hello, MFC Your first MFC Application Department of Digital Contents Sang Il Park

2 API Windows Programming (or SDK-Style)

3 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.

4 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); } Event-Driven Programming Model Event-Driven Programming Model

5 Compile and Run it!

6 Summary: WinMain(…)  main function { WNDCLASS …  Registration of the program CreateWindows (…)  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

7 Introducing MFC

8 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

9 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

10 Your first MFC Application download hello.h and hello.cpp in our homepage and add it into your project and run it! http://dasan.sejong.ac.kr/~sipark/class2012/wp/code_0313.ziphttp://dasan.sejong.ac.kr/~sipark/class2012/wp/code_0313.zip For set up the project: In Textbook Chapter 1-3: Your First MFC Application - Building the Application

11 Look into the codes classes

12 Code Structure myApp (CMyApp : CWinApp) m_pMainFrame (CMainWindow : CFrameWnd) Program itself Running message loop “Look” of the application Taking care of the user interactions

13 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; Hello.h Hello.cpp

14 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 CWnd CFrameWnd CMainWindow

15 Code Structure myApp (CMyApp : CWinApp) m_pMainFrame (CMainWindow : CFrameWnd) Program itself Running message loop “Look” of the application Taking care of the user interactions

16 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 declaration 2.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”

17 Adding a New Message handler Add a member function into your CMainWindow Modify your message map 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

18 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.

19 Chapter 2: Drawing a Window

20 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 graphics A game company took care of the graphics drivers

21 Before Windows… In 1980s, before the birth of the “windows 95” –MS-DOS (Disk Operating System) –Graphics Card: DOS does not take care of graphics A game company took care of the graphics drivers Too complicated, so people started to think about “Device-Independent”

22 22 Two big things of Windows TM 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 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

24 Chapter 2: Drawing a Window The Windows GDI

25 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. ApplicationsGDI Output Hardwares (Monitor or printer) Devide- Independent Device- Dependent

26 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

27 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 DC Windows(GDI): Checks the availability and displays the drawing if possible CDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release DC CDC* pDC = GetDC (); // require DC // Do some drawing ReleaseDC (pDC); // release DC CDC DC(this); // require DC // Do some drawing CDC DC(this); // require DC // Do some drawing or Using pointer Using class

28 28 Various kinds of Device Context Class NameDescription CPaintDCFor drawing in a window's client area (OnPaint handlers only) CClientDCFor drawing in a window's client area (anywhere but OnPaint) CWindowDCFor drawing anywhere in a window, including the nonclient area CMetaFileDCFor drawing to a GDI metafile Class Hierarchy

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

30 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 31 WM_PAINT – Invalid Region When it is no longer valid:

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

33 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


Download ppt "Chapter 1: Hello, MFC Your first MFC Application Department of Digital Contents Sang Il Park."

Similar presentations


Ads by Google