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;
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]
I used
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
without the if-statement.
See CCommandLineInfo::m_nShellCommand
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();