MFC对话框消息传递顺序

来源:互联网 发布:图片在线缩小软件 编辑:程序博客网 时间:2024/05/16 08:37
 

view plaincopy to clipboardprint?
LRESULT CALLBACK AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)  
{  
    // special message which identifies the window as using AfxWndProc  
    if (nMsg == WM_QUERYAFXWNDPROC)  
        return 1;  
 
    // all other messages route through message map  
    CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);  
    ASSERT(pWnd != NULL);  
    ASSERT(pWnd->m_hWnd == hWnd);  
    if (pWnd == NULL || pWnd->m_hWnd != hWnd)  
        return ::DefWindowProc(hWnd, nMsg, wParam, lParam);  
    return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);  

LRESULT CALLBACK AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
 // special message which identifies the window as using AfxWndProc
 if (nMsg == WM_QUERYAFXWNDPROC)
  return 1;

 // all other messages route through message map
 CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
 ASSERT(pWnd != NULL);
 ASSERT(pWnd->m_hWnd == hWnd);
 if (pWnd == NULL || pWnd->m_hWnd != hWnd)
  return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
 return AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
}

1.AfxWndProc()      该函数负责接收消息,找到消息所属的CWnd对象,然后调用AfxCallWndProc。

view plaincopy to clipboardprint?
LRESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,  
    WPARAM wParam = 0, LPARAM lParam = 0)  
{  
    _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();  
    MSG oldState = pThreadState->m_lastSentMsg;   // save for nesting  
    pThreadState->m_lastSentMsg.hwnd = hWnd;  
    pThreadState->m_lastSentMsg.message = nMsg;  
    pThreadState->m_lastSentMsg.wParam = wParam;  
    pThreadState->m_lastSentMsg.lParam = lParam;  
 
#ifdef _DEBUG  
    if (afxTraceFlags & traceWinMsg)  
        _AfxTraceMsg(_T("WndProc"), &pThreadState->m_lastSentMsg);  
#endif  
 
    // Catch exceptions thrown outside the scope of a callback  
    // in debug builds and warn the user.  
    LRESULT lResult;  
    TRY  
    {  
#ifndef _AFX_NO_OCC_SUPPORT  
        // special case for WM_DESTROY  
        if ((nMsg == WM_DESTROY) && (pWnd->m_pCtrlCont != NULL))  
            pWnd->m_pCtrlCont->OnUIActivate(NULL);  
#endif  
 
        // special case for WM_INITDIALOG  
        CRect rectOld;  
        DWORD dwStyle = 0;  
        if (nMsg == WM_INITDIALOG)  
            _AfxPreInitDialog(pWnd, &rectOld, &dwStyle);  
 
        // delegate to object's WindowProc  
        lResult = pWnd->WindowProc(nMsg, wParam, lParam);  
 
        // more special case for WM_INITDIALOG  
        if (nMsg == WM_INITDIALOG)  
            _AfxPostInitDialog(pWnd, rectOld, dwStyle);  
    }  
    CATCH_ALL(e)  
    {  
        CWinThread* pWinThread = AfxGetThread();  
        if ( pWinThread != NULL )  
        {  
            lResult = pWinThread->ProcessWndProcException(e, &pThreadState->m_lastSentMsg);  
            TRACE1("Warning: Uncaught exception in WindowProc (returning %ld)./n",  
                lResult);  
        }  
        else 
        {  
            TRACE0("Warning: Uncaught exception in WindowProc./n");  
            lResult = 0;  
        }  
        DELETE_EXCEPTION(e);  
    }  
    END_CATCH_ALL  
 
    pThreadState->m_lastSentMsg = oldState;  
    return lResult;  

LRESULT AFXAPI AfxCallWndProc(CWnd* pWnd, HWND hWnd, UINT nMsg,
 WPARAM wParam = 0, LPARAM lParam = 0)
{
 _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
 MSG oldState = pThreadState->m_lastSentMsg;   // save for nesting
 pThreadState->m_lastSentMsg.hwnd = hWnd;
 pThreadState->m_lastSentMsg.message = nMsg;
 pThreadState->m_lastSentMsg.wParam = wParam;
 pThreadState->m_lastSentMsg.lParam = lParam;

#ifdef _DEBUG
 if (afxTraceFlags & traceWinMsg)
  _AfxTraceMsg(_T("WndProc"), &pThreadState->m_lastSentMsg);
#endif

 // Catch exceptions thrown outside the scope of a callback
 // in debug builds and warn the user.
 LRESULT lResult;
 TRY
 {
#ifndef _AFX_NO_OCC_SUPPORT
  // special case for WM_DESTROY
  if ((nMsg == WM_DESTROY) && (pWnd->m_pCtrlCont != NULL))
   pWnd->m_pCtrlCont->OnUIActivate(NULL);
#endif

  // special case for WM_INITDIALOG
  CRect rectOld;
  DWORD dwStyle = 0;
  if (nMsg == WM_INITDIALOG)
   _AfxPreInitDialog(pWnd, &rectOld, &dwStyle);

  // delegate to object's WindowProc
  lResult = pWnd->WindowProc(nMsg, wParam, lParam);

  // more special case for WM_INITDIALOG
  if (nMsg == WM_INITDIALOG)
   _AfxPostInitDialog(pWnd, rectOld, dwStyle);
 }
 CATCH_ALL(e)
 {
  CWinThread* pWinThread = AfxGetThread();
  if ( pWinThread != NULL )
  {
   lResult = pWinThread->ProcessWndProcException(e, &pThreadState->m_lastSentMsg);
   TRACE1("Warning: Uncaught exception in WindowProc (returning %ld)./n",
    lResult);
  }
  else
  {
   TRACE0("Warning: Uncaught exception in WindowProc./n");
   lResult = 0;
  }
  DELETE_EXCEPTION(e);
 }
 END_CATCH_ALL

 pThreadState->m_lastSentMsg = oldState;
 return lResult;
}

2.AfxCallWndProc() 该函数负责保存消息(保存的内容主要是消息标识符和消息参数)供应用程序以后使用,然后调用WindowProc()函数。

3.WindowProc()      该函数负责发送消息到OnWndMsg()函数,如果未被处理,则调用DefWindowProc()函数。

4.OnWndMsg()        该函数的功能首先按字节对消息进行排序,对于WM_COMMAND消息,调用OnCommand()消息响应函数,对于WM_NOTIFY消息调用OnNotify()消息响应函数。任何被遗漏的消息将是一个窗口消息。OnWndMsg()函数搜索类的消息映像,以找到一个能处理任何窗口消息的处理函数。如果OnWndMsg()函数不能找到这样的处理函数的话,则
把消息返回到WindowProc()函数,由它将消息发送给DefWindowProc()函数。

5.OnCommand()       该函数查看这是不是一个控件通知(lParam参数不为NULL,如果lParam参数为空的话,说明该消息不是控件通知),如果它是,OnCommand()函数会试图将消息映射到制造通知的控件;如果他不是一个控件通知(或者如果控件拒绝映射的消息)OnCommand()就会调用OnCmdMsg()函数。

6.OnCmdMsg()        根据接收消息的类,OnCmdMsg()函数将在一个称为命令传递(Command Routing)的过程中潜在的传递命令消息和控件通知。例如:如果拥有该窗口的类是一个框架类,则命令和通知消息也被传递到视图和文档类,并为该
类寻找一个消息处理函数。

MFC应用程序创建窗口的过程

1.PreCreateWindow()   该函数是一个重载函数,在窗口被创建前,可以在该重载函数中改变创建参数(可以设置窗口风格等等)。

2.PreSubclassWindow() 这也是一个重载函数,允许首先子分类一个窗口。

3.OnGetMinMaxInfo()   该函数为消息响应函数,响应的是WM_GETMINMAXINFO消息,允许设置窗口的最大或者最小尺寸。

4.OnNcCreate()        该函数也是一个消息响应函数,响应WM_NCCREATE消息,发送消息以告诉窗口的客户区 即将被创建。

5.OnNcCalcSize()      该函数也是消息响应函数,响应WM_NCCALCSIZE消息,作用是允许改变窗口客户区大小。

6.OnCreate()          该函数也是一个消息响应函数,响应WM_CREATE消息,发送消息告诉一个窗口已经被创建。

7.OnSize()            该函数也是一个消息响应函数,响应WM_SIZE消息,发送该消息以告诉该窗口大小已经发生变化。

8.OnMove()            消息响应函数,响应WM_MOVE消息,发送此消息说明窗口在移动。

9.OnChildNotify()     该函数为重载函数,作为部分消息映射被调用,告诉父窗口即将被告知一个窗口刚刚被创建。

MFC应用程序关闭窗口的顺序(非模态窗口)

1.OnClose()       消息响应函数,响应窗口的WM_CLOSE消息,当关闭按钮被单击的时候发送此消息。

2.OnDestroy()     消息响应函数,响应窗口的WM_DESTROY消息,当一个窗口将被销毁时,发送此消息。

3.OnNcDestroy()   消息响应函数,响应窗口的WM_NCDESTROY消息,当一个窗口被销毁后发送此消息。

4.PostNcDestroy() 重载函数,作为处理OnNcDestroy()函数的最后动作,被CWnd调用。


MFC应用程序中打开模式对话框的函数调用顺序

1.DoModal()             重载函数,重载DoModal()成员函数。

2.PreSubclassWindow()   重载函数,允许首先子分类一个窗口。

3.OnCreate()            消息响应函数,响应WM_CREATE消息,发送此消息以告诉一个窗口已经被创建。

4.OnSize()              消息响应函数,响应WM_SIZE消息,发送此消息以告诉窗口大小发生变化。

5.OnMove()              消息响应函数,响应WM_MOVE消息,发送此消息,以告诉窗口正在移动。

6.OnSetFont()           消息响应函数,响应WM_SETFONT消息,发送此消息,以允许改变对话框中控件的字体。

7.OnInitDialog()        消息响应函数,响应WM_INITDIALOG消息,发送此消息以允许初始化对话框中的控件,或者是创建新控件。

8.OnShowWindow()        消息响应函数,响应WM_SHOWWINDOW消息,该函数被ShowWindow()函数调用。

9.OnCtlColor()          消息响应函数,响应WM_CTLCOLOR消息,被父窗口发送已改变对话框或对话框上面控件的颜色。

10. OnChildNotify()     重载函数,作为WM_CTLCOLOR消息的结果发送。

MFC应用程序中关闭模式对话框的顺序

1.OnClose()        消息响应函数,响应WM_CLOSE消息,当"关闭"按钮被单击的时候,该函数被调用。

2.OnKillFocus()    消息响应函数,响应WM_KILLFOCUS消息,当一个窗口即将失去键盘输入焦点以前被发送。

3.OnDestroy()      消息响应函数,响应WM_DESTROY消息,当一个窗口即将被销毁时,被发送。

4.OnNcDestroy()    消息响应函数,响应WM_NCDESTROY消息,当一个窗口被销毁以后被发送。

5.PostNcDestroy() 重载函数,作为处理OnNcDestroy()函数的最后动作被CWnd调用。


打开无模式对话框的顺序

1.PreSubclassWindow()    重载函数,允许用户首先子分类一个窗口。

2.OnCreate()             消息响应函数,响应WM_CREATE消息,发送此消息以告诉一个窗口已经被创建。

3.OnSize()               消息响应函数,响应WM_SIZE消息,发送此消息以告诉窗口大小发生变化。

4.OnMove()               消息响应函数,响应WM_MOVE消息,发送此消息以告诉窗口正在移动。

5.OnSetFont()            消息响应函数,响应WM_SETFONT消息,发送此消息以允许改变对话框中控件的字体。

 

 

转自: http://blog.csdn.net/zmq5411/article/details/6281966