How to stop initial new document at startup in MFC doc/view app

Locked How to stop initial new document at startup in MFC doc/view app

  • martedì 25 ottobre 2011 13:56
     
     

    My MFC skill are rusty these days, so I'm hoping this is a simple question for someone out there.

    I just created a MFC doc/view Ribbon app using the VS 2010 wizzard. When I launch my app, it always gets a call to create a new document. I traced it back to ParseCommandLine(cmdInfo); in the app InitInstance method. The m_nShellCommand in the cmdInfo object has a value of FileNew. I'm assuming this is causing the initial new document on app launch.

    So how did this command get there in the first place and how do I turn it off? I need my app to start with no documents.

Tutte le risposte

  • martedì 25 ottobre 2011 14:28
     
     Con risposta Contiene codice
    	CCommandLineInfo cmdInfo;
    /* REMOVE THIS PART
    	ParseCommandLine(cmdInfo);
    
    	// 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;
    */
    /* ADD THIS PART
    */
    	cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
    	if (!ProcessShellCommand(cmdInfo))
    		return FALSE;
    
    	// create main frame window
    	CMainFrame* pMainFrame = new CMainFrame;
    	if (!pMainFrame || !pMainFrame->LoadFrame(IDR_MAINFRAME))
    	{
    		delete pMainFrame;
    		return FALSE;
    	}
    	m_pMainWnd = pMainFrame;
    
    


    For this to work you will have to make the CMainFrame constructor public.
    • Modificato Scott McPhillipsMVP martedì 25 ottobre 2011 14:29
    • Contrassegnato come risposta daaboots martedì 25 ottobre 2011 16:34
    •  
  • martedì 25 ottobre 2011 14:36
     
      Contiene codice

    Alternatively, you can replace:

    		if (!ProcessShellCommand(cmdInfo))
    			return FALSE;
    
    

    by

    	// Dispatch commands specified on the command line.  Will return FALSE if
    	// app was launched with /RegServer, /Register, /Unregserver or /Unregister.
    	if (cmdInfo.m_nShellCommand == CCommandLineInfo::FileNew)
    	{
    		// do nothing
    	}
    	else
    	{
    		if (!ProcessShellCommand(cmdInfo))
    			return FALSE;
    	}
    
    


     

  • martedì 25 ottobre 2011 16:34
     
     
    Thanks guys!
  • mercoledì 18 aprile 2012 13:41
     
     

    Hello !!

    If you read the documentation and code :

    The CCommandLineInfo cmdInfo is created by its default constructor; which sets its properties  (m_bShowSplash=TRUE) and (more important):(m_nShellCommand=NewFile). Then, it is used through CWinApp::ParseCommandLine and ProcessShellCommand.

    You could simply add the line, just after cmdInfo is built :

    cmdInfo.m_nShellCommand = cmdInfo.FileNothing;

    best regards,

    Arnaud.


    • Modificato ArnaudG mercoledì 18 aprile 2012 13:43
    • Modificato ArnaudG mercoledì 18 aprile 2012 13:43
    •