【问题标题】:How to disable auto document/view creation when MFC application first starts如何在 MFC 应用程序首次启动时禁用自动文档/视图创建
【发布时间】:2014-07-07 11:51:08
【问题描述】:

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

有什么办法吗?

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

【问题讨论】:

    标签: c++ visual-c++ mfc


    【解决方案1】:

    标准 MFC(由向导生成)代码假定如果程序只是自行运行(而不是双击数据文件或使用命令行选项运行它以打开文件);在调用 ProcessShellCommand() 之前插入以下行以禁用此“功能”:

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

    [如果您有兴趣,可以单步调试ParseCommandLine() 的MFC 源代码,如果命令行中没有任何内容,它将m_nShellCommand 设置为CCommandLineInfo::FileNew]

    【讨论】:

      【解决方案2】:

      我用过

          cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
      

      没有 if 语句。

      CCommandLineInfo::m_nShellCommand

      【讨论】:

        【解决方案3】:

        对于我的 MFC 应用程序,我想要类似的东西,但是我发现接受的答案对我来说只是部分解决方案。如果 MFC 应用程序启动时在命令行中指定了文件名,则使用接受的答案将无法打开该文件。

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

        在扩展 CWinAppEx 的应用程序的 InitInstance() 覆盖中,我使用了以下源:

        // 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();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多