添加到对话框里的webbrowser无法响应键盘的按键消息,如:回车、快捷键、tab等
网上找到了个简单的方法:http://www.codeproject.com/KB/wtl/wtl4mfc6.aspx
方法在“Keyboard Handling”这一节,翻译过来就是:
问题原因:对话框使用IsDialogMessage来处理了所有的键盘消息,这样webbrowser就无法得到响应的键盘消息了
解决方法:在对话框的PreTranslateMessage里发送WM_FORWARDMSG消息,普通的窗口因无法识别这个消息而自动忽略,但是获得焦点(has the focus)的ActiveX控件能正确识别这个消息并看看是否要对这个消息进行处理
代码:
- BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
- {
- // This was stolen from an SDI app using a form view.
- //
- // Pass keyboard messages along to the child window that has the focus.
- // When the browser has the focus, the message is passed to its containing
- // CAxWindow, which lets the control handle the message.
- if((pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST) &&
- (pMsg->message < WM_MOUSEFIRST || pMsg->message > WM_MOUSELAST))
- return FALSE;
- HWND hWndCtl = ::GetFocus();
- if(IsChild(hWndCtl))
- {
- // find a direct child of the dialog from the window that has focus
- while(::GetParent(hWndCtl) != m_hWnd)
- hWndCtl = ::GetParent(hWndCtl);
- // give control a chance to translate this message
- if(::SendMessage(hWndCtl, WM_FORWARDMSG, 0, (LPARAM)pMsg) != 0)
- return TRUE;
- }
- // A normal control has the focus, so call IsDialogMessage() so that
- // the dialog shortcut keys work (TAB, etc.)
- return IsDialogMessage(pMsg);
- }
这个是wtl代码,mfc的应该也差不多
添加这个代码后webbrowser里的回车、tab、快捷键都能正常响应了