在ATL组件编程中,弹出的非模态对话框无法响应某些键盘事件,一种解决办法是使用钩子:
CSDN的帖子:100分求救:Atl组件中键盘消息的响应问题
http://community.csdn.net/Expert/topic/5252/5252728.xml?temp=.8889887
改造后的实际应用:
// 存放非模态的对话框句柄
std::set<HWND> m_setHwnd;
static HHOOK hHook = NULL;
LRESULT FAR PASCAL GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
{
LPMSG lpMsg = (LPMSG) lParam;
if ( nCode >= 0 && PM_REMOVE == wParam )
{
// Don't translate non-input events.
if ( (lpMsg->message != WM_CHAR && lpMsg->message >= WM_KEYFIRST && lpMsg->message <= WM_KEYLAST) )
{
std::set<HWND>::iterator iter = m_setHwnd.begin();
for (; iter != m_setHwnd.end(); iter++) {
HWND hWnd = *iter;
if ( IsDialogMessage(hWnd, lpMsg) )
{
// The value returned from this hookproc is ignored,
// and it cannot be used to tell Windows the message has been handled.
// To avoid further processing, convert the message to WM_NULL
// before returning.
lpMsg->message = WM_NULL;
lpMsg->lParam = 0;
lpMsg->wParam = 0;
}
}
}
}
return CallNextHookEx(hHook, nCode, wParam, lParam);
}
// 钩子的植入
BOOL CXXXApp::InitInstance()
{
AfxEnableControlContainer();
hHook = SetWindowsHookEx( WH_GETMESSAGE, GetMsgProc,
NULL, GetCurrentThreadId() );
return CWinApp::InitInstance();
}
//钩子的卸载
int CXXXApp::ExitInstance()
{
UnhookWindowsHookEx( hHook );
return CWinApp::ExitInstance();
}