深入浅出MFC:对话框消息路由

[appmodul.cpp]

extern "C" int WINAPI _tWinMain(HINSTANCE hInstance, 
                                HINSTANCE hPrevInstance,
                                LPTSTR lpCmdLine, 
                                int nCmdShow)
{
    return AfxWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
}

[winmain.cpp]

int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                      LPTSTR lpCmdLine, int nCmdShow)
{
    int nReturnCode = -1;
    CWinThread* pThread = AfxGetThread();
    CWinApp* pApp = AfxGetApp();

    pApp->InitApplication();

    if (!pThread->InitInstance())
    {
        return nReturnCode;
    }

    nReturnCode = pApp->Run();

    return nReturnCode;
}

CWinApp::InitApplication
CWinThread::InitInstance
CWinThread::Run
均是virtual函数


[ProjName.cpp]

BOOL CProjNameApp::InitInstance()
{
    CWinAppEx::InitInstance();

    CTestWYDlg dlg;
    m_pMainWnd = &dlg;
    INT_PTR nResponse = dlg.DoModal();
    if (nResponse == IDOK)
    {
    }
    else if (nResponse == IDCANCEL)
    {
    }

    return FALSE;
}

由于该函数返回FALSE,所以在AfxWinMain中函数结束后直接退出,
不会运行pApp->Run()


[dlgcore.cpp]

INT_PTR CDialog::DoModal()
{
    AfxHookWindowCreate(this);
    CreateDlgIndirect();
    RunModalLoop();   //消息循环

    DestroyWindow();  //销毁了窗口

    return;
}

[wincore.cpp]

int CWnd::RunModalLoop(DWORD dwFlags)
{
    for (;;)
    {
        if (!AfxPumpMessage())
        {
            AfxPostQuitMessage(0);
            return -1;
        }
    }

    return 0;
}

[thrdcore.cpp]

BOOL AFXAPI AfxPumpMessage()
 {
    CWinThread *pThread = AfxGetThread();
    return pThread->PumpMessage();
 }

[thrdcore.cpp]

BOOL CWinThread::PumpMessage()
{
    return AfxInternalPumpMessage();
}

[thrdcore.cpp]

BOOL AFXAPI AfxInternalPumpMessage()
{
    MSG msg;
    ::GetMessage(&msg);
    if (!AfxPreTranslateMessage(&msg))
    {
        ::TranslateMessage(&msg);
        ::DispatchMessage(&msg);
    }
    return TRUE;
}

[thrdcore.cpp]

BOOL AfxPreTranslateMessage(MSG* pMsg)
{
    CWinThread *pThread = AfxGetThread();
    return pThread->PreTranslateMessage(pMsg);
}

[thrdcore.cpp]

BOOL CWinThread::PreTranslateMessage(MSG* pMsg)
{
    return AfxInternalPreTranslateMessage(pMsg);
}

[thrdcore.cpp]

BOOL AfxInternalPreTranslateMessage(MSG* pMsg)
{
    CWnd* pMainWnd = AfxGetMainWnd();
    if (CWnd::WalkPreTranslateTree(pMainWnd->GetSafeHwnd(), pMsg))
        return TRUE;
    return FALSE;
}

[wincore.cpp]

//
// 返回TRUE or FALSE
// 由以上流程得:该函数返回TRUE,则pMsg指向的消息将会被忽略而不处理
//                    返回FALSE,则会DispatchMessage到对应的窗口过程函数处理
//
BOOL PASCAL CWnd::WalkPreTranslateTree(HWND hWndStop, MSG* pMsg)
{
    //
    // 先调用本窗口对应CWnd类的PreTranslateMessage
    // 再往父窗口层层上溯调用对应CWnd类的PreTranslateMessage直到上溯到hWndStop对应的窗口
    // 若其中的某个PreTranslateMessage返回TRUE, 则函数退出返回TRUE
    // 否则上溯完成返回FALSE
    //
    for (HWND hWnd = pMsg->hwnd; hWnd != NULL; hWnd = ::GetParent(hWnd))
    {
        CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
        if (pWnd != NULL)
        {
            if (pWnd->PreTranslateMessage(pMsg))                        
                return TRUE;
        }
        if (hWnd == hWndStop)
            break;
    }
    return FALSE;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值