MFC消息处理流程概述
2012-10-21 21:22
337 查看
Win32下的消息流程清晰明了,但在MFC下,由于封装的缘故,隐藏的有点深,对一般的开发人员而言,就不甚明了喽。本文试图粗略展示出MFC下消息处理的基本流程。
一、先看一下Win32下的消息处理流程
[cpp]
view plaincopyprint?
while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
二、MFC下的消息处理流程
[cpp]
view plaincopyprint?
BOOL AFXAPI AfxInternalPumpMessage()
{
MSG msg;
::GetMessage(&msg, NULL, NULL, NULL);
if (!AfxPreTranslateMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return TRUE;
}
注:以上代码为示意代码,具体请参照MFC的源码。
[cpp]
view plaincopyprint?
LRESULT CALLBACK AfxWndProc(HWND hWnd,UINT nMsg,
WPARAM wParam,LPARAM lParam)
{
CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
//从HWND获取对应的CWnd*
if (pWnd == NULL || pWnd->m_hWnd != hWnd)
return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
else
return pWnd->WindowProc(nMsg, wParam, lParam);
}
很显然,调用了CWnd类的虚函数virtual CWnd::WindowProc处理。
[cpp]
view plaincopyprint?
LRESULT CWnd::WindowProc(UINT message,WPARAM wParam,
LPARAM lParam)
{
// OnWndMsg does most of the work, except for DefWindowProc call
LRESULT lResult = 0;
if (!OnWndMsg(message, wParam, lParam, &lResult))
lResult = DefWindowProc(message, wParam, lParam);
return lResult;
}
WindowProc函数又调用了CWnd类的虚函数virtual CWnd::OnWndMsg处理。
[cpp]
view plaincopyprint?
BOOL CWnd::OnWndMsg(UINT message,WPARAM wParam,
LPARAM lParam,LRESULT* pResult)
{
LRESULT lResult = 0;
union MessageMapFunctions mmf;
mmf.pfn = 0;
CInternalGlobalLock winMsgLock;
// special case for commands
if (message == WM_COMMAND)
{
if (OnCommand(wParam, lParam))
{
lResult = 1;
goto LReturnTrue;
}
return FALSE;
}
// special case for notifies
if (message == WM_NOTIFY)
{
NMHDR* pNMHDR = (NMHDR*)lParam;
if (pNMHDR->hwndFrom != NULL && OnNotify(wParam, lParam, &lResult))
goto LReturnTrue;
return FALSE;
}
......
return TRUE;
}
一、先看一下Win32下的消息处理流程
每一个线程都对应有一个消息队列,利用API函数GetMessage从消息队列中获取消息,然后利用TranslateMessage翻译消息(主要是一些键盘消息),再利用DispatchMessage将消息分发给对应的窗口过程函数处理。
一般我们在WinMain函数中利用如下代码来处理消息:
[cpp]
view plaincopyprint?
while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); }
很显然,整个消息循环很清楚。
二、MFC下的消息处理流程
1、MFC下的消息处理流程由thrdcore.cpp中的AfxInternalPumpMessage开始:
[cpp]
view plaincopyprint?
BOOL AFXAPI AfxInternalPumpMessage()
{
MSG msg;
::GetMessage(&msg, NULL, NULL, NULL);
if (!AfxPreTranslateMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return TRUE;
}
BOOL AFXAPI AfxInternalPumpMessage() { MSG msg; ::GetMessage(&msg, NULL, NULL, NULL); if (!AfxPreTranslateMessage(&msg)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } return TRUE; }
注:以上代码为示意代码,具体请参照MFC的源码。
很显然,其消息处理流程也类似<一>中Win32下的消息处理,只不过在调用TranslateMessage、DispatchMessage处理消息前增加了类似过滤的函数AfxPreTranslateMessage。该函数会调用CWnd类的PreTranslateMessage函数,函数返回True则消息将不会被处理。我们经常会通过重载CWnd类的PreTranslateMessage来改变MFC的消息控制流程。
2、窗口过程函数
通过调用DispatchMessage将消息分发给了具体的窗口过程函数处理。MFC下的所有窗口都拥有公用的窗口过程函数AfxWndProc。该函数的示意代码如下:
[cpp]
view plaincopyprint?
LRESULT CALLBACK AfxWndProc(HWND hWnd,UINT nMsg,
WPARAM wParam,LPARAM lParam)
{
CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
//从HWND获取对应的CWnd*
if (pWnd == NULL || pWnd->m_hWnd != hWnd)
return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
else
return pWnd->WindowProc(nMsg, wParam, lParam);
}
LRESULT CALLBACK AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) { CWnd* pWnd = CWnd::FromHandlePermanent(hWnd); //从HWND获取对应的CWnd* if (pWnd == NULL || pWnd->m_hWnd != hWnd) return ::DefWindowProc(hWnd, nMsg, wParam, lParam); else return pWnd->WindowProc(nMsg, wParam, lParam); }
很显然,调用了CWnd类的虚函数virtual CWnd::WindowProc处理。
[cpp]
view plaincopyprint?
LRESULT CWnd::WindowProc(UINT message,WPARAM wParam,
LPARAM lParam)
{
// OnWndMsg does most of the work, except for DefWindowProc call
LRESULT lResult = 0;
if (!OnWndMsg(message, wParam, lParam, &lResult))
lResult = DefWindowProc(message, wParam, lParam);
return lResult;
}
LRESULT CWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam) { // OnWndMsg does most of the work, except for DefWindowProc call LRESULT lResult = 0; if (!OnWndMsg(message, wParam, lParam, &lResult)) lResult = DefWindowProc(message, wParam, lParam); return lResult; }
WindowProc函数又调用了CWnd类的虚函数virtual CWnd::OnWndMsg处理。
[cpp]
view plaincopyprint?
BOOL CWnd::OnWndMsg(UINT message,WPARAM wParam,
LPARAM lParam,LRESULT* pResult)
{
LRESULT lResult = 0;
union MessageMapFunctions mmf;
mmf.pfn = 0;
CInternalGlobalLock winMsgLock;
// special case for commands
if (message == WM_COMMAND)
{
if (OnCommand(wParam, lParam))
{
lResult = 1;
goto LReturnTrue;
}
return FALSE;
}
// special case for notifies
if (message == WM_NOTIFY)
{
NMHDR* pNMHDR = (NMHDR*)lParam;
if (pNMHDR->hwndFrom != NULL && OnNotify(wParam, lParam, &lResult))
goto LReturnTrue;
return FALSE;
}
......
return TRUE;
}
BOOL CWnd::OnWndMsg(UINT message, WPARAM wParam, LPARAM lParam, LRESULT* pResult) { LRESULT lResult = 0; union MessageMapFunctions mmf; mmf.pfn = 0; CInternalGlobalLock winMsgLock; // special case for commands if (message == WM_COMMAND) { if (OnCommand(wParam, lParam)) { lResult = 1; goto LReturnTrue; } return FALSE; } // special case for notifies if (message == WM_NOTIFY) { NMHDR* pNMHDR = (NMHDR*)lParam; if (pNMHDR->hwndFrom != NULL && OnNotify(wParam, lParam, &lResult)) goto LReturnTrue; return FALSE; } ...... return TRUE; }
在OnWndMsg函数中会根据具体的消息类型,在MFC的消息映射表中找到对应的函数处理。
以上就是MFC处理消息的大致流程。
相关文章推荐
- MFC消息处理流程概述
- MFC消息处理流程概述 .
- MFC消息处理流程概述
- MFC消息处理流程概述
- MFC中的消息处理流程
- MFC消息处理流程
- MFC下WM_NOTIFY消息处理流程
- MFC消息分类和处理方式以及MFC程序的执行流程
- MFC下WM_NOTIFY消息处理流程
- 深入剖析MFC中对于Windows消息处理、运行机制
- MFC应用程序中处理消息的顺序 (转)
- 【转】MFC消息处理(一)
- MFC消息映射与处理
- MFC编程入门之九(对话框:为控件添加消息处理函数)
- MFC中处理消息的顺序
- Qualcomm平台qcril初始化及消息处理流程
- MFC消息映射和处理机制整理
- MFC应用程序中处理消息的顺序
- 在MFC中手动添加消息处理函数PreTranslateMessage和自定义消息
- MFC的消息处理机制由两部分组成:CCmdTarget类和消息映射表。