在文檔窗口創建的時候 ,它缺省總是會新建一個新文檔 , 那么怎么讓它不新建文檔呢?就這個問題 , 我對文檔視圖窗口應用程序啟動時的文檔創建機制 , 稍稍的淺淺挖了一下 , 做了一個詳細的分析 , 希望能夠對初學者有所幫助 .
在App文件的InitInstance()函數中, 有如下幾行代碼:
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if (!ProcessShellCommand(cmdInfo)) return FALSE;
這幾行代碼是程序啟動時創建新文檔的關鍵代碼 .
1: 我們首先來看看讓CCommandLineInfo類是個什么東西:( 部分源代碼 )
//in afxwin.h
class CCommandLineInfo : public CObject
{
public:
// Sets default values
CCommandLineInfo();
BOOL m_bShowSplash;
BOOL m_bRunEmbedded;
BOOL m_bRunAutomated;
enum { FileNew, FileOpen, FilePrint, FilePrintTo, FileDDE, AppRegister,
AppUnregister, FileNothing = -1 } m_nShellCommand;
// not valid for FileNew
CString m_strFileName;
. . .
~CCommandLineInfo();
. . .
};
這里要重點注意enum {FileNew, . . . , FileNothing = -1 }m_nShellCommand;
這里聯合類型定義的m_nShellCommand 就是外殼程序執行的命令類型 , 如果m_nShellCommand設置為FileNew ,那么程序就會創建新文檔 . 如果想在文檔開始時不創建新文檔 , 就必須將m_nShellCommand設置為FilleNothing .
下面我們再看看CCommandLineInfo的構造函數 .
//in appcore.cpp
CCommandLineInfo::CCommandLineInfo()
{
m_bShowSplash = TRUE;
m_bRunEmbedded = FALSE;
m_bRunAutomated = FALSE;
m_nShellCommand = FileNew;
}
這里很明白的看出 , 構造函數中 , 缺省將 m_nShellCommand設置為 FileNew .
2:再來看看ParseCommandLine(cmdInfo); 函數 .
void CWinApp::ParseCommandLine(CCommandLineInfo& rCmdInfo)
{
for (int i = 1; i < __argc; i++)
{
LPCTSTR pszParam = __targv[i];
BOOL bFlag = FALSE;
BOOL bLast = ((i + 1) == __argc);
if (pszParam[0] == '-' || pszParam[0] == '/')
{
// remove flag specifier
bFlag = TRUE;
++pszParam;
}
rCmdInfo.ParseParam(pszParam, bFlag, bLast);
}
}
可以看出ParseCommandLine主要是對輸入的命令行參數做一些分析 , 并調用ParseParam來進行處理 .繼續分析 ParseParam函數 , 查看如下源代碼:
void CCommandLineInfo::ParseParam(const TCHAR* pszParam,BOOL bFlag,BOOL bLast)
{
if (bFlag)
{
USES_CONVERSION;
ParseParamFlag(T2CA(pszParam));
}
else
ParseParamNotFlag(pszParam);
ParseLast(bLast);
}
其它的函數撇開不看 , 我們重點來分析一下ParseParamFlag()和ParseLast()函數 .
void CCommandLineInfo::ParseParamFlag(const char* pszParam)
{
// OLE command switches are case insensitive, while
// shell command switches are case sensitive
if (lstrcmpA(pszParam, "pt") == 0)
m_nShellCommand = FilePrintTo;
else if (lstrcmpA(pszParam, "p") == 0)
m_nShellCommand = FilePrint;
else if (lstrcmpiA(pszParam, "Unregister") == 0 ||
lstrcmpiA(pszParam, "Unregserver") == 0)
m_nShellCommand = AppUnregister;
else if (lstrcmpA(pszParam, "dde") == 0)
{
AfxOleSetUserCtrl(FALSE);
m_nShellCommand = FileDDE;
}
else if (lstrcmpiA(pszParam, "Embedding") == 0)
{
AfxOleSetUserCtrl(FALSE);
m_bRunEmbedded = TRUE;
m_bShowSplash = FALSE;
}
else if (lstrcmpiA(pszParam, "Automation") == 0)
{
AfxOleSetUserCtrl(FALSE);
m_bRunAutomated = TRUE;
m_bShowSplash = FALSE;
}
}
ParseParamFlag判斷傳過來的字符串 ,判斷它的參數類型 , 并根據參數類型做不同的處理 .
void CCommandLineInfo::ParseLast(BOOL bLast)
{
if (bLast)
{
if (m_nShellCommand == FileNew && !m_strFileName.IsEmpty())
m_nShellCommand = FileOpen;
m_bShowSplash = !m_bRunEmbedded && !m_bRunAutomated;
}
}
ParseLast會判斷是否是是FileNew打開新文檔 , 如果是打開新文檔 , 并且打開的文檔名不為空的話, 就假定用戶想打開這個文檔 , 把命令設置為FileOpen .
最后 , 我們可以總結一下ParseCommandLine的作用 . ParseCommandLine的作用主要是分析命令行參數,如果沒有命令行參數 ,ParseCommandLine()就假定用戶想新建一個文檔,于是設置一個FileNew命令,如果命令行參數中有一個文件名,ParseCommandLine()就假定用戶想打開該文件,于是設置一個FileOpen命令。
3: 最后 , 我們來重點看看外殼命令解析的主角 : ProcessShellCommand ();(部分源代碼)
BOOL CWinApp::ProcessShellCommand(CCommandLineInfo& rCmdInfo)
{
BOOL bResult = TRUE;
switch (rCmdInfo.m_nShellCommand)
{
case CCommandLineInfo::FileNew:
if (!AfxGetApp()->OnCmdMsg(ID_FILE_NEW, 0, NULL, NULL))
OnFileNew();
if (m_pMainWnd == NULL)
bResult = FALSE;
break;
case CCommandLineInfo::FileOpen: . . .
case CCommandLineInfo::FilePrintTo: . . .
case CCommandLineInfo::FilePrint: . . .
case CCommandLineInfo::FileDDE: . . .
case CCommandLineInfo::AppRegister: . . .
case CCommandLineInfo::AppUnregister: . . .
. . .
}
}
代碼看到這里 , 一切都很明白了 . ProcessShellCommand分析m_nShellCommand ,并根據m_nShellCommand不同的類型值進行不同的處理 .
再來分析下面兩行代碼:
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if (!ProcessShellCommand(cmdInfo)) return FALSE;
1: 當CCommandLineInfo cmdInfo進行定義時 , 首先調用構造函數 , 構造函數中m_nShellCommand被設置為FileNew
2: 然后執行ParseCommandLine(cmdInfo);對命令進行分析 .
3: 最后執行ProcessShellCommand (cmdInfo) , ProcessShellCommand ()判斷m_nShellCommand為FileNew , 于是調用OnFileNew()創建了一個新的文檔 .
這也就是創建新文檔的來龍去脈 .
最后, 我們看怎么樣解決不想在應用程序啟動時的創建新文檔的問題:
直接在InitInstance()函數中用如下代碼代替原來的幾行即可:
CCommandLineInfo cmdInfo;
cmdInfo.m_nShellCommand = CCommandLineInfo::FileNothing;
ParseCommandLine(cmdInfo);
if (!ProcessShellCommand(cmdInfo)) return FALSE;