原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182120

1、MFC窗口如何与AfxWndProc建立联系。

当一个新的CWnd派生类创建时,在调用CWnd::CreateEx()过程中,MFC都会安装AfxCbtFilterHook()。这个Hook将拦截HCBT_CREATEWND,将窗体的消息处理函数设置为AfxWndProc()。

  1. // wincore.cpp 651
  2. // CWnd::CreateEx函数通过AfxHookWindowCreate函数安插Hook
  3. BOOL CWnd::CreateEx(DWORD dwExStyle, LPCTSTR lpszClassName,
  4. LPCTSTR lpszWindowName, DWORD dwStyle,
  5. int x, int y, int nWidth, int nHeight,
  6. HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam)
  7. {
  8. // ...
  9. // 如果cs中窗体类名为空,PreCreateWindow (见wincore.cpp 714) 将调用
  10. // AfxEndDeferRegisterClass (见wincore.cpp 4431)注册默认窗口类,
  11. // 默认注册的窗口消息处理函数是DefWindowProc
  12. if (!PreCreateWindow(cs))
  13. {
  14. PostNcDestroy();
  15. return FALSE;
  16. }
  17. // 安插AfxCbtFilterHook
  18. AfxHookWindowCreate(this);
  19. HWND hWnd = ::AfxCtxCreateWindowEx(cs.dwExStyle, cs.lpszClass,
  20. cs.lpszName, cs.style, cs.x, cs.y, cs.cx, cs.cy,
  21. cs.hwndParent, cs.hMenu, cs.hInstance, cs.lpCreateParams);
  22. if (!AfxUnhookWindowCreate())
  23. PostNcDestroy();        // cleanup if CreateWindowEx fails too soon
  24. // ...
  25. }
  26. // wincore.cpp 609
  27. void AFXAPI AfxHookWindowCreate(CWnd* pWnd)
  28. {
  29. _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
  30. if (pThreadState->m_pWndInit == pWnd)
  31. return;
  32. if (pThreadState->m_hHookOldCbtFilter == NULL)
  33. {
  34. // 安插_AfxCbtFilterHook,因为_AfxCbtFilterHook()是一个基于计算机训练的hook,
  35. // 所以windows在激活、创建、销毁、最大化、最小化、移动窗口或改变窗口大小之前,
  36. // 会调用_AfxCbtFilterHook()
  37. pThreadState->m_hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT,
  38. _AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
  39. if (pThreadState->m_hHookOldCbtFilter == NULL)
  40. AfxThrowMemoryException();
  41. }
  42. pThreadState->m_pWndInit = pWnd;
  43. }

_AfxCbtFilterHook通过SetWindowLongPtr函数将窗口的处理函数替换成AfxWndProc(),同时,在CWnd::m_pfnSuper中保存原来的窗口消息处理函数指针。

  1. // wincore.cpp 472
  2. LRESULT CALLBACK
  3. _AfxCbtFilterHook(int code, WPARAM wParam, LPARAM lParam)
  4. {
  5. _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
  6. if (code != HCBT_CREATEWND)
  7. {
  8. // 如果不是HCBT_CREATEWND消息,直接忽略。接收到该消息意味着窗口要创建了
  9. return CallNextHookEx(pThreadState->m_hHookOldCbtFilter, code,
  10. wParam, lParam);
  11. }
  12. LPCREATESTRUCT lpcs = ((LPCBT_CREATEWND)lParam)->lpcs;
  13. CWnd* pWndInit = pThreadState->m_pWndInit;
  14. BOOL bContextIsDLL = afxContextIsDLL;
  15. if (pWndInit != NULL || (!(lpcs->style & WS_CHILD) && !bContextIsDLL))
  16. {
  17. // ...
  18. HWND hWnd = (HWND)wParam;
  19. WNDPROC oldWndProc;
  20. if (pWndInit != NULL)
  21. {
  22. AFX_MANAGE_STATE(pWndInit->m_pModuleState);
  23. // connect the HWND to pWndInit...
  24. pWndInit->Attach(hWnd);
  25. // allow other subclassing to occur first
  26. pWndInit->PreSubclassWindow();
  27. // 消息处理函数指针,用于存储原来的消息处理函数
  28. WNDPROC *pOldWndProc = pWndInit->GetSuperWndProcAddr();
  29. // 将窗口的消息处理函数设置成AfxWndProc()
  30. WNDPROC afxWndProc = AfxGetAfxWndProc();
  31. oldWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC,
  32. (DWORD_PTR)afxWndProc);
  33. if (oldWndProc != afxWndProc)
  34. *pOldWndProc = oldWndProc;      // 保存原来的指针
  35. pThreadState->m_pWndInit = NULL;
  36. }
  37. // ...
  38. }
  39. // ...
  40. }
  41. // wincore.cpp 1048
  42. WNDPROC* CWnd::GetSuperWndProcAddr()
  43. {
  44. // Note: it is no longer necessary to override GetSuperWndProcAddr
  45. //  for each control class with a different WNDCLASS.
  46. //  This implementation now uses instance data, such that the previous
  47. //  WNDPROC can be anything.
  48. return &m_pfnSuper;
  49. }
  50. // wincore.cpp 392
  51. WNDPROC AFXAPI AfxGetAfxWndProc()
  52. {
  53. // 静态库版本的消息处理函数
  54. return &AfxWndProc;
  55. }

微软不将AfxWndProc()做为注册窗口过程的原因是DefWindowPorc()可以支持3D控件。这些控件都在微软的CTL3D.dll中。如果系统具有CTL3D功能已经是一种迫切需要,那么应用程序就要覆盖CTL3D的功能(在处理WM_CTLCOLOR消息方面)。为了确保这一点,MFC必须按照以下顺序调用:AfxWndProc()、CTL3D的WndProc()和最后的DefWindowProc()。可见为了确保这一点,微软不得不允许CTL3D在AfxWndProc()之前分类,这就意味着延迟AfxWndProc()的引入。

2、处理消息
MFC用两种方式表示窗口:(1)用统一的系统定义的窗口句柄;(2)用表示窗口的C++类。窗口句柄由CWnd和CWnd的派生类包装。因为窗口句柄是CWnd的成员变量。
MFC用CMapPtrToPtr对象将窗口句柄映射成CWnd对象。MFC在窗口存在期间维护这个链接。如果使用CWnd创建一个窗口,窗口句柄就会和CWnd对象关联在一起,也就是说二者通过句柄映射表关联在一起,MFC这样做就使得框架可以使用C++对象,而不是窗口句柄。
 
AfxWndProc()处理一个特定消息:WM_QUERYAFXWNDPROC,如果消息是WM_QUERYAFXWNDPROC,AfxWndProc()就返回1。应用程序可以通过发送WM_QUERYAFXWNDPROC消息来查询该窗口是否是使用MFC消息映射系统的MFC窗口。
 
  1. // wincore.cpp 375
  2. LRESULT CALLBACK
  3. AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
  4. {
  5. // special message which identifies the window as using AfxWndProc
  6. if (nMsg == WM_QUERYAFXWNDPROC)
  7. return 1;
  8. // all other messages route through message map
  9. CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
  10. if (pWnd == NULL || pWnd->m_hWnd != hWnd)
  11. return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
  12. return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);    // 调用AfxCallWndProc
  13. }
  14. // wincore.cpp 208
  15. LRESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
  16. WPARAM wParam = 0, LPARAM lParam = 0)
  17. {
  18. _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
  19. MSG oldState = pThreadState->m_lastSentMsg;   // 保存最后发送消息
  20. pThreadState->m_lastSentMsg.hwnd = hWnd;
  21. pThreadState->m_lastSentMsg.message = nMsg;
  22. pThreadState->m_lastSentMsg.wParam = wParam;
  23. pThreadState->m_lastSentMsg.lParam = lParam;
  24. // Catch exceptions thrown outside the scope of a callback
  25. // in debug builds and warn the user.
  26. LRESULT lResult;
  27. #ifndef _AFX_NO_OCC_SUPPORT
  28. // special case for WM_DESTROY
  29. if ((nMsg == WM_DESTROY) && (pWnd->m_pCtrlCont != NULL))
  30. pWnd->m_pCtrlCont->OnUIActivate(NULL);
  31. #endif
  32. // special case for WM_INITDIALOG
  33. CRect rectOld;
  34. DWORD dwStyle = 0;
  35. if (nMsg == WM_INITDIALOG)
  36. _AfxPreInitDialog(pWnd, &rectOld, &dwStyle);        // 使对话框自动置于窗口中间
  37. // 委托给对象的WindowProc,覆盖该函数,可以在MFC查看某个消息之前处理这个消息
  38. lResult = pWnd->WindowProc(nMsg, wParam, lParam);
  39. // more special case for WM_INITDIALOG
  40. if (nMsg == WM_INITDIALOG)
  41. _AfxPostInitDialog(pWnd, rectOld, dwStyle);
  42. pThreadState->m_lastSentMsg = oldState;
  43. return lResult;
  44. }
  45. // wincore.cpp 1737
  46. LRESULT CWnd::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  47. {
  48. // OnWndMsg does most of the work, except for DefWindowProc call
  49. LRESULT lResult = 0;
  50. if (!OnWndMsg(message, wParam, lParam, &lResult))   // wincore.cpp 1746
  51. lResult = DefWindowProc(message, wParam, lParam);
  52. return lResult;
  53. }

OnWndMsg函数很大,它首先过滤出WM_COMMAND、WM_NOTIFY、WM_ACTIVE和WM_SETCURSOR。对于这几个消息,框架有自己的处理方法。如果不是这几个,OnWndMsg会在消息映射表中查找消息。MFC维护着一个消息映射表入口缓存,可以通过散列值访问它。

【转】MFC消息处理(一)的更多相关文章

  1. MFC消息处理

    1.MFC窗口如何与AfxWndProc建立联系. 当一个新的CWnd派生类创建时,在调用CWnd::CreateEx()过程中,MFC都会安装AfxCbtFilterHook().这个Hook将拦截 ...

  2. 深入剖析MFC中对于Windows消息处理、运行机制

    序: 本人对Windows系统.MFC谈不上有深入的了解,但对MFC本身包装API的机制很有兴趣,特别是读了候老师的<深入浅出MFC>后,感觉到VISUAL C++的Application ...

  3. MFC下WM_NOTIFY消息处理流程

    参考文章:MFC的消息反射机制 在前一篇文章:MFC消息处理流程概述中描述了MFC消息处理的大体流程.由CWnd::OnWndMsg函数可知,当消息为WM_NOTIFY消息时,调用的是virtual ...

  4. windows消息机制(MFC)

    消息分类与消息队列 Windows中,消息使用统一的结构体(MSG)来存放信息,其中message表明消息的具体的类型, 而wParam,lParam是其最灵活的两个变量,为不同的消息类型时,存放数据 ...

  5. MFC中的几个虚函数

    1.PreTranslateMessage()和WindowProc() PreTranslateMessage是消息在送给TranslateMessage函数之前被调用的,通过函数名也可以猜出来.绝 ...

  6. [转]windows消息机制(MFC)

    消息分类与消息队列 Windows中,消息使用统一的结构体(MSG)来存放信息,其中message表明消息的具体的类型, 而wParam,lParam是其最灵活的两个变量,为不同的消息类型时,存放数据 ...

  7. VC2008中如何为MFC应用程序添加和删除消息响应函数

    最近重温<MFC Windows应用程序设计>第二版这本书,里面的代码全部是使用VC6.0写的,我Win7下安装的是VS2008开发环境. VC2008下添加和删除常见的消息响应函数有两种 ...

  8. C语言中的sizeof()

    sizeof,一个其貌不扬的家伙,引无数菜鸟竟折腰,小虾我当初也没少犯迷糊,秉着"辛苦我一个,幸福千万人"的伟大思想,我决定将其尽可能详细的总结一下. 但当我总结的时候才发现,这个 ...

  9. 全面总结sizeof的用法(定义、语法、指针变量、数组、结构体、类、联合体、位域位段)

    一.前言 编译环境是vs2010(32位). <span style="font-size:18px;">#include<iostream> #inclu ...

随机推荐

  1. Mkdocs 搭建

    1. 利用pip安装mkdocs sudo pip install mkdocs 2.如果报pip不存在 或是 报权限错误,要不是pip没有安装,就是python里某个库没有关联上,这时候需要重新安装 ...

  2. Linux的crontab

    如果要让unix系统重复,定期做一件事,我们就会用到crontab. 实质上真正去执行每一个重复任务的是cron,cron是的unix家族的一个后台常驻程序,cron是由cron文件来驱动的,cron ...

  3. sqlite的缺点和限制

    随着查询变大变复杂,查询时间使得网络调用或者事务处理开销相形见绌, 这时一些大型的设计复杂的数据库开始发挥作用了. 虽然SQLite也能处理复杂的查询,但是它没有精密的优化器或者查询计划器. SQLi ...

  4. Linux服务器使用tar加密压缩文件

    使用tar命令进行加密压缩时需要和OpenSSL进行结合 加密压缩命令如下: tar -zcf - filename |openssl des3 -salt -k passwd | dd of=fil ...

  5. ArrayList扩容

    jdk1.5 public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new Illega ...

  6. 框架-springmvc源码分析(一)

    框架-springmvc源码分析(一) 参考: http://www.cnblogs.com/heavenyes/p/3905844.html#a1 https://www.cnblogs.com/B ...

  7. Ubuntu 14.04安装gnuplot 解决Terminal type set to 'unknown'问题 简易命令教程

    参考: 照猫画虎学gnuplot之折线图 gnuplot 入门教程 1 gnuplot安装,及error:terminal type set to 'unknown'的解决 安装 sudo apt-g ...

  8. Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees dfs序+线段树

    E. Little Girl and Problem on Trees time limit per test 2 seconds memory limit per test 256 megabyte ...

  9. 创建一个 SQLite 数据库

    首先,我们学习如何创建一个SQLite 数据库.如果想要在data/example.sqlite 这个路径中创建一个示例数据库,就必须确保该路径存在.如果该路径不存在,就必须先创建路径:if (!di ...

  10. vue 脚手架(一,创建脚手架)

    本文以转移至本人的个人博客,请多多关注! 本文以转移至本人的个人博客,请多多关注! 本文以转移至本人的个人博客,请多多关注! 本文以转移至本人的个人博客,请多多关注! 经过一段时间对Vue的学习.觉得 ...