按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
达pApp …》Run 。
稍早我说过了,pApp 指向CMyWinApp 对象(也就是本例的theApp ),所以,当程序
调用:
pApp…》Run();
相当于调用:
CMyWinApp::Run();
390
…………………………………………………………Page 453……………………………………………………………
第6章 MFC 程式的生死因果
要知道,CMyWinApp 继承自CWinApp,而Run 又是CWinApp 的一个虚拟函数。我们
并没有改写它(大部份情况下不需改写它),所以上述动作相当于调用:
CWinApp::Run();
其源代码出现在APPCORE。CPP 中:
int CWinApp::Run()
{
if (m_pMainWnd == NULL && AfxOleGetUserCtrl())
{
// Not launched /Embedding or /Automation; but has no main window!
TRACE0(〃Warning: m_pMainWnd is NULL in CWinApp::Run quitting
application。n〃);
AfxPostQuitMessage(0);
}
return CWinThread::Run();
}
32 位MFC 与16 位MFC 的巨大差异在于CWinApp 与CCmdTarget 之间多出了一
个CWinThread,事情变得稍微复杂一些。CWinThread 定义于THRDCORE。CPP :
int CWinThread::Run()
{
// for tracking the idle time state
BOOL bIdle = TRUE;
LONG lIdleCount = 0;
// acquire and dispatch messages until a WM_QUIT message is received。
for (;;)
{
// phase1: check to see if we can do idle work
while (bIdle &&
!::PeekMessage(&m_msgCur; NULL; NULL; NULL; PM_NOREMOVE))
{
// call OnIdle while in bIdle state
if (!OnIdle(lIdleCount++))
bIdle = FALSE; // assume 〃no idle〃 state
}
// phase2: pump messages while available
do
{
// pump message; but quit on WM_QUIT
391
…………………………………………………………Page 454……………………………………………………………
第篇 湷觥 FC 程式設計
if (!PumpMessage ())
return ExitInstance();
// reset 〃no idle〃 state after pumping 〃normal〃 message
if (IsIdleMessage(&m_msgCur))
{
bIdle = TRUE;
lIdleCount = 0;
}
} while (::PeekMessage(&m_msgCur; NULL; NULL; NULL; PM_NOREMOVE));
}
ASSERT(FALSE); // not reachable
}
BOOL CWinThread::PumpMessage()
{
if (!::GetMessage(&m_msgCur; NULL; NULL; NULL))
{
return FALSE;
}
// process this message
if (m_msgCur。message != WM_KICKIDLE && !PreTranslateMessage(&m_msgCur))
{
::TranslateMessage(&m_msgCur);
::DispatchMessage(&m_msgCur);
}
return TRUE;
}
获得的消息如何交给适当的例程去处理呢?SDK 程序的作法是调用DispatchMessage ,把
消息丢给窗口函数;MFC 也是如此。但我们并未在Hello 程序中提供任何窗口函数,是
的,窗口函数事实上由MFC 提供。回头看看前面AfxEndDeferRegisterClass 源代码,它
在注册四种窗口类别之前已经指定窗口函数为:
wndcls。lpfnWndProc = DefWindowProc;
392
…………………………………………………………Page 455……………………………………………………………
第6章 MFC 程式的生死因果
注意,虽然窗口函数被指定为DefWindowProc 成员函数,但事实上消息并不是被唧往该
处,而是一个名为AfxWndProc 的全域函数去。这其中牵扯到MFC 暗中做了大挪移的
手脚(利用hook 和subclassing),我将在第9章详细讨论这个「乾坤大挪移」。
你看,WinMain 已由MFC 提供,窗口类别已由MFC 注册完成、连窗口函数也都由MFC
提供。那么我们(程序员)如何为特定的消息设计特定的处理例程?MFC 应用程序对讯
息的辨识与判别是采用所谓的「Message Map 机制」。
393
…………………………………………………………Page 456……………………………………………………………
第篇 湷觥 FC 程式設計
把消息与处理函数串接在一起:Message Map 机制
HELLO。CPP
1 CMyWinApp theApp; // application object
BOOL CMyWinApp::InitInstance()
WINMAIN。CPP
{
5
int AFXAPI AfxWinMain (。。。) m_pMainWnd = new CMyFrameWnd();
{ m_pMainWnd…》ShowWindow(m_nCmdShow);
7
CWinApp* pApp = AfxGetApp(); m_pMainWnd…》UpdateWindow();
8
return TRUE;
2 }