在BOOL CWinThread::PumpMessage()中有如下:
if (m_msgCur.message != WM_KICKIDLE && !PreTranslateMessage(&m_msgCur))
{
   ::TranslateMessage(&m_msgCur);
   ::DispatchMessage(&m_msgCur);
}
其中m_msgCur是一个MSG结构的变量,MSG的结构如下:typedef struct tagMSG {     // msg 
    HWND   hwnd;     
    UINT   message; 
    WPARAM wParam; 
    LPARAM lParam; 
    DWORD  time; 
    POINT  pt; 
} MSG; 其中的hwnd:Handle to the window whose window procedure receives the message. 当::DispatchMessage(&m_msgCur);发生以后,就会调用AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM 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);
}我现在有个疑问:
不管是什么消息,WM_XX ,命令消息 ,控件消息 ,为什么在这里在AfxWndProc中的 CWnd* pWnd 始终是一个 Cwnd派生类的指针。
难道说产生消息的只能是窗口类吗?一个例子:
如果是一个命令命令消息(比如菜单),可以由C**DOC来响应,用函数栈可以看到这个pWnd 是指向CMainFrame

解决方案 »

  1.   

    难道说产生消息的只能是窗口类吗?
    正是。CMainFrame也是从CWnd逐级派生出来的。
      

  2.   

    是吗?天那!
    那些不是从CWND派生下来的类,只有通过MFC本身的消息路由分派机制 来获得机会 处理消息了?
    还有,
    当点击一下 菜单,产生消息,填充消息结构MSG,其中的hwnd,就被填充为CMainFrame,有那么“智能”吗?
      

  3.   

    to :surstar(断水流),您所指的“界面”,就意味着cwnd的派生吧?
     
    我在
    void CTestView::OnMouseMove(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    AfxMessageBox("view mousemove");
    CView::OnMouseMove(nFlags, point);
    }
    void CMainFrame::OnMouseMove(UINT nFlags, CPoint point) 
    {
    // TODO: Add your message handler code here and/or call default
    AfxMessageBox("mainfram moouse");
    CFrameWnd::OnMouseMove(nFlags, point);
    }
    view 把 本来属于CMainFrame的覆盖了,所以 接收不到CMainFrame的MouseMove
    同样的道理:菜单应该是属于什么呢?
    在CMainFrame中只有:
    CStatusBar  m_wndStatusBar;//状态
    CToolBar    m_wndToolBar;//工具它们和 view 覆盖了CMainFrame,使得CMainFrame接收不到MouseMove
    当点击一下 菜单,产生消息,填充消息结构MSG,其中的hwnd,就被填充为CMainFrame,有那么“智能”吗?
      

  4.   

    要有界面才可以接受消息,所以它们都是从CWnd继承来的。
      

  5.   

    菜单消息 ON_COMMAND好像不一定要CWND类处理吧~~~
    只要是CCmdTarget的子类就可以   比如document类可以响应菜单消息