扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:宋宝华 来源:天极开发 2007年10月16日
关键字:
BOOL CDocument::OnSaveDocument(LPCTSTR lpszPathName) { CFileException fe; CFile* pFile = NULL; pFile = GetFile(lpszPathName, CFile::modeCreate |CFile::modeReadWrite | CFile::shareExclusive, &fe); if (pFile == NULL) { ReportSaveLoadException(lpszPathName, &fe,TRUE, AFX_IDP_INVALID_FILENAME); return FALSE; } CArchive saveArchive(pFile, CArchive::store | CArchive::bNoFlushOnDelete); saveArchive.m_pDocument = this; saveArchive.m_bForceFlat = FALSE; TRY { CWaitCursor wait; Serialize(saveArchive); // save me saveArchive.Close(); ReleaseFile(pFile, FALSE); } CATCH_ALL(e) { ReleaseFile(pFile, TRUE); TRY { ReportSaveLoadException(lpszPathName, e,TRUE, AFX_IDP_FAILED_TO_SAVE_DOC); } END_TRY DELETE_EXCEPTION(e); return FALSE; } END_CATCH_ALL SetModifiedFlag(FALSE); // back to unmodified return TRUE; // success } |
void CDocument::OnCloseDocument() // must close all views now (no prompting) - usually destroys this { // destroy all frames viewing this document // the last destroy may destroy us BOOL bAutoDelete = m_bAutoDelete; m_bAutoDelete = FALSE; // don't destroy document while closing views while (!m_viewList.IsEmpty()) { // get frame attached to the view CView* pView = (CView*)m_viewList.GetHead(); ASSERT_VALID(pView); CFrameWnd* pFrame = pView->GetParentFrame(); ASSERT_VALID(pFrame); // and close it PreCloseFrame(pFrame); pFrame->DestroyWindow(); // will destroy the view as well } m_bAutoDelete = bAutoDelete; // clean up contents of document before destroying the document itself DeleteContents(); // delete the document if necessary if (m_bAutoDelete) delete this; } |
void CExampleDoc::Serialize(CArchive& ar) { if (ar.IsStoring()) { // TODO: add storing code here ar << var1 << var2; } else { // TODO: add loading code here var2 >> var1 >> ar; } } |
void CDocument::UpdateAllViews(CView* pSender, LPARAM lHint, CObject* pHint) // walk through all views { ASSERT(pSender == NULL || !m_viewList.IsEmpty()); // must have views if sent by one of them POSITION pos = GetFirstViewPosition(); while (pos != NULL) { CView* pView = GetNextView(pos); ASSERT_VALID(pView); if (pView != pSender) pView->OnUpdate(pSender, lHint, pHint); } } |
afx_msg void OnFileNew(); afx_msg void OnFileOpen(); |
virtual void OnFileNew(); virtual void OnFileOpen(); virtual CDocument* OpenDocumentFile(LPCTSTR lpszFileName); // open named file |
virtual CDocument* OpenDocumentFile( LPCTSTR lpszPathName, BOOL bMakeVisible = TRUE) = 0; // open named file // if lpszPathName == NULL => create new file with this type virtual CDocument* CreateNewDocument(); |
virtual BOOL OnNewDocument(); virtual BOOL OnOpenDocument(LPCTSTR lpszPathName); |
图3.1文档模板的选择 |
void CDocManager::OnFileNew() { if (m_templateList.IsEmpty()) { ... return ; } //取第一个文档模板的指针 CDocTemplate *pTemplate = (CDocTemplate*)m_templateList.GetHead(); if (m_templateList.GetCount() > 1) { // 如果多于一个文档模板,弹出对话框提示用户选择 CNewTypeDlg dlg(&m_templateList); int nID = dlg.DoModal(); if (nID == IDOK) pTemplate = dlg.m_pSelectedTemplate; else return ; // none - cancel operation } … //参数为NULL的时候OpenDocument File会新建一个文件 pTemplate->OpenDocumentFile(NULL); } |
图3.2文档、框架窗口的创建顺序 |
图3.3视图的创建顺序 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者