PreTranslateMessage是标准窗口的消息预处理响应函数,在任何标准窗口有效。
DLL中窗口的创建是在一个导出函数中,并在调用CWnd::Create这前调用了
AFX_MANAGE_STATE(AfxGetStaticModuleState())来切换模块线程状态,导致该窗口所在的模块线程状态和MFC调用CWinApp::PreTranslateMessage时的不同,所以DLL中的窗口就无法响应PreTranslateMessage函数了。
解决方案:
1.dll导出一条函数 DllPreTranslateMessage
BOOL PASCAL DllPreTranslateMessage(MSG *pMsg)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return theApp.PreTranslateMessage(pMsg);
}
2.在主程序的CWinApp的PreTranslateMessage中直接调用DLL的DllPreTranslateMessage函数。但记住要先调用DLL中的函数。
BOOL CMyApp::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(DllPreTranslateMessage(pMsg))
return TRUE;
return CWinApp::PreTranslateMessage(pMsg);
}
经过以上两步,DLL中的窗口就可以响应PreTranslateMessage了。
温馨提示:答案为网友推荐,仅供参考