对于Dialog, 是没有ProcessShellCommand的, 可以自己分析完, 存起来要用的时候再用.
对于sdi, mdi, ProcessShellCommand是要打开文档的.
CMyCommandLineInfo cmdInfo;// 自定义了命令分析类
MyParseCommandLine(cmdInfo);//自定义分析函数
// Dispatch commands specified on the command line
if (!MyProcessShellCommand(cmdInfo)) //这里怎么对应OpenNewDocument?
return FALSE;
//看见过一个sdi 的Demo, 自定义了命令行分析, 没注意. 那时我的应用是Dialog.
现在我直接按照标准的内容填写CCommandLineInfo cmdInfo,
ProcessShellCommand(cmdInfo)返回FALSE. 实际上要打开的文件名已经传过去了.
如果不判断ProcessShellCommand(cmdInfo)的返回值, 程序是可以正常打开文件.
问题找到, 打开的文件必须存在.
BOOL CMyProcessShellCommandApp::InitInstance() { AfxEnableControlContainer(); // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. #ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); LoadStdProfileSettings(); // Load standard INI file options (including MRU) // Register the application's document templates. Document templates // serve as the connection between documents, frame windows and views. CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CMyProcessShellCommandDoc), RUNTIME_CLASS(CMainFrame), // main SDI frame window RUNTIME_CLASS(CMyProcessShellCommandView)); AddDocTemplate(pDocTemplate); // Parse command line for standard shell commands, DDE, file open // CCommandLineInfo cmdInfo; // ParseCommandLine(cmdInfo); // // // Dispatch commands specified on the command line // if (!ProcessShellCommand(cmdInfo)) // return FALSE; /** * 可以在这用自定义的参数接收分析, 把参数都保存好 * 然后自己根据自定义参数构造sdi, mdi需要的操作的文件名 * 多出来的参数用到的时候,再使用 * 标准参数如下, 最常用的就是 CCommandLineInfo::FileOpen * enum { FileNew, FileOpen, FilePrint, FilePrintTo, FileDDE, * AppUnregister, FileNothing = -1 } m_nShellCommand; */ CCommandLineInfo MycmdInfo; MycmdInfo.m_bShowSplash = TRUE; MycmdInfo.m_bRunEmbedded = FALSE; MycmdInfo.m_bRunAutomated = FALSE; MycmdInfo.m_nShellCommand = CCommandLineInfo::FileOpen; //MycmdInfo.m_strFileName = "d://tmp//测试.txt"; /** 这个文件存在 */ /** * @note * 如果要打开的文件在物理上不存在, 报错信息如下 * "没有找到 d:/tmp/测试.doc" * 那我遇到的也是这个问题, 那这是正常的 */ MycmdInfo.m_strFileName = "d://tmp//测试.doc";/** 这个文件不存在 */ MycmdInfo.m_strPrinterName.Empty(); MycmdInfo.m_strDriverName.Empty(); MycmdInfo.m_strPortName.Empty(); /** * @note * CMyProcessShellCommandDoc::OnOpenDocument(const char * 0x0012fcdc) * CSingleDocTemplate::OpenDocumentFile(const char * 0x0012fcdc, int 0x00000001) * CDocManager::OpenDocumentFile(const char * 0x00382e6c) * CWinApp::OpenDocumentFile(const char * 0x00382e6c) * CWinApp::ProcessShellCommand(CCommandLineInfo & {CCommandLineInfo}) * 不存在的文件就会返回FALSE; */ if (!ProcessShellCommand(MycmdInfo)) return FALSE; // The one and only window has been initialized, so show and update it. m_pMainWnd->ShowWindow(SW_SHOW); m_pMainWnd->UpdateWindow(); return TRUE; }