MFC将大部分函数都进行了封装,程序员只要改写部分重要的virtual函数即可,这往往使初学者摸不着头脑,连个WinMain函数都看不到,程序从哪开始从哪结束?基本的条理搞不清,永远也不会有提高。下面简单讲下基运行过程.
1,CMyWinApp theApp 程序从这里开始
2,_tWinMain() 在APPMODUL.CPP 它实际上只调用AfxWinMain函数
3,AfxWinMain() WINAMIN.CPP,去掉一些次要信息,它作的事就是:
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
int nReturnCode = -1;
CWinThread* pThread = AfxGetThread();
CWinApp* pApp = AfxGetApp(); ->实际上就是取得CMyWinApp对象指针
AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
pApp->InitApplication()
pThread->InitInstance()
pThread->Run();
AfxWinTerm();
return nReturnCode;
}
其中:
AfxGetApp(); AFXWIN.CPP中
InitApplication() AFXWIN.CPP中
InitInstance() AFXWIN.CPP中
实际上AfxWinMainy就是调用:
CWinApp::InitApplication 因为我们程序没有改写它
CMyWinApp::InitInstance 我们改写了它且必须改写它,为什么?看源码就能证明一切
BOOL CWinApp::InitInstance() APPCORE.CPP中
{
return TRUE;
}
看到了吧,它什么也没干,直接return TRUE;
CWinApp::Run();
4:AfxWinInit() AFX内部初始化操作,APPINIT.CPP中,贴下源码,如下:
BOOL AFXAPI AfxWinInit(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
..,去掉部分
pModuleState->m_hCurrentInstanceHandle = hInstance;
pModuleState->m_hCurrentResourceHandle = hInstance;
CWinApp* pApp = AfxGetApp();
if (pApp != NULL)
{
// Windows specific initialization (not done if no CWinApp)
pApp->m_hInstance = hInstance;
pApp->m_hPrevInstance = hPrevInstance;
pApp->m_lpCmdLine = lpCmdLine;
pApp->m_nCmdShow = nCmdShow;
pApp->SetCurrentHandles();
}
if (!afxContextIsDLL)
AfxInitThread();
return TRUE;
}
可以看到。它主要进行一些初始化工作.其中:
AfxInitThread() 在THRDCORE.CPP中