[ Home | Syllabus | Course Notes | Assignments | Search]
A programming model:
Easiest way to generate a Document/View application is to use VC++ Application Wizard
Now it is instructive to look at what you get for free.
I called the project (Skeleton) --
NOTE: this example was generated from an older version of Visual C++ and there may be some
differences from newer versions.
#if !defined(AFX_SKELETON_H__BA3C1EA4_4590_11D1_A985_00609752BFB9__INCLUDED_) #define AFX_SKELETON_H__BA3C1EA4_4590_11D1_A985_00609752BFB9__INCLUDED_ #include "resource.h" // main symbols class CSkeletonApp : public CWinApp { public: CSkeletonApp(); // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSkeletonApp) public: virtual BOOL InitInstance(); //}}AFX_VIRTUAL // Implementation //{{AFX_MSG(CSkeletonApp) afx_msg void OnAppAbout(); // NOTE - the ClassWizard will add and remove member functions here. // DO NOT EDIT what you see in these blocks of generated code ! //}}AFX_MSG DECLARE_MESSAGE_MAP() }; ///////////////////////////////////////////////////////////////////////////// //{{AFX_INSERT_LOCATION}} // Microsoft Developer Studio will insert additional declarations immediately before the previous line. #endif // !defined(AFX_SKELETON_H__BA3C1EA4_4590_11D1_A985_00609752BFB9__INCLUDED_)
FOOTNOTES
BEGIN_MESSAGE_MAP(CSkeletonApp, CWinApp) //{{AFX_MSG_MAP(CSkeletonApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP // Standard file based document commands ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew) ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) END_MESSAGE_MAP() // The one and only CSkeletonApp object CSkeletonApp theApp; ///////////////////////////////////////////////////////////////////////////// // CSkeletonApp initialization BOOL CSkeletonApp::InitInstance() { // Change the registry key under which our settings are stored. // You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CSkeletonDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CSkeletonView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo); // Dispatch commands specified on the command line if (!ProcessShellCommand(cmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; }
FOOTNOTES
Documents contain the data, could be text, pictures, sound, video - anything you might want to store on disk
class CSkeletonDoc : public CDocument { protected: // create from serialization only CSkeletonDoc(); DECLARE_DYNCREATE(CSkeletonDoc) // Attributes public: // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSkeletonDoc) public: virtual BOOL OnNewDocument(); virtual void Serialize(CArchive& ar); //}}AFX_VIRTUAL // Implementation public: virtual ~CSkeletonDoc(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif
FOOTNOTES:
There are several important derived classes (see hierarchy chart beginning of text)
Overridables:
class CSkeletonView : public CView { protected: // create from serialization only CSkeletonView(); DECLARE_DYNCREATE(CSkeletonView) // Attributes public: CSkeletonDoc* GetDocument(); // Operations public: // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSkeletonView) public: virtual void OnDraw(CDC* pDC); // overridden to draw this view virtual BOOL PreCreateWindow(CREATESTRUCT& cs); protected: //}}AFX_VIRTUAL // Implementation public: virtual ~CSkeletonView(); #ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const; #endif protected: #ifndef _DEBUG // debug version in SkeletonView.cpp inline CSkeletonDoc* CSkeletonView::GetDocument() { return (CSkeletonDoc*)m_pDocument; } #endif
#ifdef _DEBUG CSkeletonDoc* CSkeletonView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CSkeletonDoc))); return (CSkeletonDoc*)m_pDocument; } #endif //_DEBUG
F11: Like OnPaint - except device context can be printer or print preview
static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL, }; int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; if (!m_wndStatusBar.Create(this) || !m_wndStatusBar.SetIndicators(indicators, sizeof(indicators)/sizeof(UINT))) { TRACE0("Failed to create status bar\n"); return -1; // fail to create } return 0; } BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CFrameWnd::PreCreateWindow(cs); }
BOOL CFrameWnd::OnCmdMsg(...) { CView* pView = GetActiveView(); if(pView != NULL && pView->OnCmdMsg(...)) return TRUE; if (CWnd::OnCmdMsg(...)) return TRUE;
CWinApp* pApp = AfxGetApp(); if(pApp != NULL && pApp->OnCmdMsg(...)) return TRUE; return FALSE; }
Application:
Document:
CFrameWnd
NOTE: only command messages and UI updates (not yet discussed) can be routed.
Standard keyboard and mouse messages must be handled by the active window. (either view or frame as appropriate).
Document and application objects will NEVER receive a noncommand message.