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

来源:互联网 发布:python粒子群算法 编辑:程序博客网 时间:2024/06/07 05:08

不调用与 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");}
          回到顶端
          原创粉丝点击