自定義MFC打開保存對話框的擴展名_第1頁
自定義MFC打開保存對話框的擴展名_第2頁
自定義MFC打開保存對話框的擴展名_第3頁
自定義MFC打開保存對話框的擴展名_第4頁
全文預覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

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"。要想更細致地分類,需要重寫相關(guān)的虛函數(shù),具體做法不唯一,我覺得比較好的一種是重寫CDocManager:DoPromptFileName。 下面的文字有些凌亂,因為它的內(nèi)容是按照我的探索過程組織的。 首先考慮打開對話框。第一步是要弄清,打開對話框是什么時候(在哪)彈出來的? 默認情況下,CDTriNetApp調(diào)用CWinApp:OnFileOpen方法處理FileOpen事件: ON_COMMAND(ID_FILE_OPEN, &CWinApp:OnFileOpen CWinApp:OnFileOpen又調(diào)

3、用CDocManager:OnFileOpen處理FileOpen事件: void CWinApp:OnFileOpen( ENSURE(m_pDocManager != NULL; m_pDocManager->OnFileOpen(; CDocManager:OnFileOpen顯示對話框與用戶交互,然后調(diào)用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方法中顯示自定義對話框,然后調(diào)用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方法 現(xiàn)在考慮保存對話框。第一步仍然是弄清,保存對話框是什么時候(在哪)彈出來的? 分發(fā)消息時,調(diào)用了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調(diào)用CDocument:DoSave,也是一個虛方法:(注:沒有DoSaveAs方法,lp

8、szPathName參數(shù)決定了CDocumen t:DoSave是表現(xiàn)為“保存”還是“另存為”。) 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虛方法調(diào)用CWinApp:DoPromptFileName方法彈出保存對話框,后者又調(diào)用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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論