webbrowser无法响应快捷键、按键消息

 

添加到对话框里的webbrowser无法响应键盘的按键消息,如:回车、快捷键、tab等

 

网上找到了个简单的方法:http://www.codeproject.com/KB/wtl/wtl4mfc6.aspx

 

方法在“Keyboard Handling”这一节,翻译过来就是:

 

问题原因:对话框使用IsDialogMessage来处理了所有的键盘消息,这样webbrowser就无法得到响应的键盘消息了

 

解决方法:在对话框的PreTranslateMessage里发送WM_FORWARDMSG消息,普通的窗口因无法识别这个消息而自动忽略,但是获得焦点(has the focus)的ActiveX控件能正确识别这个消息并看看是否要对这个消息进行处理

 

代码:

  1. BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)  
  2. {  
  3.     // This was stolen from an SDI app using a form view.  
  4.     //   
  5.     // Pass keyboard messages along to the child window that has the focus.  
  6.     // When the browser has the focus, the message is passed to its containing  
  7.     // CAxWindow, which lets the control handle the message.  
  8.     if((pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST) &&  
  9.        (pMsg->message < WM_MOUSEFIRST || pMsg->message > WM_MOUSELAST))  
  10.         return FALSE;  
  11.     HWND hWndCtl = ::GetFocus();  
  12.       
  13.     if(IsChild(hWndCtl))  
  14.         {  
  15.         // find a direct child of the dialog from the window that has focus  
  16.         while(::GetParent(hWndCtl) != m_hWnd)  
  17.             hWndCtl = ::GetParent(hWndCtl);  
  18.         // give control a chance to translate this message  
  19.         if(::SendMessage(hWndCtl, WM_FORWARDMSG, 0, (LPARAM)pMsg) != 0)  
  20.             return TRUE;  
  21.         }  
  22.     // A normal control has the focus, so call IsDialogMessage() so that  
  23.     // the dialog shortcut keys work (TAB, etc.)  
  24.     return IsDialogMessage(pMsg);  
  25. }  
 

 

这个是wtl代码,mfc的应该也差不多

添加这个代码后webbrowser里的回车、tab、快捷键都能正常响应了

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值