第一种办法:
在普通情况下,IWebBrowser2控件加入到窗口中时,浏览器的快捷键,包括DEL,TAB等特殊键都不能使用。这个不是控件的Bug,而是一种特性。要让控件处理这些按键,需要通过webBrowser插件的IOleInPlaceActiveObject来处理。
具体操作是在主窗口的事件循环里处理。步骤如下:
1
2
3
4
5
6
7
8
9
10
11
|
//窗口主循环线程
while
(bRet=GetMessage(&msg,NULL,0,0)!=0){
if
(bRet==-1)
break
;
TranslateMessage(&msg);
if
(IsDialogMessage(WebBrowser2控件窗口句柄,&msg)){
//如果是控件窗口的消息
QueryInterface取得IWebBrowser2的IOleInPlaceActiveObject
调用IOleInPlaceActiveObject的TranslateAccelerator函数,参数是&msg
}
else
DispatchMessage(&msg);
}
第二种办法
添加到对话框里的webbrowser无法响应键盘的按键消息,如:回车、快捷键、tab等
网上找到了个简单的方法:http://www.codeproject.com/KB/wtl/wtl4mfc6.aspx 方法在“Keyboard Handling”这一节,翻译过来就是: 问题原因:对话框使用IsDialogMessage来处理了所有的键盘消息,这样webbrowser就无法得到响应的键盘消息了 解决方法:在对话框的PreTranslateMessage里发送WM_FORWARDMSG消息,普通的窗口因无法识别这个消息而自动忽略,但是获得焦点(has the focus)的ActiveX控件能正确识别这个消息并看看是否要对这个消息进行处理 代码: view plaincopy to clipboardprint? 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、快捷键都能正常响应了 |