IWebBrowser2* GetIEFromHWnd(HWND hIEWindow) { HWND hWnd ; if(hIEWindow==NULL){ hWnd= FindWindow("IEFrame", NULL); if(hWnd==NULL) hWnd= FindWindow("CabinetWClass", NULL); if( hWnd == NULL){ MessageBox (NULL,"No Running instance of Internet Explorer!","message", MB_OK); } // walk Shell DocObject View->Internet Explorer_Server HWND hWndChild = FindWindowEx(hWnd, 0, "Shell DocObject View", NULL); if(hWndChild !=0){ hWndChild = FindWindowEx(hWndChild, 0, "Internet Explorer_Server", NULL); } hWnd=hWndChild; } else{ hWnd=hIEWindow; } // 我们需要显示地装载OLEACC.DLL,这样我们才知道有没有安装MSAA HINSTANCE hInst = LoadLibrary( _T("OLEACC.DLL") ); IWebBrowser2* pWebBrowser2=NULL; if ( hInst != NULL ){ if ( hWnd != NULL ){ LRESULT lRes; UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") ); ::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") ); if ( pfObjectFromLresult != NULL ){ HRESULT hr; CComPtr<IHTMLDocument2>spDoc; hr=pfObjectFromLresult(lRes,IID_IHTMLDocument2,0,(void**)&spDoc); if ( SUCCEEDED(hr) ){ CComPtr<IHTMLWindow2>spWnd2; CComPtr<IServiceProvider>spServiceProv; hr=spDoc->get_parentWindow ((IHTMLWindow2**)&spWnd2); if(SUCCEEDED(hr)){ hr=spWnd2->QueryInterface (IID_IServiceProvider,(void**)&spServiceProv); if(SUCCEEDED(hr)){ hr = spServiceProv->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (void**)&pWebBrowser2);
} } }
} } ::FreeLibrary(hInst); } else{//如果没有安装MSAA MessageBox(NULL,_T("Please Install Microsoft Active Accessibility"),"Error",MB_OK); } return pWebBrowser2; }
|