不调用与 PostThreadMessage() PRB: MFC 消息处理程序

不调用与 PostThreadMessage() PRB: MFC 消息处理程序

本页

<style>#tocTitle, #tocDiv{display: none;}</style>

症状
当代码使用 PostThreadMessage() Win32 函数时, 都不会调用 MFC 消息处理程序。

当代码使用 PostThreadMessage() Win32 函数时, 都不会调用 MFC 消息处理程序。   

原因
您在调用 PostThreadMessage() 时该消息被放在线程的消息队列中。但是,因为发布这种方式的消息不是与窗口相关联,MFC 将不发送这些消息或命令处...

您在调用 PostThreadMessage() 时该消息被放在线程的消息队列中。但是,因为发布这种方式的消息不是与窗口相关联,MFC 将不发送这些消息或命令处理程序。  为了处理这些消息,重写您 CWinApp 派生类的 PreTranslateMessage() 函数和手动处理的消息。 

解决方案
下面的代码演示如何调用 PostThreadMessage() CWinApp 派生类使用单线程应用程序的 InitInstance() 中。该原则是相同的辅助...

下面的代码演示如何调用 PostThreadMessage() CWinApp 派生类使用单线程应用程序的 InitInstance() 中。该原则是相同的辅助线程,不同之处在于此处显示该代码将放在备用 CWinThread 派生类中。

visual c + + 4.2 支持过帐到线程的处理消息。有关详细的信息请参阅 Visual c + + 4.2 文档中的 ON_THREAD_MESSAGE。

: MFC 工作线程没有消息循环/泵与它们关联,因此您必须使用用户界面线程。   

示例代码

/* Compile options needed:

   standard MFC project generated by AppWizard */ 

BOOL CThreadMsgApp::PreTranslateMessage(MSG* pMsg)
{
    // Is it the Message you want?
    // You can use a switch statement but because this is
    // only looking for one message, you can use the if/else
    if (pMsg->message == WM_USER+2268)
    {
        // Call the function to handle this message
   OnReceivedCommand(pMsg->wParam,pMsg->lParam);
        // Tell MFC no more processing is needed
        return TRUE;
    }
    else
        // Call MFC to have it continue processing the message
        return CWinThread::PreTranslateMessage(pMsg);
}

BOOL CThreadMsgApp::InitInstance()
{
    WPARAM wParam;
    LPARAM lParam;
    wParam = MAKEWPARAM(0,0); // We can put whatever we
    lParam = MAKELPARAM(0,0); // want in wParam & lParam

    // Send the user-defined Thread Message
    // m_nThreadID is a member of CWinThread that holds the thread ID
    PostThreadMessage(m_nThreadID, WM_USER+2268, wParam, lParam);

    return TRUE;
}

void CThreadMsgApp::OnReceivedCommand(WPARAM wParam, LPARAM lParam)
{
    // You can do whatever you want in here, this is simply
    // sending output to the debug window
    TRACE0("Received WM_USER+2268!!\n");
}
				
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值