MFC Windows Programming: Document/View Approach More detailed notes at: 360/notes-html/class15.htm.

Slides:



Advertisements
Similar presentations
Using Macros and Visual Basic for Applications (VBA) with Excel
Advertisements

MFC Workshop: Intro to the Document/View Architecture.
Advanced Microsoft Word Hosted by Jared Hoffman Topics Keyboard Shortcuts Customizing Toolbars and Menus Auto Format & Auto Correct Tabs Inserting Pictures.
© by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Visual Basic 2010 How to Program Reference: Instructor: Maysoon Bin Duwais slides Visual Basic 2010 how to program by Deitel © by Pearson Education,
Automating Tasks With Macros
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.
Macros Tutorial Week 20. Objectives By the end of this tutorial you should understand how to: Create macros Assign macros to events Associate macros with.
Introduction to Microsoft Windows MFC Programming: the Application/Window Approach Lecture 4.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
Access Tutorial 10 Automating Tasks with Macros
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
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.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 22 Macros.
Building an MFC Application Using the Wizard. Terms Solution – A container for related projects – An executable or DLL – May be constructed from multiple.
Department of Mechanical Engineering, LSUSession VII MATLAB Tutorials Session VIII Graphical User Interface using MATLAB Rajeev Madazhy
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
© 2008 The McGraw-Hill Companies, Inc. All rights reserved. WORD 2007 M I C R O S O F T ® THE PROFESSIONAL APPROACH S E R I E S Lesson 21 Fields and Forms.
Visual C++ Lecture 11 Friday, 29 Aug Windows Graphic User Interface l Event driven programming environment l Windows graphic libraries (X11 on Unix,
Introduction to Matlab & Data Analysis
Spreadsheets in Finance and Forecasting Presentation 9 Macros.
Automating Database Processing Chapter 6. Chapter Introduction Design and implement user-friendly menu – Called navigation form Macros – Automate repetitive.
C++ MFCs CS 123/CS 231. MFC: Writing Applications for Windows zClasses in MFC make up an application framework zFramework defines the skeleton of an application.
BZUPAGES.COM Visual Programming Lecture – 5 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.
Overview of Previous Lesson(s) Over View  Microsoft Foundation Classes (MFC)  A set of predefined classes upon which Windows programming with Visual.
Classic Controls Trần Anh Tuấn A. Week 1 How to create a MFC project in VS 6.0 How to create a MFC project in VS 6.0 Introduction to Classic Controls.
OBJECTIVE  After completing this Lab, students will upgrade their knowledge in the field of VC++.  Students will also get the clear view about the concepts.
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.
BZUPAGES.COM Visual Programming Lecture – 6- 7 Miss. SADAF MAJEED SIAL Computer Science Department Bahauddin Zakariya University Multan.
Chapter Two Creating a First Project in Visual Basic.
Creating Graphical User Interfaces (GUI’s) with MATLAB By Jeffrey A. Webb OSU Gateway Coalition Member.
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Bitmap (Chapter 15).
Chapter2: Drawing a Window. Review: Introducing MFC.
Copyright © Curt Hill More Components Varying the input of Dev-C++ Windows Programs.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 10 1 Microsoft Office Access 2003 Tutorial 10 – Automating Tasks With Macros.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Chapter2: Drawing a Window
Chapter 8 Dialog Boxes and Property Sheet
Overview of Previous Lesson(s) Over View 3 Program.
Chapter 6: FILE I/O and Serialize CFile class. FILE I/O Serialization and the CArchive Class.
Microsoft Foundation Classes
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
Overview of Previous Lesson(s) Over View  Windows Programming  WinMain()  Where execution of the program begins and basic program initialization is.
LC++ programming in Windows environment Department of Information Engineering INFORMATION TECHNOLOGY dr. László Dudás 10./0. lThe Visual Component Library.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Part II Document/View Architecture. Chapter 9 Documents, Views, and the Single Document Interface.
Creating ActiveX Controls at runtime If you need to create an ActiveX Control at runtime without a resource template entry, follow the programming steps.
McGraw-Hill/Irwin The Interactive Computing Series © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Microsoft Excel 2002 Using Macros Lesson.
Chapter 14 Windows Programming with the Microsoft Foundation Classes
Introduction to Windows Programming
MULTIPLE DOCUMENTS AND MULTIPLE VIEWS Prosise - Chapter 11
Document/View Architecture
Message Handling in MFC
Microsoft Foundation Classes MFC
Steps to Build Frame Window Recipe Application
MFC Dialog Application
CIS16 Application Development Programming with Visual Basic
MFC Document/View programs
C++ programming in Windows environment The Borland C++ Builder
Overview of the IDE Visual Studio .NET is Microsoft’s Integrated Development Environment (IDE) for creating, running and debugging programs (also.
Chapter 12 Windows Programming with the Microsoft Foundation Classes
Steps to Build Frame Window Recipe Application
Chapter 4 Enhancing the Graphical User Interface
Presentation transcript:

MFC Windows Programming: Document/View Approach More detailed notes at: 360/notes-html/class15.htm

MFC Windows Programming: App/Window Approach vs. Document/View Approach An App/Window approach program creates application and window objects Mirrors Win32 API program organization Main difference--MFC automates and masks details … and does many other necessary tasks But data & rendering of data are intertwined Frequently, data members exist in window class – Example in MSG2005.CPP: Output string defined in window-based class But output string is data Really has nothing to do with window it’s being displayed in

Doc/View Achieves Separation of Data and Data Presentation  Encapsulates data in a CDocument class object  Encapsulates data display mechanism and user interaction with it in a CView class object  Classes derived from CDocument – Should handle anything affecting an application's data  Classes derived from CView – Should handle display of data and user interactions with that display  Conceptually data is different from rendering of data  In an App/Window approach program they are mixed together in same window class  Frequently we need to have different views of same data – (e.g., displaying data in a window or on a printer)  So it’s a good idea to separate data and data presentation

Documents  Document – Contain any forms of data associated with the application (pure data) – Not limited to text – Could be anything game data, graphical data, etc. Other Classes are Still Needed  Still need to create CFrameWnd and CWinApp classes  But their roles are reduced

Views  A rendering of a document; a physical representation of the data  Provides mechanism for displaying data stored in a document  Defines how data is to be displayed in a window  Defines how the user can interact with it Document Interfaces  Single Document interface (SDI) application – Program that deals with one document at a time – All our programs to date have been SDI apps  Multiple Document Interface (MDI) application – Program organized to handle multiple documents simultaneously – More than one document can be displayed in a window at the same time – Example of an MDI application: Microsoft Excel

Frame Window  Window in which a view of a document is displayed  A document can have multiple views associated with it – different ways of looking at the same data  But a view has only one document associated with it

Template/Document/View/Window MFC Template Class Object  Handles coordination between documents, views, and frame windows  In general: – Application object creates a template... – which coordinates display of document's data… – in a view… – inside a frame window  i.e., our CWinApp object creates a Document Template which creates a CDocument object and a CFrameWnd object – The CFrameWnd object creates a CView object – Which displays the document data

Dynamic Creation  In Doc/View approach, objects are dynamic  Doc/View program is run – Its frame window, document, and view are created dynamically – Often Doc/View objects are synthesized from file data They need to be created at load time (run time) – To allow for dynamic creation, use dynamic creation macros in classes derived from CFrameWnd, CDocument, and CView) Serialization  Provides for storage/retrieval of document data  Usually to/from a disk file  CDocument class has serialization built into it – So in DOCUMENT/VIEW apps, saving/storing data is straightforward

SDI Doc/View CWinApp’s Class InitInstance()  Create document template and window: CSingleDocTemplate *pDocTemplate( IDR_MAINFRAME // Resource ID RUNTIME_CLASS(CPgmDoc) // the document RUNTIME_CLASS(CMainFrame) // main SDI frame window RUNTIME_CLASS(CPgmView)); // the view // RUNTIME_CLASS(): a macro returns pointers to app, doc, view classes // Will only work after dynamic declaration/creation macros are invoked AddDocTemplate(pDocTemplate): // Adds template and creates win dow  Finally, show window and update client area as usual: m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); Dynamic Creation Macros  DECLARE_DYNCREATE(class_name) – in declaration (.h file)  IMPLEMENT_DYNCREATE(class_name, parent_class_name) – (in.cpp file)  After IMPLEMENT_DYNCREATE() macro is invoked: – Class is enabled for dynamic creation – Now a template can be created

Microsoft Developer Studio AppWizard and ClassWizard Tools Document/View Programs  Almost always have at least four classes derived from: – CFrameWnd – CDocument – CView – CWinApp  Usually put into separate declaration (.h) and implementation (.cpp) files  Because of template and dynamic creation, there’s lots of initialization  Could be done by hand, but nobody does it that way

ClassWizards  Facilitate message handling in a framework- based MFC application  Tools that connect resources and user-generated events to program response code  Write C++ skeleton routines to handle messages  Insert code into appropriate places in program – Code then can then be customized by hand  Can be used to create new classes or derive classes from MFC base classes – Add new member variables/functions to classes  In.NET many “class wizards” are available through Properties window AppWizard  Tool that generates a Doc/View MFC program framework automatically  Can be built on and customized by programmer  Fast, efficient way of producing Windows Apps  Performs required initialization automatically  Creates functional CFrameWnd, CView, CDocument, CWinApp classes  After AppWizard does it's thing: – Application can be built and run – Full-fledged window with all common menu items, tools, etc.

 Sketch data (points) won't be saved – So leave document (CSketchDoc) class created by AppWizard alone  Base functionality of application (CSketchApp) and frame window (CMainFrame) classes are adequate – Leave them alone  Use ClassWizard to add sketching to CView class SKETCH Application  Example of Using AppWizard and ClassWizard  User can use mouse as a drawing pencil Left mouse button down: – lines in window follow mouse motion  Left mouse button up: – sketching stops  User clicks "Clear" menu item – window client area is erased

Variables  BOOLEAN m_butdn  CPoint m_pt, m_ptold  COLORREF m_color  CDC* pDC Sketching Requirements  If left mouse button is down: – Each time mouse moves: Get a DC Create a pen of drawing color Select pen into DC Move to old point Draw a line to the new point Make current point the old point Select pen out of DC

 4. Add member variables to CSketchView – Can do manually in.h file – Easier to: Select Class View pane Click on SketchView class – Note member functions & variables Right click on CSketchView class – Choose “Add” / “ Variable” – Launches “Add Member Variable Wizard” – Variable Type: enter CPoint – Name: m_pt – Access: Public (default) – Note after “Finish” that it’s been added to the.h file Repeat for other variables (or add directly in.h file): – CPoint m_ptold – bool m_butdn – COLORREF m_color – CDC* pDC Steps in Preparing SKETCH  1. “File / New / Project” – Project Type: “Visual C++ Projects” – Template: “MFC Application” – Enter name: Sketch  2. In “Welcome to MFC Application Wizard” – Application type: “Single Document” Application – Take defaults for all other screens  3. Build Application --> Full-fledged SDI App with empty window and no functionality

 5. Add message handler functions: – Select CSketchView in Class View – Select “Messages” icon in Properties window Results in a list of WM_ messages – Scroll to WM_LBUTTONDOWN & select it – Add the handler by clicking on down arrow and “ OnLButtonDown” Note that the function is added in the edit window and the cursor is positioned over it: – After “TODO…” enter following code: m_butdn = TRUE; m_ptold = point;

 Repeat process for WM_LBUTTONUP handler: – Scroll to WM_LBUTTONUP – Click: “ OnLButtonUp”, – Edit Code by adding: m_butdn = FALSE;

 6. Initialize variables in CSketchView constructor – Double click on CSketchView constructor CSketchView(void) in Class View – After “TODO…”, Add code: m_butdn = FALSE; m_pt = m_ptold = CPoint(0,0); m_color = RGB(0,0,0);  Repeat for WM_MOUSEMOVE – Scroll to WM_MOUSEMOVE – Click: “ OnMouseMove” – Edit by adding code: if (m_butdn) { pDC = GetDC(); m_pt = point; CPen newPen (PS_SOLID, 1, m_color); CPen* pPenOld = pDC->SelectObject (&newPen); pDC->MoveTo (m_ptold); pDC->LineTo (m_pt); m_ptold = m_pt; pDC->SelectObject (pPenOld); }

Menus and Command Messages  User clicks on menu item  WM_COMMAND message is sent  ID_XXX identifies which menu item (its ID)  No predefined handlers  So message mapping macro is different  ON_COMMAND(ID_XXX, OnXxx) – OnXxx() is the handler function – Must be declared in.h file and defined in.cpp file  7. Changing Window’s Properties – Use window’s SetWindowXxxxx() functions In CWinApp-derived class before window is shown and updated – Example: Changing the default window title m_pMainWnd->SetWindowText( TEXT(“Sketching Application”)); – There are many other SetWindowXxxxx() functions that can be used to change other properties of the window  8. Build and run the application

Add Menu Item Command Handler Function – One way: Use “Event Handler Wizard” – In “Resource View” bring up menu editor – Right click on “Red” menu item – Select “Add Event Handler”  “Event Handler Wizard” dialog box Class list: CSketchView Message type: COMMAND Function handler name: accept default – OnDrawingcolorRed Click on “Add and edit” After “TODO…” in editor enter following code: m_color = RGB(255,0,0); Adding Color and Clear Menu Items to SKETCH App  Resource View (sketch.rc folder) – Double click Menu folder – Double click IDR_MAINFRAME menu – Add: “Drawing Color” popup menu item with items: “Red”, ID_DRAWING_COLOR_RED (default) “Blue”, ID_DRAWINGCOLOR_BLUE “Green”, ID_DRAWINGCOLOR_GREEN “Black”, ID_DRAWINGCOLOR_BLACK – Add another main menu item: “Clear Screen”, ID_CLEARSCREEN Set Popup property to False

Another Method of Adding a Menu Item Command Handler – In Class View Select CSketchView – In Properties window select Events (lightning bolt icon) – Scroll down to: ID_DRAWINGCOLOR_RED – Select “COMMAND” – Click “ OnDrawingcolorRed” handler – Edit code by adding: m_color = RGB(255,0,0);

Repeat for ID_DRAWINGCOLOR_BLUE Code: m_color = RGB(0,0,255); Repeat for ID_DRAWINGCOLOR_GREEN Code: m_color = RGB(0,255,0); Repeat for ID_DRAWINGCOLOR_BLACK Code: m_color = RGB(0,0,0); Repeat for ID_CLEAR Code: Invalidate();

Destroying the Window  Just need to call DestroyWindow() – Do this in the CMainFrame class – usually in response to a “Quit” menu item