2014-07-07 25 views
1

我有一个使用Doc/View体系结构的常规MFC应用程序。当应用程序启动时,它会自动创建一个空文档的视图。我想在启动时禁用此自动视图,并仅在用户从“文件”菜单中单击“新建文档”时才显示视图。如何在MFC应用程序第一次启动时禁用自动文档/视图创建

有没有办法做到这一点?

CMultiDocTemplate* template = new CMultiDocTemplate(IDR_DorlionTYPE, 
     RUNTIME_CLASS(CDocument), 
     RUNTIME_CLASS(CChildFrame), // custom MDI child frame 
     RUNTIME_CLASS(CView)); 
    if (!CView) 
     return FALSE; 

回答

2

标准MFC(生成向导)代码假定你总是希望看到一个新的文档,如果这个计划只是在自己独立运行(而不是双击的数据文件或命令运行它在线选项打开文件);调用ProcessShellCommand()之前插入以下行来禁用这个“功能”:

if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew) // actually none 
    cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing; 

[如果你有兴趣,你可以通过为ParseCommandLine() MFC源代码在那里设置m_nShellCommandCCommandLineInfo::FileNew步骤,如果有什么是在命令行]

0

随着我的MFC应用程序,我想类似的东西,但是我发现,接受的答案是只为我的部分解决方案。如果在启动MFC应用程序的命令行时指定了文件名,则使用接受的答案将不会打开该文件。

我想(1)当从命令行调用MFC应用程序时允许打开文件并(2)更改当前工作文件夹。

InitInstance()倍率延伸CWinAppEx应用程序的我用下面的源:

// determine the user's home folder for documents such as C:\user\xxx\Documents 
if (SUCCEEDED(SHGetFolderPath(NULL, CSIDL_MYDOCUMENTS, NULL, 0, CMFCApplication4Doc::m_UserDocumentsFolder))) { 
    PathAppend(CMFCApplication4Doc::m_UserDocumentsFolder, L"GenPOS BO"); 
    TRACE1("home path found %s\n", CMFCApplication4Doc::m_UserDocumentsFolder); 
    if (!CreateDirectory(CMFCApplication4Doc::m_UserDocumentsFolder, NULL)) { 
     DWORD dwLastError = GetLastError(); 
     if (dwLastError != ERROR_ALREADY_EXISTS) { 
      // may be ERROR_PATH_NOT_FOUND indicating intermediate directories do not exist. 
      // CreateDirectory() will only create the final folder in the path so intermediate folders 
      // must already exist. 
      TRACE1("CreateDirectory error %d\n", dwLastError); 
     } 
    } 
    SetCurrentDirectory(CMFCApplication4Doc::m_UserDocumentsFolder); 
} 
else { 
    TRACE0("home path not found"); 
} 

// Parse command line for standard shell commands, DDE, file open 
CCommandLineInfo cmdInfo; 
ParseCommandLine(cmdInfo); 
if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew) // actually none 
    cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing; 

// Dispatch commands specified on the command line. Will return FALSE if 
// app was launched with /RegServer, /Register, /Unregserver or /Unregister. 
if (!ProcessShellCommand(cmdInfo)) 
    return FALSE; 
// The main window has been initialized, so show and update it 
pMainFrame->ShowWindow(m_nCmdShow); 
pMainFrame->UpdateWindow(); 
相关问题