单文档程序重复打开一个文件无反应

vincen_cn 2015-12-30 04:23:03
mfc中重复打开一个文件时候,第二次没有反应,opendocument也不走,如何才能每次都打开。
...全文
145 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
schlafenhamster 2015-12-31
  • 打赏
  • 举报
回复
注意 STATUS This behavior is by design.
schlafenhamster 2015-12-31
  • 打赏
  • 举报
回复
PRB: MFC Does Not Reopen an Open Document Last reviewed: July 10, 1997 Article ID: Q139828 1.00 1.50 1.51 1.52 | 1.00 2.00 2.10 2.20 4.00 WINDOWS | WINDOWS NT kbprg kbprb The information in this article applies to: The Microsoft Foundation Classes (MFC) included with: - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1, 2.2, 4.0 SYMPTOMS In a default MFC AppWizard application, the framework will not reopen a document file from disk that is currently open in the application. RESOLUTION This is by design. In a typical MFC application, the Open File command is mapped to the CWinApp::OnFileOpen() function. In earlier versions of MFC, this function in turn called the CWinApp::OpenDocumentFile() function. Since MFC 4.0, there is now an intervening CDocManager class, but the call to OnFileOpen() still eventually results in a call to CWinApp::OpenDocumentFile(). CWinApp::OpenDocumentFile() first processes the string holding the requested file name. Then it searches through the list of document templates that were added for the application by calls to AddDocTemplate in order to find the best match between the name of the file and a document template to open it with. At this point, if OpenDocumentFile() finds that this file is already currently opened for one of the templates, OpenDocumentFile() activates the view for that file and then returns. It does not re-open the file. If that document file is not currently open and OpenDocumentFile() has found a valid template to open the file with, it calls that template's OpenDocumentFile() function. This function is responsible for opening the file and loading its data into an appropriate document. In some situations, you may want to reopen an open document. For example, Notepad does this. On a file open request, Notepad first displays a prompt dialog to allow the user to save a modified file. If the user does not click cancel on this dialog box, Notepad then brings up the File Open dialog box. If the user chooses to reopen the current file, Notepad rereads it from disk and discards any unsaved changes. To duplicate this behavior in an MFC program, the programmer needs to override the OpenDocumentFile() member function of CWinApp. Or, if the appropriate template for the file is easy to determine (such as when the application has only one kind of doc template), it would be possible to call the template's OpenDocumentFile() directly from an override of CWinApp::OnFileOpen(). This is demonstrated in the "Sample Code" section of this article. Note that MFC will display the Save Modified prompt dialog after the Open File dialog box; this is counter to the behavior of Notepad. STATUS This behavior is by design. Sample Code /* Compile options needed: none */ void CWinApp::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 // Process newName string if necessary // Get pTemplate, a pointer to one of the app's document templates pTemplate->OpenDocumentFile(newName); } // end of CWinApp::OnFileOpen()
meryal 2015-12-31
  • 打赏
  • 举报
回复
这是MFC设计的就是这样,如果非要实现,你可以自己响应消息
vincen_cn 2015-12-30
  • 打赏
  • 举报
回复
引用 3 楼 schlafenhamster的回复:
" MFC不会再打开 已经打开的文件" http://blog.csdn.net/schlafenhamster/article/details/26065979
不是这么做的,要重载onopendocument然后在其中得到doc调用opendocument
schlafenhamster 2015-12-30
  • 打赏
  • 举报
回复
" MFC不会再打开 已经打开的文件" http://blog.csdn.net/schlafenhamster/article/details/26065979
vincen_cn 2015-12-30
  • 打赏
  • 举报
回复
你这个是什么意思,我这里也看不到源码啊。
Eleven 2015-12-30
  • 打赏
  • 举报
回复

CDocument* CDocManager::OpenDocumentFile(LPCTSTR lpszFileName, BOOL bAddToMRU)
{
	if (lpszFileName == NULL)
	{
		AfxThrowInvalidArgException();
	}
	// find the highest confidence
	POSITION pos = m_templateList.GetHeadPosition();
	CDocTemplate::Confidence bestMatch = CDocTemplate::noAttempt;
	CDocTemplate* pBestTemplate = NULL;
	CDocument* pOpenDocument = NULL;

	TCHAR szPath[_MAX_PATH];
	ASSERT(AtlStrLen(lpszFileName) < _countof(szPath));
	TCHAR szTemp[_MAX_PATH];
	if (lpszFileName[0] == '\"')
		++lpszFileName;
	Checked::tcsncpy_s(szTemp, _countof(szTemp), lpszFileName, _TRUNCATE);
	LPTSTR lpszLast = _tcsrchr(szTemp, '\"');
	if (lpszLast != NULL)
		*lpszLast = 0;
	
	if( AfxFullPath(szPath, szTemp) == FALSE )
	{
		ASSERT(FALSE);
		return NULL; // We won't open the file. MFC requires paths with
		             // length < _MAX_PATH
	}

	TCHAR szLinkName[_MAX_PATH];
	if (AfxResolveShortcut(AfxGetMainWnd(), szPath, szLinkName, _MAX_PATH))
		Checked::tcscpy_s(szPath, _countof(szPath), szLinkName);

	while (pos != NULL)
	{
		CDocTemplate* pTemplate = (CDocTemplate*)m_templateList.GetNext(pos);
		ASSERT_KINDOF(CDocTemplate, pTemplate);

		CDocTemplate::Confidence match;
		ASSERT(pOpenDocument == NULL);
		match = pTemplate->MatchDocType(szPath, pOpenDocument);
		if (match > bestMatch)
		{
			bestMatch = match;
			pBestTemplate = pTemplate;
		}
		if (match == CDocTemplate::yesAlreadyOpen) // Note: This code ...
			break;      // stop here
	}

	if (pOpenDocument != NULL)
	{
		POSITION posOpenDoc = pOpenDocument->GetFirstViewPosition();
		if (posOpenDoc != NULL)
		{
			CView* pView = pOpenDocument->GetNextView(posOpenDoc); // get first one
			ASSERT_VALID(pView);
			CFrameWnd* pFrame = pView->GetParentFrame();

			if (pFrame == NULL)
				TRACE(traceAppMsg, 0, "Error: Can not find a frame for document to activate.\n");
			else
			{
				pFrame->ActivateFrame();

				if (pFrame->GetParent() != NULL)
				{
					CFrameWnd* pAppFrame;
					if (pFrame != (pAppFrame = (CFrameWnd*)AfxGetApp()->m_pMainWnd))
					{
						ASSERT_KINDOF(CFrameWnd, pAppFrame);
						pAppFrame->ActivateFrame();
					}
				}
			}
		}
		else
			TRACE(traceAppMsg, 0, "Error: Can not find a view for document to activate.\n");

		return pOpenDocument;
	}

	if (pBestTemplate == NULL)
	{
		AfxMessageBox(AFX_IDP_FAILED_TO_OPEN_DOC);
		return NULL;
	}

	return pBestTemplate->OpenDocumentFile(szPath, bAddToMRU, TRUE);
}

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧