您的位置:首页 > 其它

DLL 内部的非模态对话框的 PreTranslateMessage 函数不执行

2015-08-19 16:12 375 查看
虽然MFC Regular DLL派生了CWinApp类,并有一个theApp全局对象。但它不包含CWinApp::Run机制,主消息由 exe 负责接收、分发。如果DLL 生成了无模式对话框或有自己的主框架窗口,则它应该导出函数来调用PreTranslateMessage。

exe程序需要调用这个导出函数。示例代码如下:

//DLL端需要导出函数,调用AfxGetApp()->PreTranslateMessage
__declspec(dllexport) BOOL DllPreTranslateMessage(MSG* pMsg)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); //切换模块状态
return AfxGetApp()->PreTranslateMessage(pMsg);
}

//exe端需要调用DLL的导出函数
class CTestApp : public CWinApp
{
public:
BOOL PreTranslateMessage(MSG* pMsg)
{
if(DllPreTranslateMessage(pMsg))
{
return TRUE;
}
return CWinApp::PreTranslateMessage(pMsg);
}
...


//对话框 cpp 中处理回车事件
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN)
{
AfxMessage(_T("按下了回车键"));
return TRUE;
}
return CDialog::PreTranslateMessage(pMsg);
}


如果不这么做,则dll内部的非模态对话框PreTranslateMessage函数不会被执行,对话框内按Tab键也无法切换焦点。如果exe不是MFC程序,而是VB6.0、VC#程序,该如何处理PreTranslateMessage?这个问题需要再进行深入研究。此时,非模态对话框内按Tab键无法切换焦点,该如何处理?估计得使用键盘钩子了……
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: