生活随笔
收集整理的這篇文章主要介紹了
MFC 多文档中同时打开多个文档
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
MFC多文檔中的打開(kāi)繼承自CWinAppEx中的函數(shù)OnFileOpen(),這一點(diǎn)在CXXX.cpp中可以看到。比如如下:
[cpp]?view plain
?copy ?? ?? BEGIN_MESSAGE_MAP(CCVMFCApp,?CWinAppEx)?? ????ON_COMMAND(ID_APP_ABOUT,?&CCVMFCApp::OnAppAbout)?? ?????? ????ON_COMMAND(ID_FILE_NEW,?&CWinAppEx::OnFileNew)?? ????ON_COMMAND(ID_FILE_OPEN,?&CWinAppEx::OnFileOpen)?? ?????? ????ON_COMMAND(ID_FILE_PRINT_SETUP,?&CWinAppEx::OnFilePrintSetup)?? END_MESSAGE_MAP()??
中的
ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
它默認(rèn)的只能打開(kāi)一個(gè)文檔,那如果想打開(kāi)多個(gè)文檔腫么辦呢?
這里給一個(gè)輔助類(lèi)MutiOpenDocManager,代碼如下:
MutiOpenDocManager.h:
[cpp]?view plain
?copy ?? ?? ?? ?? #if?!defined(AFX_MUTIOPENDOCMANAGER_H__8E7F5957_C207_4C9E_BA92_979203C32025__INCLUDED_)?? #define?AFX_MUTIOPENDOCMANAGER_H__8E7F5957_C207_4C9E_BA92_979203C32025__INCLUDED_?? ?? #if?_MSC_VER?>?1000?? #pragma?once?? #endif?//?_MSC_VER?>?1000?? ?? class?CMutiOpenDocManager?:?public?CDocManager???? {?? public:?? ????virtual?BOOL?DoPromptFileNames(CStringList&?fileNames,UINT?nIDSTitle,DWORD?lFlags,BOOL?bOpenFileDialog,CDocTemplate*?pTemplate);?? ????virtual?void?OnFileOpen();?? ????CMutiOpenDocManager();?? ????virtual?~CMutiOpenDocManager();?? ????void?AppendFilterSuffix(CString?&filter,?OPENFILENAME?&ofn,?CDocTemplate?*pTemplate,?CString?*pstrDefaultExt);?? ?? };?? ?? #endif?//?!defined(AFX_MUTIOPENDOCMANAGER_H__8E7F5957_C207_4C9E_BA92_979203C32025__INCLUDED_)??
MutiOpenDocManager.cpp:
[cpp]?view plain
?copy ?? ?? ?? ?? #include?"stdafx.h"?? #include?"MutiOpenDocManager.h"?? ?? #ifdef?_DEBUG?? #undef?THIS_FILE?? static?char?THIS_FILE[]=__FILE__;?? #define?new?DEBUG_NEW?? #endif?? ?? ?? ?? ?? ?? CMutiOpenDocManager::CMutiOpenDocManager()?? {?? ?? }?? ?? CMutiOpenDocManager::~CMutiOpenDocManager()?? {?? ?? }?? ?? void?CMutiOpenDocManager::OnFileOpen()?? {?? ????CStringList?newNames;?? ????if(!DoPromptFileNames(newNames,AFX_IDS_OPENFILE,OFN_HIDEREADONLY|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT,TRUE,NULL))?? ????????return;?? ????POSITION?pos=newNames.GetHeadPosition();?? ????while(pos)?? ????{?? ????????CString?newName=newNames.GetNext(pos);?? ????????AfxGetApp()->OpenDocumentFile(newName);?? ????}?? ?? }?? ?? BOOL?CMutiOpenDocManager::DoPromptFileNames(CStringList&?fileNames,?UINT?nIDSTitle,?DWORD?lFlags,?BOOL?bOpenFileDialog,?CDocTemplate?*pTemplate)?? {?? ????CFileDialog?dlgFile(bOpenFileDialog);?? ?? ????CString?title;?? ????VERIFY(title.LoadString(nIDSTitle));?? ?? ????dlgFile.m_ofn.Flags?|=?lFlags;?? ?? ????CString?strFilter;?? ????CString?strDefault;?? ????if?(pTemplate?!=?NULL)?? ????{?? ????????ASSERT_VALID(pTemplate);?? ????????AppendFilterSuffix(strFilter,?dlgFile.m_ofn,?pTemplate,?&strDefault);?? ????}?? ????else?? ????{?? ?????????? ????????POSITION?pos?=?m_templateList.GetHeadPosition();?? ????????BOOL?bFirst?=?TRUE;?? ????????while?(pos?!=?NULL)?? ????????{?? ????????????CDocTemplate*?pTemplate?=?(CDocTemplate*)m_templateList.GetNext(pos);?? ????????????AppendFilterSuffix(strFilter,?dlgFile.m_ofn,?pTemplate,?? ????????????????bFirst???&strDefault?:?NULL);?? ????????????bFirst?=?FALSE;?? ????????}?? ????}?? ?? ?????? ????CString?allFilter;?? ????VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));?? ????strFilter?+=?allFilter;?? ????strFilter?+=?(TCHAR)'\0';????? ????#ifndef?_MAC?? ????strFilter?+=?_T("*.*");?? ????#else?? ????strFilter?+=?_T("****");?? ????#endif?? ????strFilter?+=?(TCHAR)'\0';????? ????dlgFile.m_ofn.nMaxCustFilter++;?? ????dlgFile.m_ofn.lpstrFilter?=?strFilter;?? ?? ????#ifndef?_MAC?? ????dlgFile.m_ofn.lpstrTitle?=?title;?? ????#else?? ????dlgFile.m_ofn.lpstrPrompt?=?title;?? ????#endif?? ?? ????CString?strFileNames;?? ????dlgFile.m_ofn.lpstrFile=strFileNames.GetBuffer(2048);?? ????dlgFile.m_ofn.nMaxFile=2048;?? ????BOOL?bResult=dlgFile.DoModal()==IDOK?TRUE:FALSE;?? ????strFileNames.ReleaseBuffer();?? ????if(!bResult)?? ????????return?FALSE;?? ?????? ????POSITION?pos=dlgFile.GetStartPosition();?? ????while(pos)?? ????{?? ????????fileNames.AddTail(dlgFile.GetNextPathName(pos));?? ????}?? ????return?? ????????TRUE;?? }?? ?? ?? void?CMutiOpenDocManager::AppendFilterSuffix(CString?&filter,?OPENFILENAME?&ofn,?CDocTemplate?*pTemplate,?CString?*pstrDefaultExt)?? {?? ????ASSERT_VALID(pTemplate);?? ????ASSERT_KINDOF(CDocTemplate,?pTemplate);?? ?? ????CString?strFilterExt,?strFilterName;?? ????if?(pTemplate->GetDocString(strFilterExt,?CDocTemplate::filterExt)?&&?? ?????!strFilterExt.IsEmpty()?&&?? ?????pTemplate->GetDocString(strFilterName,?CDocTemplate::filterName)?&&?? ?????!strFilterName.IsEmpty())?? ????{?? ?????????? ????????ASSERT(strFilterExt[0]?==?'.');?? ????????if?(pstrDefaultExt?!=?NULL)?? ????????{?? ?????????????? ????????????*pstrDefaultExt?=?((LPCTSTR)strFilterExt)?+?1;???? ????????????ofn.lpstrDefExt?=?(LPTSTR)(LPCTSTR)(*pstrDefaultExt);?? ????????????ofn.nFilterIndex?=?ofn.nMaxCustFilter?+?1;???? ????????}?? ?? ?????????? ????????filter?+=?strFilterName;?? ????????ASSERT(!filter.IsEmpty());???? ????????filter?+=?(TCHAR)'\0';???? ????????filter?+=?(TCHAR)'*';?? ????????filter?+=?strFilterExt;?? ????????filter?+=?(TCHAR)'\0';???? ????????ofn.nMaxCustFilter++;?? ????}?? }??
然后再在CXXX.cpp的一個(gè)位置插入一句話!至關(guān)重要:
[cpp]?view plain
?copy BOOL?CCVMFCApp::InitInstance()?? {?? ?????? ?????? ?????? ????INITCOMMONCONTROLSEX?InitCtrls;?? ????InitCtrls.dwSize?=?sizeof(InitCtrls);?? ?????? ?????? ????InitCtrls.dwICC?=?ICC_WIN95_CLASSES;?? ????InitCommonControlsEx(&InitCtrls);?? ?? ????CWinAppEx::InitInstance();?? ?? ?????? ????if?(!AfxOleInit())?? ????{?? ????????AfxMessageBox(IDP_OLE_INIT_FAILED);?? ????????return?FALSE;?? ????}?? ????AfxEnableControlContainer();?? ?????? ?????? ?????? ?????? ?????? ?????? ?????? ????SetRegistryKey(_T("應(yīng)用程序向?qū)傻谋镜貞?yīng)用程序"));?? ????LoadStdProfileSettings(4);???? ?? ????InitContextMenuManager();?? ?? ????InitKeyboardManager();?? ?? ????InitTooltipManager();?? ????CMFCToolTipInfo?ttParams;?? ????ttParams.m_bVislManagerTheme?=?TRUE;?? ????theApp.GetTooltipManager()->SetTooltipParams(AFX_TOOLTIP_TYPE_ALL,?? ????????RUNTIME_CLASS(CMFCToolTipCtrl),?&ttParams);?? ?? ?????? ?????? ????CMultiDocTemplate*?pDocTemplate;?? ????m_pDocManager?=?new?CMutiOpenDocManager;?? ?? ????pDocTemplate?=?new?CMultiDocTemplate(IDR_CVMFCTYPE,?? ????????RUNTIME_CLASS(CCVMFCDoc),?? ????????RUNTIME_CLASS(CChildFrame),??? ????????RUNTIME_CLASS(CCVMFCView));?? ????if?(!pDocTemplate)?? ????????return?FALSE;?? ????AddDocTemplate(pDocTemplate);?? ?? ?????? ????CMainFrame*?pMainFrame?=?new?CMainFrame;?? ????if?(!pMainFrame?||?!pMainFrame->LoadFrame(IDR_MAINFRAME))?? ????{?? ????????delete?pMainFrame;?? ????????return?FALSE;?? ????}?? ????m_pMainWnd?=?pMainFrame;?? ?????? ?????? ?? ?? ?????? ????CCommandLineInfo?cmdInfo;?? ????ParseCommandLine(cmdInfo);?? ?? ?? ?????? ?????? ????if?(!ProcessShellCommand(cmdInfo))?? ????????return?FALSE;?? ?????? ????pMainFrame->ShowWindow(m_nCmdShow);?? ????pMainFrame->UpdateWindow();?? ?? ????return?TRUE;?? }??
注意45行的那句話,這是因?yàn)樵趧?chuàng)建類(lèi)實(shí)例的時(shí)候?qū)⑽臋n類(lèi)定義為MutiOpenDocManager類(lèi),就可以使用其中的打開(kāi)函數(shù)了。
當(dāng)然,如果工程是對(duì)話框就更好辦了,直接把onopen函數(shù)寫(xiě)成這樣即可打開(kāi)多個(gè)文檔:
[cpp]?view plain
?copy void?CCFileDialogST_demoDlg::OnOpen()??? {?? ????CFileDialogST???dlg(TRUE,?NULL,?NULL,?OFN_OVERWRITEPROMPT?|?OFN_ALLOWMULTISELECT,?_T("All?files\0*.*\0"),?this);?? ????CString?????????sPathName;?? ????int?????????????nRetValue;?? ?? ????nRetValue?=?dlg.DoModal();?? ????if?(nRetValue?==?IDOK)?? ????{?? ????????POSITION????Pos;?? ?? ????????Pos?=?dlg.GetStartPosition();?? ????????while?(Pos?!=?NULL)?? ????????{?? ????????????sPathName?=?dlg.GetNextPathName(Pos);?? ????????????MessageBox(sPathName,?_T("GetNextPathName"),?MB_ICONINFORMATION);?? ????????}??? ????}??? }??? from:?http://blog.csdn.net/abcjennifer/article/details/7441354
總結(jié)
以上是生活随笔為你收集整理的MFC 多文档中同时打开多个文档的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。