Title: Programming Windows with MFC
1Programming Windows with MFC
2Chapter 9
- Documents, Views, and
- the Single Document Interface
3MFC 2.0
- Introducing the document/view architecture
- CDocument class
- the application's data
- CView class
- views of data
4Two types of document/view applications
- Single document interface (SDI) applications
- one open document at a time
- Multiple document interface (MDI) applications
- two or more documents to be open concurrently
- multiple views of a given document
5The SDI document/view architecture
6The InitInstance Function Revisited
- CSingleDocTemplate pDocTemplate
- pDocTemplate new CSingleDocTemplate
- ( IDR_MAINFRAME,
- RUNTIME_CLASS (CMyDoc),
- RUNTIME_CLASS (CMainFrame),
- RUNTIME_CLASS (CMyView) )
- AddDocTemplate (pDocTemplate)
- CCommandLineInfo cmdInfo
- ParseCommandLine (cmdInfo)
- if (!ProcessShellCommand (cmdInfo))
- return FALSE
- m_pMainWnd-gtShowWindow (SW_SHOW)
- m_pMainWnd-gtUpdateWindow ()
7The Document Object
- Data is stored in a document object
- Derived from CDocument
8Key CDocument Operations
Function Description
GetFirstViewPosition Returns a POSITION value that can be passed to GetNextView to begin enumerating the views associated with this document
GetNextView Returns a CView pointer to the next view in the list of views associated with this document
GetPathName Retrieves the document's file name and pathfor example, "C\Documents\Personal\MyFile.doc" returns an empty string if the document hasn't been named
GetTitle Retrieves the document's titlefor example, "MyFile" returns an empty string if the document hasn't been named
IsModified Returns a nonzero value if the document contains unsaved data or 0 if it doesn't
SetModifiedFlagS Sets or clears the document's modified flag, which indicates whether the document contains unsaved data
UpdateAllViews Updates all views associated with the document by calling each view's OnUpdate function
9Key CDocument Overridables
Function Description
OnNewDocument Called by the framework when a new document is created. Override to apply specific initializations to the document object each time a new document is created.
OnOpenDocument Called by the framework when a document is loaded from disk. Override to apply specific initializations to the document object each time a document is loaded.
DeleteContents Called by the framework to delete the document's contents. Override to free memory and other resources allocated to the document before it is closed.
Serialize Called by the framework to serialize the document to or from disk. Override to provide document-specific serialization code so that documents can be loaded and saved.
10The View Object
- Render visual representations of a document on
the screen - Translate the user's input
11The GetDocument Function
- A view always belongs to just one document
- A view identify its document by calling
GetDocument - m_pDocument
12CView Overridables
13Key CView Overridables
Function Description
OnDraw Called to draw the document's data. Override to paint views of a document.
OnInitialUpdate Called when a view is first attached to a document. Override to initialize the view object each time a document is created or loaded.
OnUpdate Called when the document's data has changed and the view needs to be updated. Override to implement "smart" update behavior that redraws only the part of the view that needs redrawing rather than the entire view.
14Predefined Command IDs and Command Handlers
- MFC classes
- provide default handlers for common menu commands
- provide update handlers
- ON_COMMAND message-map
15Predefined Command IDs and Command Handlers
Command ID Menu Item Default Handler Prewired?
File menu File menu File menu File menu
ID_FILE_NEW New CWinAppOnFileNew No
ID_FILE_OPEN Open CWinAppOnFileOpen No
ID_FILE_SAVE Save CDocumentOnFileSave Yes
ID_APP_EXIT Exit CWinAppOnAppExit Yes
Edit menu Edit menu Edit menu Edit menu
ID_EDIT_CUT Cut None N/A
ID_EDIT_COPY Copy None N/A
View menu View menu View menu View menu
ID_VIEW_TOOLBAR Toolbar CFrameWndOnBarCheck Yes
ID_VIEW_STATUS_BAR Status Bar CFrameWndOnBarCheck Yes
Window menu (MDI applications only) Window menu (MDI applications only) Window menu (MDI applications only) Window menu (MDI applications only)
Help menu Help menu Help menu Help menu
ID_APP_ABOUT About AppName None N/A
16MFC AppWizard