请问mfc组件中如何使PreTranslateMessage函数有作用,望给个明确易懂的步骤

dll组件:
BOOL CDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message==WM_KEYDOWN)
{
MessageBox("您按下了rt");
}
return CDialog::PreTranslateMessage(pMsg);
}
如上面,我在程序中添着,没反应,在exe程序中可以的,希望给个方法,理论我不大懂

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了。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-02-12
一般DLL中,使用 WindowProc
用法和PreTranslateMessage基本一样,试试吧。

另外,菜鸟大道期待你的加入哦。
菜鸟大道是专为新人入门而开设的~~~