最近在WIN32项目中用到IWebBrower2接口,发现复制粘贴等快捷键无法使用,后再网上查询到可以再消息循环阶段拦截IE的消息进行处理,代码如下:
1 if (msg.message >= WM_KEYDOWN && msg.message < WM_KEYLAST) { 2 TCHAR szClassName[256] = {0}; 3 ::GetClassName(msg.hwnd, szClassName, 256); 4 if (_tcsicmp(szClassName, _T("Internet Explorer_Server")) == 0) { 5 HMODULE hModule = ::LoadLibrary(_T("OLEACC.DLL")); 6 if (hModule) { 7 LRESULT lRes; 8 UINT nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT")); 9 ::SendMessageTimeout(msg.hwnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes); 10 LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress(hModule, _T("ObjectFromLresult")); 11 if (pfObjectFromLresult) { 12 CComPtr<IServiceProvider> spServiceProv; 13 HRESULT hr = (*pfObjectFromLresult)(lRes, IID_IServiceProvider, 0, (void**)&spServiceProv); 14 if (SUCCEEDED(hr)) { 15 IWebBrowser2* pWebBrowser2 = NULL; 16 hr = spServiceProv->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&pWebBrowser2); 17 IOleInPlaceActiveObject* pOleInPlaceActiveObject = NULL; 18 hr = pWebBrowser2->QueryInterface(IID_IOleInPlaceActiveObject, (LPVOID*)&pOleInPlaceActiveObject); 19 if (hr == S_OK && pOleInPlaceActiveObject) { 20 hr = pOleInPlaceActiveObject->TranslateAccelerator(&msg); 21 DWORD dw = ::GetLastError(); 22 if (hr == S_OK) { 23 continue; 24 } 25 } 26 } 27 } 28 ::FreeLibrary( hModule ); 29 } 30 } 31 }