全局变量的定义:
在MFC的框架中有个全局变量:
CTestApp theApp;
theApp是在进入WinMain函数之前就构造好值的。
所以可以进入WinMain函数之前是先执行theApp的构造函数。
孙鑫老师说MFC是通过应用程序类的对象来标识应用程序的实例的(这句话不明白,应该都是hInstance标识的吧,也许要等到我看侯捷老师书的时候才知道了),且每个MFC程序有且仅有一个从CWinApp派生的类。也仅有一个CWinApp派生类的对象,即仅有一个theApp对象。theApp对象就标识了应用程序本身。
即theApp是统领其它类的。
并且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中还提供了一些额外的操作全局信息的函数:
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()函数:
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);
}