IWebBrowser2控件快捷键,DEL,TAB等无效的解决办法

第一种办法:

在普通情况下,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);
}
if (g_WebBrowser)
        {
            CComPtr<IDispatch> disp;
            HRESULT hr = g_WebBrowser->get_Document(&disp);
           
            if (S_OK == hr)
            {
                CComQIPtr<IOleInPlaceActiveObject> spInPlace;

                hr = disp->QueryInterface(__uuidof(IOleInPlaceActiveObject), (void**)&spInPlace);
                if (S_OK == hr)
                {
                    if (spInPlace)
                    {
                        spInPlace->TranslateAccelerator(&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、快捷键都能正常响应了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值