不调用与 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 工作线程没有消息循环/泵与它们关联,因此您必须使用用户界面线程。
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"); }