MfC打开过程详解及应用

本文详细解析了MFC中File->Open命令的响应流程,从CWinApp::OnFileOpen()开始,经过CDocManager::OnFileOpen()、CDocManager::OpenDocumentFile()等函数,直到CDocument::OnOpenDocument(),并介绍了如何根据需求重载OnFileOpen()函数以打开特定类型的文件。
摘要由CSDN通过智能技术生成

本文主要介绍:在MFC中,菜单打开命令的响应过程。

一、MFC打开命令的响应过程:

File->Open 对应的ID为ID_FILE_OPEN,其响应过程如下:

注:如果自己已将ID_FLIE_OPEN在MFC中重载了,则会直接响应重载函数,不会按以下过程响应。

1.点击File->Open,首先响应的函数为: CWinApp::OnFileOpen(),其函数原型为:

void CWinApp::OnFileOpen()
{
	ASSERT(m_pDocManager != NULL);
        m_pDocManager->OnFileOpen();
}

2.由上面的程序可知,接着调用的是: CDocManager::onFileOpen(),该函数功能是:显示打开文件的对话框,并获取文件的路径,其函数原型为:

void CDocManager::OnFileOpen()
{
       // prompt the user (with all document templates)
	CString newName;  //弹出打开文件的对话框,获取文件路径
    if (!DoPromptFileName(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
}

3.接着调用函数:  CWinApp::OpenDocumentFile(LPCTSTR lpszFileName),其函数原型为:
CDocument* CWinApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
    ASSERT(m_pDocManager != NULL);
    return m_pDocManager->OpenDocumentFile(lpszFileName);
}

4.再调用函数: CDocManager::OpenDocumentFile(LPCTSTR lpszFileName),该函数遍历文档模板,对每个文档进行匹配,若该文件已经在某个文档中打开,则会激活该文档视图,否则用匹配的文档模板,调用下一个打开函数,其原型为:

CDocument* CDocManager::OpenDocumentFile(LPCTSTR lpszFileName)
{
       // find the highest confidence
    POSITION pos = m_templateList.GetHeadPosition();
    CDocTemplate::Confidence bestMatch = CDocTemplate::noAttempt;
    CDocTemplate* pBestTemplate = NULL;
    CDocument* pOpenDocument = NULL;
       lstrcpyn(szTemp, lpszFileName, _MAX_PATH);
       LPTSTR lpszLast = _tcsrchr(szTemp, '"');

       if (lpszLast != NULL)
              *lpszLast = 0;
			  
       AfxFullPath(szPath, szTemp);
       TCHAR szLinkName[_MAX_PATH];
	   
       if (AfxResolveShortcut(AfxGetMainWnd(), szPath, szLinkName, _MAX_PATH))
              lstrcpy(szPath, szLinkName);

       while (pos != NULL)
       {
              CDocTemplate* pTemplate = (CDocTempla
MFC打开文件和文件夹 一、利用API实现打开文件对话框和利用MFC来实现打开文件对话框。 方法一:API部分: // OPENFILENAME ofn; // TCHAR szFile[MAX_PATH]; // ZeroMemory(&ofn,sizeof(ofn)); // ofn.lStructSize = sizeof(ofn); // ofn.lpstrFile = szFile; // ofn.lpstrFile[0] = TEXT('/0'); // ofn.nMaxFile = sizeof(szFile); // ofn.lpstrFilter = TEXT("all/0*.*/0jpg/0*.jpg/0bmp/0*.bmp/0"); //定义三个选项,all,text和exe // ofn.nFilterIndex = 1; //定义首选项是哪一个 // ofn.lpstrFileTitle = NULL; // ofn.nMaxFileTitle = 0; // ofn.lpstrInitialDir = NULL; // ofn.Flags = OFN_EXPLORER |OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; // if(GetOpenFileName(&ofn)) // { // ::SetDlgItemText(this->m_hWnd,IDC_EDIT1,szFile); // } 方法二、MFC实现 // CFileDialog dlg(TRUE, NULL, NULL, // OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, // NULL); // // if(dlg.DoModal() == IDOK) // { // szFilename = dlg.GetPathName(); // ::MessageBox(NULL,szFilename,NULL,MB_OK); // } 注意:打开文件的方式和打开文件夹的方式是不同的。打开文件的方式是不断的打开直到出现末端为文件,否则遇到文件夹还要继续打开。而打开文件夹则是只要选择到一个路径的文件夹就打开。 下面的这种方式是打开文件夹:MFC实现。 static TCHAR strDirName[MAX_PATH]; BROWSEINFO bi; CString szString = TEXT("选择一个源文件子文件夹"); bi.hwndOwner = ::GetFocus(); bi.pidlRoot = NULL; bi.pszDisplayName = strDirName; bi.lpszTitle = szString; bi.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS; bi.lpfn = NULL; bi.lParam = 0; bi.iImage = 0; LPITEMIDLIST pItemIDList = ::SHBrowseForFolder(&bi); if(pItemIDList == NULL) { return ; } ::SHGetPathFromIDList(pItemIDList, strDirName); CString str = strDirName; if(str != "" && str.Right(1) != '//') str += '//'; ::SetDlgItemText(this->m_hWnd,IDC_EDIT1,str);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值