Chapter 1: Hello, MFC Your first MFC Application 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.
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.
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)
Introduction to Microsoft Windows MFC Programming: the Application/Window Approach Lecture 4.
MFC Workshop: Intro to MFC. What is MFC? Microsoft Foundation Classes C++ wrappers to the Windows SDK An application framework A useful set of extensions.
Chapter 6: Graphical User Interface (GUI) and Object-Oriented Design (OOD) J ava P rogramming: Program Design Including Data Structures Program Design.
GVE, Hallym University, Song, Chang Geun, Ph.D. 컴퓨터 그래픽스 응용 한림대학교 컴퓨터공학부 송 창근.
INTRODUCTION TO VC++ As the Microsoft Windows 3.X and then 5.5 operating system was becoming popular, many programmers were interested in creating graphical.
Win32 API Programming Event-driven, graphics oriented Example: User clicks mouse over a program’s window area (an event) -- – Windows decodes HW signals.
Bertrand Bellenot ROOT Users Workshop Mar ROOT GUI Builder Status & Plans ROOT & External GUI World MFC, FOX, Qt, PVSS… Snapshot of the Future.
Building an MFC Application Using the Wizard. Terms Solution – A container for related projects – An executable or DLL – May be constructed from multiple.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition1  Learn about basic GUI components.  Explore how the GUI components JFrame,
Overview of Previous Lesson(s) Over View  Visual C++ provides us with 3 basic ways of creating an interactive Windows application  Using the Windows.
Chapter 1: Hello, MFC Windows Programming Model Department of Digital Contents Sang Il Park.
MFC Windows Programming: Document/View Approach More detailed notes at: 360/notes-html/class15.htm.
QT – Introduction C++ GUI Programming with Qt 4
Overview of Previous Lesson(s) Over View  Microsoft Foundation Classes (MFC)  A set of predefined classes upon which Windows programming with Visual.
Microsoft Foundation Classes. What is MFC? Set of C++ classes written by MS Simplifies writing complex programs Covers many areas: – GUI – I/O – O/S interfaces.
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.
Presentation Outline Introduction Painting and Repainting GDI.
Bitmap (Chapter 15).
Chapter2: Drawing a Window. Review: Introducing MFC.
GUI-Based Programming ECE 417/617: Elements of Software Engineering Stan Birchfield Clemson University.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
Chapter2: Drawing a Window
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.
Introduction to OpenGL
Chapter 6 Graphical User Interface (GUI) and Object-Oriented Design (OOD)
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.
Part II Document/View Architecture. Chapter 9 Documents, Views, and the Single Document Interface.
Drawing in Windows. To help with drawing on the Windows operating system, Microsoft created the Graphical Device Interface, abbreviated as GDI. – It 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)
Visual Info Processing Programming Guide
Message Handling in MFC
Microsoft Foundation Classes MFC
Presentation Outline Introduction Painting and Repainting GDI.
Windows Programming Lecture 10.
Windows Programming Lecture 09.
Window.
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
CS 1253 Visual Programming Unit I Windows Environment
Windows Development Dynadata Copyright, 2014 © DynaData S.A. 1/10.
Graphics Laboratory Korea University
GUI Socket Application
Presentation transcript:

Chapter 1: Hello, MFC Your 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 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

Compile and Run it!

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

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 project and run it! For set up the project: In Textbook Chapter 1-3: Your First MFC Application - Building the Application

Look into the codes classes

Code Structure myApp (CMyApp : CWinApp) m_pMainFrame (CMainWindow : CFrameWnd) Program itself Running message loop “Look” of the application Taking 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; 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 CWnd CFrameWnd CMainWindow

Code Structure myApp (CMyApp : CWinApp) m_pMainFrame (CMainWindow : CFrameWnd) Program itself Running message loop “Look” of the application Taking 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 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”

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

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 graphics A 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 graphics A game company took care of the graphics drivers Too complicated, so people started to think about “Device-Independent”

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 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. ApplicationsGDI Output 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

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

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

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