从HWnd得到IWebbrowser2接口

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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值