从一个窗口句柄获取IWebBrowser2和IHTMLDocument2接口

将以下代码张贴到对话框程序,运行即可。调用之前请确保打开IE浏览器

/****************************************************************************
寻找指定类名的子窗口句柄 
****************************************************************************/
HWND FindWithClassName(HWND ParentWnd,TCHAR* FindClassName)
{
 HWND hChild = ::GetWindow(ParentWnd, GW_CHILD);

 for(; hChild!=NULL ; hChild=::GetWindow(hChild,GW_HWNDNEXT))
 {
  TCHAR ClassName[100]={0};
  ::GetClassName(hChild,ClassName,sizeof(ClassName)/sizeof(TCHAR));

  if (_tcscmp(ClassName,FindClassName)==0)
   return hChild;
  
  HWND FindWnd=FindWithClassName(hChild,FindClassName);
  if (FindWnd)
   return FindWnd;
 }
 return NULL;
}


/****************************************************************************
从一个窗口句柄获取IHTMLDocument2接口
使用完后要调用Release
如果找不到接口,返回NULL

原理:
如果你的系统安装了Microsoft 活动辅助功能(MSAA),则您可以向浏览器窗口
(类名"Internet Explorer_Server")发送WM_HTML_GETOBJECT消息,将消息返回的结果
作为一个参数传递给MSAA函数ObjectFromLresult,从而获取IHTMLDocument2 接口。
 
必须包含的头文件
#include <mshtml.h> 
#include <oleacc.h> 
#include <atlbase.h>  //需要安装ATL库
****************************************************************************/

#include <mshtml.h> 
#include <oleacc.h> 
#include <atlbase.h>

//You can store the interface pointer in a member variable 
//for easier access
void GetIHTMLDocument2Interface(HWND BrowserWnd) 
{
 CoInitialize(NULL);
 
 HRESULT hr;

 // Explicitly load MSAA so we know if it's installed
 HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
 if ( hInst )
 {
  LRESULT lRes; //SendMessageTimeout后的返回值,用于函数pfObjectFromLresult的第1个参数
  UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
  ::SendMessageTimeout( BrowserWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
  
  //获取函数pfObjectFromLresult
  LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
  if ( pfObjectFromLresult  )
  {
   CComPtr<IHTMLDocument2> spDoc;
   hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
   if ( SUCCEEDED(hr) )
   {
    //获取文档接口
    CComPtr<IDispatch> spDisp;
    spDoc->get_Script( &spDisp );
    CComQIPtr<IHTMLWindow2> spWin=spDisp;
    spWin->get_document( &spDoc.p );

   //  Change background color to red
    spDoc->put_bgColor( CComVariant("red") );

   } // else document not ready
  } // else Internet Explorer is not running
  ::FreeLibrary( hInst );
 } // else Active Accessibility is not installed
 
 CoUninitialize();
}


/****************************************************************************
//调用测试
****************************************************************************/
void CDemoDlg::OnButton1() 
{
 //获取IE主窗口
 HWND ExplorerWnd=::FindWindow(_T("IEFrame"),NULL); 
 if (!ExplorerWnd)
  ::MessageBox(m_hWnd,TEXT("没有找打IE窗口"),NULL,MB_OK);
 ::SetForegroundWindow(ExplorerWnd);

 //根据IE主窗口获取浏览器窗口
 HWND BrowserWnd=FindWithClassName( ExplorerWnd , _T("Internet Explorer_Server"));
 if ( BrowserWnd )
 {
  GetIHTMLDocument2Interface(BrowserWnd);
 }
}

 

/****************************************************************************
如何从一个窗口句柄获取IWebBrowser2接口
使用完后要调用Release
如果找不到接口,返回NULL

原理:
如果你的系统安装了Microsoft 活动辅助功能(MSAA),则您可以向浏览器窗口
(类名"Internet Explorer_Server")发送WM_HTML_GETOBJECT消息,将消息返回的结果
作为一个参数传递给MSAA函数ObjectFromLresult,从而获取IHTMLDocument2 接口。
 
必须包含的头文件
#include <mshtml.h> 
#include <oleacc.h> 
#include <atlbase.h>  //需要安装ATL库
****************************************************************************/

#include <mshtml.h> 
#include <oleacc.h> 
#include <atlbase.h>  //需要安装ATL库

//测试代码中的_bstr_t 需要使用COMUTIL.H>
#include <COMUTIL.H>
#pragma comment(lib,"comsupp.lib")

IWebBrowser2* GetIWebBrowserInterface(HWND BrowserWnd) 
{
 CoInitialize(NULL);
 
 IWebBrowser2* pWebBrowser2=NULL;
 HRESULT hr;

 // Explicitly load MSAA so we know if it's installed
 HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
 if ( hInst )
 {
  LRESULT lRes;
  UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
  ::SendMessageTimeout( BrowserWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
  
  LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
  if ( pfObjectFromLresult  )
  {
   CComPtr<IServiceProvider> spServiceProv;
   hr = (*pfObjectFromLresult)( lRes, IID_IServiceProvider, 0, (void**)&spServiceProv );
   if ( SUCCEEDED(hr) )
   {
    hr = spServiceProv->QueryService(SID_SWebBrowserApp,
     IID_IWebBrowser2,(void**)&pWebBrowser2);
    
   } // else document not ready
  } // else Internet Explorer is not running
  ::FreeLibrary( hInst );
 } // else Active Accessibility is not installed
 
 CoUninitialize();
 
 return SUCCEEDED(hr) ? pWebBrowser2 : NULL;
}

/****************************************************************************
//调用测试
****************************************************************************/
void CDemoDlg::OnButton2() 
{
 //获取IE主窗口
 HWND ExplorerWnd=::FindWindow(_T("IEFrame"),NULL); 
 if (!ExplorerWnd)
  ::MessageBox(m_hWnd,TEXT("没有找打IE窗口"),NULL,MB_OK);
 ::SetForegroundWindow(ExplorerWnd);

 //根据IE主窗口获取浏览器窗口
 HWND BrowserWnd=FindWithClassName( ExplorerWnd , _T("Internet Explorer_Server"));
 if ( BrowserWnd )
 {
  IWebBrowser2* pWebBrowser2=GetIWebBrowserInterface(BrowserWnd);
  if (pWebBrowser2)
  {
   //浏览网页
   _bstr_t bsSite= "http://www.shilehui.com/";
   VARIANT vEmpty;
   VariantInit(&vEmpty);
   pWebBrowser2->Navigate(bsSite, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
   
   //获取窗口
   HWND wnd;
   pWebBrowser2->get_HWND((LONG*)(&wnd));
   
   pWebBrowser2->Release();
  }
 }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值