How to disable auto document/view creation when MFC application first starts

扶醉桌前 提交于 2020-01-24 23:03:59

问题


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

Is there any way to do so?

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

回答1:


Standard MFC (wizard generated) code assumes that you would always want to see a new document if the program is just run by itself (as opposed to double-clicking on the data file or running it with a command-line option to open the file); insert the following lines before the call to ProcessShellCommand() to disable this "feature":

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

[if you are interested, you can step through the MFC source code for ParseCommandLine() where it sets m_nShellCommand to CCommandLineInfo::FileNew if there's nothing in the command line]




回答2:


I used

    cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

without the if-statement.

See CCommandLineInfo::m_nShellCommand




回答3:


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

I wanted to (1) allow a file to be opened when the MFC application is invoked from a command line and (2) change the current working folder.

In the InitInstance() override of the application which extends CWinAppEx I used the following source:

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


来源:https://stackoverflow.com/questions/24609936/how-to-disable-auto-document-view-creation-when-mfc-application-first-starts

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!