全局變量的定義:
在MFC的框架中有個(gè)全局變量:
CTestApp theApp;
theApp是在進(jìn)入WinMain函數(shù)之前就構(gòu)造好值的。
所以可以進(jìn)入WinMain函數(shù)之前是先執(zhí)行theApp的構(gòu)造函數(shù)。
孫鑫老師說MFC是通過應(yīng)用程序類的對(duì)象來標(biāo)識(shí)應(yīng)用程序的實(shí)例的(這句話不明白,應(yīng)該都是hInstance標(biāo)識(shí)的吧,也許要等到我看侯捷老師書的時(shí)候才知道了),且每個(gè)MFC程序有且僅有一個(gè)從CWinApp派生的類。也僅有一個(gè)CWinApp派生類的對(duì)象,即僅有一個(gè)theApp對(duì)象。theApp對(duì)象就標(biāo)識(shí)了應(yīng)用程序本身。
即theApp是統(tǒng)領(lǐng)其它類的。
并且CWinApp類是派生于CWinThread類的,
There are two general types of threads that CWinThread supports: worker threads and user-interface threads. Worker threads have no message pump: for example, a thread that performs background calculations in a spreadsheet application. User-interface threads have a message pump and process messages received from the system. CWinApp and classes derived from it are examples of user-interface threads. Other user-interface threads can also be derived directly from CWinThread.
MSDN中還提供了一些額外的操作全局信息的函數(shù):
AfxGetApp Obtains a pointer to the CWinApp object.
AfxGetInstanceHandle Obtains a handle to the current application instance.
AfxGetResourceHandle Obtains a handle to the application's resources.
AfxGetAppName Obtains a pointer to a string containing the application's name. Alternately, if you have a pointer to the CWinApp object, use m_pszExeName to get the application's name.
看看MFC中的 main()函數(shù):
extern "C" int WINAPI
_tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine, int nCmdShow)
#pragma warning(suppress: 4985)
{
// call shared/exported WinMain
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}