Download presentation
Presentation is loading. Please wait.
Published byDominick Strickland Modified over 8 years ago
1
Part II Document/View Architecture
2
Chapter 9 Documents, Views, and the Single Document Interface
3
3 Document/View Fundamentals Single document/view architecture
4
4 Document/View Fundamentals Application object: –Provides message loop and send messages to the frame window and the view Frame Window object: –Shows menu, toolbar and status bar View object: –Displays(renders) data –Translates mouse and keyboard input into commands to operates the data Document object: –Stores data –Manipulates data
5
5 SDI and MDI Single Document Interface vs. Multiple Document Interface –Different in the number of documents to open at once
6
6 Document Template –Identifies the document class, view class and frame window class. –Stores the resource ID for menu, tool bar and other resources for the frame window MFC Class Hierarchy SDI MDI
7
7 Document Template Initial Setting for an application: InitInstance() BOOL CExFileApp::InitInstance() {... CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CExFileDoc), RUNTIME_CLASS(CMainFrame), RUNTIME_CLASS(CExFileView)); AddDocTemplate(pDocTemplate);... }
8
8 Single Document Interface Example (1/4) New Project: –Single Document + Document/View Support
9
9 Single Document Interface Example (2/4) Change OnDraw function of View Class void CExFileView::OnDraw(CDC* pDC) { CExFileDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); pDC->SetMapMode(MM_LOMETRIC); pDC->Ellipse(100, -100, 600, -600); }
10
10 Single Document Interface Example (3/4) Result
11
11 Single Document Interface Example (4/4) 실행 결과 (cont'd)
12
12 SDI Application A Simple Structure CWinApp CSingleDocTemplateCDocumentCView CFrameWnd
13
13 SDI Application A structure with two views CWinApp CSingleDocTemplateCDocument CView 1 CFrameWnd CView 2
14
SDI Application How to refer each other Document Object View Object Frame Window Object GetFirstViewPosition & GetNextView GetDocument GetActiveDocumentGetActiveView Application Object AfxGetMainWnd AfxGetApp m_pMainWnd GetParentFrame Document Template GetFirstDocTemplatePosition & GetNextDocTemplate GetDocTemplate GetFirstDocPosition & GetNextDoc
15
15 SDI Application Key Functions –CWinApp* AfxGetApp ( ); Return the pointer to the application object –CWnd* AfxGetMainWnd ( ); Return the pointer to the frame window object –CView* CFrameWnd::GetActiveView ( ); Return the pointer to the view object –CDocument* CView::GetDocument ( ); Return the pointer to the document object –POSITION CDocument::GetFirstViewPosition ( ); CView* CDocument::GetNextView (POSITION& rPosition); Access to the view object(s) Document Object m_viewList View #1View #2View #3 NULL
16
Document/View in detail Create a new application as a SDI –Edit the document template string
17
17 Document/View in detail Document template string ExSDI\n\nExSDI\nExSDI 파일 (*.sdi)\n.sdi\nExSDI.Document ① ② ③ ④ ⑤ ⑥ \nExSDI Document ⑦ No.Description 1Title shown on the title bar 2A initial name of the document. If skipped, “untitled” will be given 3Brief name of the document 4Brief name of the document in file dialog box 5Default extension name 6Document ID used in the windows registry 7Document name used in the windows registry
18
Document Class Key CDocument operations FunctionDescription GetFirstViewPositionReturns a POSITION value that can be passed to GetNextView to begin en umerating the views associated with this document GetNextViewReturns a CView pointer to the next view in the list of views associated with this document GetPathNameRetrieves the document's file name and path—for example, "C:\Documents \Personal\MyFile.doc"; returns an empty string if the document hasn't been named GetTitleRetrieves the document's title—for example, "MyFile"; returns an empty stri ng if the document hasn't been named IsModifiedReturns a nonzero value if the document contains unsaved data or 0 if it do esn't SetModifiedFlagSets or clears the document's modified flag, which indicates whether the do cument contains unsaved data UpdateAllViewsUpdates all views associated with the document by calling each view's OnUpdate function
19
19 Document Class Key CDocument Overrides FunctionDescription OnNewDocumentCalled by the framework when a new document is created. Over ride to apply specific initializations to the document object each ti me a new document is created. OnOpenDocumentCalled by the framework when a document is loaded from disk. Override to apply specific initializations to the document object e ach time a document is loaded. DeleteContentsCalled by the framework to delete the document's contents. Ove rride to free memory and other resources allocated to the docum ent before it is closed. SerializeCalled by the framework to serialize the document to or from dis k. Override to provide document-specific serialization code so th at documents can be loaded and saved.
20
20 Document class What happens when: –[FILE]->[New] –[FILE]->[Open...] –[FILE]->[Save] or [File]->[Save as...] DeleteContents() OnNewDocument() DeleteContents() Serialize() OnOpenDocument() Serialize()
21
21 View Class Key CView Overrides FunctionDescription OnDrawCalled to draw the document's data. Override to paint views of a document. OnInitialUpdateCalled when a view is first attached to a document. Override to initialize the view object each time a document is created or loaded. OnUpdateCalled when the document's data has changed and the view nee ds 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.
22
Coding practice Using Document/View architecture, make an application which draws circles and save/load it Things to change CView:: OnDraw() CDocument:: OnNewDocument() Serialize() Things to change CView:: OnDraw() CDocument:: OnNewDocument() Serialize()
23
Splitter Window
24
24 Splitter Window Dynamic Splitter window –Showing Multiple views by using a Single View Class –In total, 4 views (pane) are supported
25
25 Dynamic Splitter Window Application setting for the splitter window
26
Key Member Functions Find out the location of the current pane: 1.Get the current row and col number 2.Implement each pane according to the location bool CSpliterWnd::IsChildPane ( CWnd * wnd, int * row, int * col ) if (col == 0) { … } Else if (col == 1) { …, } if (col == 0) { … } Else if (col == 1) { …, }
27
27 Static Splitter Window –Each pane can use different view class 서로 다른 뷰 클 –256 panes can be created in maximum
28
28 Static Splitter Window Implementation: BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/, CCreateContext* pContext) { m_wndSplitter.CreateStatic(this, 2, 1); m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CFirstView), CSize(300, 200), pContext); m_wndSplitter.CreateView(1, 0, RUNTIME_CLASS(CSecondView), CSize(300, 200), pContext); return TRUE; }
29
Various View Classes
30
View Classes There are many view classes
31
How to select? At application wizard:
32
Example: FormView A View Class similar to a Dialog Box
33
Conclusion: we have learned.. MFC Structures Draw using MFC Mouse and Keyboard CList Timer Menu and tool bar FILE Input/Output Controls Dialog Box Document/View Architecture (SDI and Splitter WIndow)
34
Thank you for your effort Final Exam: June 21 st (Thursday) Final Grade will be available from: June 21 st 18:00 (Our Homepage) Any questions about grade? Visit my office June 22 nd (14:00~17:00) June 25 th (14:00~17:00)
35
Programming Assignment #5 Don’t need to submit Try to make it during vacation for reviewing everything we have learned
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.