atl 中modaldialogbox實現的CDialogImpl 是沒有PreTranslateMessage的,就算增加對應實現也是沒有作用的
原因:PreTranslateMessage是框架部分的代碼並非系統部分modaldialogbox的獨立消息循環並不能調用PreTranslateMessage
解決方法:設置系統鉤子捕獲消息
static HHOOK g_hHook_GetMessage = 0;
static DWORD g_hHook_GetMessage_TargetThreadId = 0;
BOOL g_PreTranslateMessage(MSG* pMsg)
{
if (g_pMessageLoop)
{
return g_pMessageLoop->PreTranslateMessage(pMsg);
}
return FALSE;
}
static LRESULT CALLBACK s_MessageProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode < 0)
{
return CallNextHookEx(g_hHook_GetMessage, nCode, wParam, lParam);
}
if(
MSGF_DIALOGBOX == nCode ||
MSGF_MENU == nCode ||
MSGF_SCROLLBAR == nCode
)
{
MSG* pMsg = (MSG*)lParam;
if (pMsg)
{
if (g_PreTranslateMessage(pMsg))
{
return TRUE;
}
}
}
return CallNextHookEx(g_hHook_GetMessage, nCode, wParam, lParam);
}
void InstallPreTranslateMessage(HINSTANCE hInstance,DWORD dwThreadId)
{
//HHOOK SetWindowsHookEx(int idHook,
// HOOKPROC lpfn,
// HINSTANCE hMod,
// DWORD dwThreadId
// );
g_hHook_GetMessage = ::SetWindowsHookEx(WH_MSGFILTER, s_MessageProc, hInstance, dwThreadId);
}