自定义MFC打开保存对话框的扩展名_第1页
自定义MFC打开保存对话框的扩展名_第2页
自定义MFC打开保存对话框的扩展名_第3页
自定义MFC打开保存对话框的扩展名_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、这里的顶目名称是D-TriNet,文档扩展名是.dtn和.csv。 要让打开/保存对话框支持多个扩展名,最简单的方法是修改资源文件中的IDR_DTriNetTYPE字段: STRINGTABLE BEGIN IDR_MAINFRAME "D-TriNet" IDR_DTriNetTYPE "nDTriNetnD-TriNetnD-TriNet Files(*.dtn;*.csvn.dtn;.csvnDTriNet.DocumentnD-TriNet.Document" END 这样做的不足是,文件虽然可以有多个扩展名,但仍然只分为两类:"D-T

2、riNet Files"和"All Files"。要想更细致地分类,需要重写相关的虚函数,具体做法不唯一,我觉得比较好的一种是重写CDocManager:DoPromptFileName。 下面的文字有些凌乱,因为它的内容是按照我的探索过程组织的。 首先考虑打开对话框。第一步是要弄清,打开对话框是什么时候(在哪)弹出来的? 默认情况下,CDTriNetApp调用CWinApp:OnFileOpen方法处理FileOpen事件: ON_COMMAND(ID_FILE_OPEN, &CWinApp:OnFileOpen CWinApp:OnFileOpen又调

3、用CDocManager:OnFileOpen处理FileOpen事件: void CWinApp:OnFileOpen( ENSURE(m_pDocManager != NULL; m_pDocManager->OnFileOpen(; CDocManager:OnFileOpen显示对话框与用户交互,然后调用CWinApp:OpenDocumentFile方法: void CDocManager:OnFileOpen( / prompt the user (with all document templates CString newName; if (!DoPromptFileNa

4、me(newName, AFX_IDS_OPENFILE, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE, NULL return; / open cancelled AfxGetApp(->OpenDocumentFile(newName; / if returns NULL, the user has already been alerted 显然,一种可能的解决办法是绕过CWinApp和CDocManager,在CDTriNetApp:OnFileOpen方法中显示自定义对话框,然后调用CWinApp:OpenDocumentFile方法。

5、ON_COMMAND(ID_FILE_OPEN, &CDTriNetApp:OnFileOpen void CDTriNetApp:OnFileOpen( LPCTSTR szFilter = L"DTriNet文件(*.dtn|*.dtn|CSV文件(*.csv|*.csv|所有文件(*.*|*.*|" CFileDialog oFileDlg(TRUE, L".dtn", NULL, 4|2, szFilter; if(oFileDlg.DoModal( = IDOK OpenDocumentFile(oFileDlg.GetFileName

6、(; / CDTriNetApp不需要重写CWinApp:OpenDocumentFile方法 现在考虑保存对话框。第一步仍然是弄清,保存对话框是什么时候(在哪)弹出来的? 分发消息时,调用了CDocument:DoFileSave虚方法: BOOL CDocument:DoFileSave( DWORD dwAttrib = GetFileAttributes(m_strPathName; if (dwAttrib & FILE_ATTRIBUTE_READONLY / we do not have read-write access or the file does not (no

7、w exist if (!DoSave(NULL TRACE(traceAppMsg, 0, "Warning: File save with new name failed.n" return FALSE; else if (!DoSave(m_strPathName TRACE(traceAppMsg, 0, "Warning: File save failed.n" return FALSE; return TRUE; CDocument:DoFileSave调用CDocument:DoSave,也是一个虚方法:(注:没有DoSaveAs方法,lp

8、szPathName参数决定了CDocumen t:DoSave是表现为“保存”还是“另存为”。) BOOL CDocument:DoSave(LPCTSTR lpszPathName, BOOL bReplace / Save the document data to a file / lpszPathName = path name where to save document file / if lpszPathName is NULL then the user will be prompted (SaveAs / note: lpszPathName can be different

9、 than 'm_strPathName' / if 'bReplace' is TRUE will change file name if successful (SaveAs / if 'bReplace' is FALSE will not change path name (SaveCopyAs CString newName = lpszPathName; if (newName.IsEmpty( CDocTemplate* pTemplate = GetDocTemplate(; ASSERT(pTemplate != NULL; n

10、ewName = m_strPathName; if (bReplace && newName.IsEmpty( newName = m_strTitle; / check for dubious filename int iBad = newName.FindOneOf(_T(":/" if (iBad != -1 newName.ReleaseBuffer(iBad; / append the default suffix if there is one CString strExt; if (pTemplate->GetDocString(str

11、Ext, CDocTemplate:filterExt && !strExt.IsEmpty( ASSERT(strExt0 = '.' int iStart = 0; newName += strExt.Tokenize(_T("", iStart; if (!AfxGetApp(->DoPromptFileName(newName, bReplace ? AFX_IDS_SAVEFILE : AFX_IDS_SAVEFILECOPY, OFN_HIDEREADONLY | OFN_PATHMUSTEXIST, FALSE, pTem

12、plate return FALSE; / don't even attempt to save CWaitCursor wait; if (!OnSaveDocument(newName if (lpszPathName = NULL / be sure to delete the file TRY CFile:Remove(newName; CATCH_ALL(e TRACE(traceAppMsg, 0, "Warning: failed to delete file after failed SaveAs.n" DELETE_EXCEPTION(e; END

13、_CATCH_ALL return FALSE; / reset the title and change the document name if (bReplace SetPathName(newName; return TRUE; / success CDocument:DoSave虚方法调用CWinApp:DoPromptFileName方法弹出保存对话框,后者又调用CDocManager:DoPromptFileName虚方法(情形与打开文件时相同): BOOL CDocManager:DoPromptFileName(CString& fileName, UINT nIDS

14、Title, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate CFileDialog dlgFile(bOpenFileDialog, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, NULL, NULL, 0; CString title; VERIFY(title.LoadString(nIDSTitle; dlgFile.m_ofn.Flags |= lFlags; CString strFilter; CString strDefault; if (pTemp

15、late != NULL ASSERT_VALID(pTemplate; _AfxAppendFilterSuffix(strFilter, dlgFile.m_ofn, pTemplate, &strDefault; else / do for all doc template POSITION pos = m_templateList.GetHeadPosition(; BOOL bFirst = TRUE; while (pos != NULL pTemplate = (CDocTemplate*m_templateList.GetNext(pos; _AfxAppendFilt

16、erSuffix(strFilter, dlgFile.m_ofn, pTemplate, bFirst ? &strDefault : NULL; bFirst = FALSE; / append the "*.*" all files filter CString allFilter; VERIFY(allFilter.LoadString (AFX_IDS_ALLFILTER; strFilter += allFilter; strFilter += (TCHAR'0' / next string please strFilter += _T(

17、"*.*" strFilter += (TCHAR'0' / last string dlgFile.m_ofn.nMaxCustFilter+; dlgFile.m_ofn.lpstrFilter = strFilter; dlgFile.m_ofn.lpstrTitle = title; dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH; INT_PTR nResult = dlgFile.DoModal(; fileName.ReleaseBuffer(; return nResult = I

18、DOK; 看起来,重写CDocManager:DoPromptFileName方法比重写CDocument:DoSave方法要省事些: BOOL CDTriNetDocMgr:DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog, CDocTemplate* pTemplate LPCTSTR strFilter = L"DTriNet文件(*.dtn|*.dtn|CSV文件(*.csv|*.csv|所有文件(*.*|*.*|" CFileDialog dlgFile(bOpenFileDialog, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, strFilter, NULL, 0; CString title; VERIFY(title.LoadString(nIDSTitle; dlgFile.m_ofn.lpstrTitle = title; dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH; INT_PTR nRe

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论