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

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

更新日期:2010-1-10
测试环境:VC6.0+WinXP

━━━━━━━━━━━━━━━━━━━━━━━━

 
将如下代码张贴到对话框程序,运行便可。调用以前请确保打开IE浏览器
愿顺利!阿弥陀佛!php

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

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

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


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

原理:
若是你的系统安装了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 );ui

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

   } // 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();
  }
 }
}

 

推荐:
━━━━━━━━━━━━━━━━━━━━━━━━

谈老师的BLOG (佛教大德)
狮姐的博客(狮子窝,学佛与灵魂之探究)
大方广(学习传统文化)
慈善点击(轻松一点,行善积德,何乐不为)
电影《地球公民》(揭示鲜为人知的一面)  
心向光明 远离邪淫(现世警钟,不可不看)
戒淫(上篇)(正淫节欲,戒除邪淫)
戒淫(中篇)(纵欲之乐,忧患随之)
公民教育——命由我造