How to disable the automatic creation of a document / view when you first start the MFC application

I have a regular MFC application that uses the Doc / View architecture. When the application starts, it automatically creates a view of an empty document. I want to disable this automatic viewing at startup and show the view only when the user clicks "New Document" in the "File" menu.

Is there any way to do this?

CMultiDocTemplate* template = new CMultiDocTemplate(IDR_DorlionTYPE,
        RUNTIME_CLASS(CDocument),
        RUNTIME_CLASS(CChildFrame), // custom MDI child frame
        RUNTIME_CLASS(CView));
    if (!CView)
        return FALSE;
+1
source share
3 answers

MFC ( ) , , ( ); ProcessShellCommand(), "":

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

[ , MFC ParseCommandLine(), m_nShellCommand CCommandLineInfo::FileNew, ]

+2

    cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

if.

CCommandLineInfo:: m_nShellCommand

0

With my MFC application, I wanted something similar, but I found that the accepted answer was only a partial solution for me. If the file name is specified on the command line of the MFC application when it starts, using the accepted answer will not open the file.

I wanted (1) to allow the file to open when the MFC application was launched from the command line and (2) to change the current working folder.

In InitInstance()overriding the application that is being distributed CWinAppEx, I used the following source:

// determine the user 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();
0
source

All Articles