采用多文档的默认Project,不改动代码,直接编译运行
会带有一个默认的子文档,
如何去掉,仅在“新建”和“打开”菜单后才出现子文档呢?
CxxxMDIApp类的InitInstance()在创建CMainFrame对象后调用CWinApp的ParseCommandLine()方法解析命令行参数,然后用ProcessShellCommand()方法处理命令行。对于空命令行,ParseCommandLine()方法将会生成一个FileNew命令,让ProcessShellCommand()方法调用CxxxMDIDoc的OnNewDocument()方法创建一个空文档。
为了避免创建空文档,一种可行的方法是修改InitInstance()来实现(字体加速的行):
BOOL CxxxMDIApp::InitInstance()
{
...
if(cmdInfo.m_nShellCommand != CCommandLineInfo::FileNew)
{
if (!ProcessShellCommand(cmdInfo))
return FALSE;
}
...
}
下面是函数,我屏蔽了三句,也实现了没有初始文档
你看看,会不会有什么“后遗症”?
BOOL CExMultiApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application’s document templates. Document templates
// serve as the connection between documents, frame windows and views.
CMultiDocTemplate* pDocTemplate;
pDocTemplate = new CMultiDocTemplate(
IDR_EXMULTTYPE,
RUNTIME_CLASS(CExMultiDoc),
RUNTIME_CLASS(CChildFrame), // custom MDI child frame
RUNTIME_CLASS(CExMultiView));
AddDocTemplate(pDocTemplate);
// create main MDI Frame window
CMainFrame* pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
return FALSE;
m_pMainWnd = pMainFrame;
/********************************************************************
Writed by Nicholas.K.J
created: 2006/02/13
created: 13:2:2006 21:36
*********************************************************************/
// // Parse command line for standard shell commands, DDE, file open
// CCommandLineInfo cmdInfo;
// ParseCommandLine(cmdInfo);
//
// // Dispatch commands specified on the command line
// if (!ProcessShellCommand(cmdInfo))
// return FALSE;
//以上是我屏蔽屏蔽的地方,也是没有了初始文档
// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
return TRUE;
屏蔽是可以的,但此时程序不再处理命令行参数。当希望处理命令行参数的时候就需要自行编写代码处理。
还可以这样修改(加粗的部分):
CCommandLineInfo cmdInfo;
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
ParseCommandLine(cmdInfo);
由于对于空命令行,ParseCommandLine()不会修改cmdInfo,有效地阻止了在启动时新建文档。
当然还可以结合注册表来实现,例如在InitInstance()调用SetRegistryKey(_T("xxx"));之后,通过调用GetProfileInt()来读取注册表中的值(例如一个名为"NewDocOnStart"的DWORD值),如:
SetRegistryKey(_T("CompanyName"));
INT nNewDocOnStart = 0;
nNewDocOnStart = GetProfileInt(_T("ApplicationName"), _T("NewDocOnStart"), nNewDocOnStart);
然后在下面修改:
CCommandLineInfo cmdInfo;
cmdInfo.m_nShellCommand = nNewDocOnStart ?CCommandLineInfo::FileNew : CCommandLineInfo::FileNothing;
ParseCommandLine(cmdInfo);