[appmodul.cpp]
extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}
[winmain.cpp]
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow)
{
int nReturnCode = -1;
CWinThread* pThread = AfxGetThread();
CWinApp* pApp = AfxGetApp();
pApp->InitApplication();
if (!pThread->InitInstance())
{
return nReturnCode;
}
nReturnCode = pApp->Run();
return nReturnCode;
}
CWinApp::InitApplication
CWinThread::InitInstance
CWinThread::Run
均是virtual函数
[thrdcore.cpp]
int CWinThread::Run()
{
for (;;)
{
if (!PumpMessage()) //遇到WM_QUIT消息则退出
return ExitInstance();
}
}
[thrdcore.cpp]
BOOL CWinThread::PumpMessage()
{
return AfxInternalPumpMessage();
}
[thrdcore.cpp]
BOOL AFXAPI AfxInternalPumpMessage()
{
MSG msg;
::GetMessage(&msg);
if (!AfxPreTranslateMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return TRUE;
}
[thrdcore.cpp]
BOOL AfxPreTranslateMessage(MSG* pMsg)
{
CWinThread *pThread = AfxGetThread();
return pThread->PreTranslateMessage(pMsg);
}
[thrdcore.cpp]
BOOL CWinThread::PreTranslateMessage(MSG* pMsg)
{
return AfxInternalPreTranslateMessage(pMsg);
}
[thrdcore.cpp]
BOOL AfxInternalPreTranslateMessage(MSG* pMsg)
{
CWnd* pMainWnd = AfxGetMainWnd();
if (CWnd::WalkPreTranslateTree(pMainWnd->GetSafeHwnd(), pMsg))
return TRUE;
return FALSE;
}
[wincore.cpp]
//
// 返回TRUE or FALSE
// 由以上流程得:该函数返回TRUE,则pMsg指向的消息将会被忽略而不处理
// 返回FALSE,则会DispatchMessage到对应的窗口过程函数处理
//
BOOL PASCAL CWnd::WalkPreTranslateTree(HWND hWndStop, MSG* pMsg)
{
//
// 先调用本窗口对应CWnd类的PreTranslateMessage
// 再往父窗口层层上溯调用对应CWnd类的PreTranslateMessage直到上溯到hWndStop对应的窗口
// 若其中的某个PreTranslateMessage返回TRUE, 则函数退出返回TRUE
// 否则上溯完成返回FALSE
//
for (HWND hWnd = pMsg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
{
CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
if (pWnd != NULL)
{
if (pWnd->PreTranslateMessage(pMsg))
return TRUE;
}
if (hWnd == hWndStop)
break;
}
return FALSE;
}