蝴蝶效应之穿越甲午:多文档入门——程序启动时显示窗口个数

来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 18:14:06
在MDI程序中,当应用程序启动时,都会自动创建一个空白窗口。但有时我们并不希望创建这样的空白窗口。比如,对于一个文件浏览器来说,空白窗口就没有什么意义。

要防止空白窗口的创建,首先就要明白这个窗口是如何被创建的。在 InitInstance()中,有一个命令行的执行过程,当命令行上没有参数时,函数ParseCommandLine(cmdInfo)会将 CCommandLineInfo::m_nShellCommand成员置为CCommandLineInfo::FileNew,这将导致 ProcessShellCommand调用CWinApp::OnFileNew成员函数。要想防止程序开始时就调用OnFileNew,解决方法之一是去掉与命令行有关的代码,但是这样就没有了命令行处理功能。另一种方法是在ProcessShellCommand调用之前加一句 cmdInfo.m_nShellCommand =CCommandLineInfo::FileNothing。具体代码见清单8.12。

清单8.12 不自动创建空白文档窗口的InitInstance成员函数定义

C/C++ code
BOOL CDrawApp::InitInstance(){// Enable DDE Execute openEnableShellOpen();RegisterShellFileTypes(TRUE);// Parse command line for standard shell commands, DDE, file openCCommandLineInfo cmdInfo;// Alter behaviour to not open window immediatelycmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;ParseCommandLine(cmdInfo);// Dispatch commands specified on the command lineif (!ProcessShellCommand(cmdInfo))return FALSE;//......}
 pMainFrame->ShowWindow(m_nCmdShow);
 pMainFrame->UpdateWindow();
//pMainFrame->SendMessage(WM_COMMAND, ID_FILE_NEW, 0);//使得程序刚启动时就打开两个空白窗口
 return TRUE;