当前位置: 首页 > 文档资料 > VC 经典教程 >

8.8 防止应用程序运行时自动创建空白窗口

优质
小牛编辑
127浏览
2023-12-01

在前面的MDI程序中,当应用程序启动时,都会自动创建一个空白窗口。但有时我们并不希望创建这样的空白窗口。比如,对于一个文件浏览器来说,空白窗口就没有什么意义。

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

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

BOOL CDrawApp::InitInstance()

{

// Enable DDE Execute open

EnableShellOpen();

RegisterShellFileTypes(TRUE);

// Parse command line for standard shell commands, DDE, file open

CCommandLineInfo cmdInfo;

// Alter behaviour to not open window immediately

 

cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;

ParseCommandLine(cmdInfo);

// Dispatch commands specified on the command line

if (!ProcessShellCommand(cmdInfo))

return FALSE;

//......

}