在学习一MDI文档程序时,在消息映射表中发现菜单项中的“打开文件“事件映射到CWinApp::OnFileOpen事件,而不是我想象的自定义打开文件事件。
 ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)在源文件里也没有找到CWinApp::OnFileOpen的返回值。
百度后发现CWinApp::OnFileOpen原型在appdlg.cpp中,于是设好断点,程序也顺利的跟踪到 ENSURE(m_pDocManager != NULL);
m_pDocManager->OnFileOpen();
逐过程跟踪,来到cmdtarg.cpp中的 AFX_STATIC BOOL AFXAPI _AfxDispatchCmdMsg函数中,应该是消息分发把,跟踪到(pTarget->*mmf.pfnCmd_v_v)(); break;
不知道这个是什么玩意,百度后就知道是指向什么函数指针,没搞明白,之后就是一些消息路由之类的,在跟下去就是会到 “未加载的wuser32,pdb界面“,最后还是不知道获取的文件名是如何返回的,奇怪的是程序并没有中断,点几下“跳出”跟踪按钮程序能正常执行,可以看到子窗口已经加载了指定的文件了。
其实中间的过程我只想搞个大致明白就行了,问题是CWinApp::OnFileOpen的返回值什么时候得到的,如何处理的?

解决方案 »

  1.   

    添加自定义响应函数 OnMyFileOpenON_COMMAND(ID_FILE_OPEN, CMyWinApp::OnMyFileOpen)
      

  2.   

    我知道可以添加自定义过程,我是想问CWinApp::OnFileOpen的返回值什么时候得到的,如何处理的?
      

  3.   

    CWinApp::OnFileOpen has a very simple implementation of calling CWinApp::DoPromptFileName followed by CWinApp::OpenDocumentFile with the file or path name of the file to open. The CWinApp implementation routine DoPromptFileName brings up the standard FileOpen dialog and fills it with the file extensions obtained from the current document templates.
      

  4.   

    pTarget->*mmf.pfnCmd_v_v)()这个嘛,就是调用函数指针,这个指针指向OnFileOpen,
    ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen) 本质就是将ID_FILE_OPEN 这个命令和CWinApp::OnFileOpen关联起来
      

  5.   

    通过 class CRecentFileList
    可以 得到 成功打开过 的 文件名
      

  6.   


    theApp.m_pRecentFileList->m_arrNames[j]
      

  7.   

    想得到打开的文件名, 重载 App类的 OpenDocumentFile, 传进来的参数就是 打开的文件名CDocument* CMyApp::OpenDocumentFile(LPCTSTR lpszFileName) 
    {
    // TODO: Add your specialized code here and/or call the base class

    return CWinApp::OpenDocumentFile(lpszFileName);
    }
      

  8.   

    7楼正解,重载 App类的 OpenDocumentFile