從一個窗口句柄獲取IWebBrowser2和IHTMLDocument2接口

本文提供了2個接口。


測試例子:調用之前請確保打開IE瀏覽器,運行程序,點擊第一按鈕,可以改變網頁背景色,點擊第二個按鈕可以讓網頁轉到百度首頁

已將源代碼上傳:免積分下載地址 http://download.csdn.net/detail/moonshine99/4799948

源代碼是VC6的開發環境,這個接口vs2008上也測過,可以用

/****************************************************************************
尋找指定類名的子窗口句柄 
****************************************************************************/
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();
}


/****************************************************************************
//調用測試,測試的時候360會攔截2012年11月測的,先關閉360,

打開IE瀏覽器IE6和IE8都測過,能改變背景色,有些網頁寫了保護改變不了,

試過百度新聞,百度知道是可以改變的,沒有研究網頁的CSS之類的兼容。。
****************************************************************************/
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,從而獲取IServiceProvider接口。

IServiceProvider是IWebBrowser2, IDocument2等公共的方法,請重視這個接口
 
必須包含的頭文件
#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.baidu.com/";
   VARIANT vEmpty;
   VariantInit(&vEmpty);
   pWebBrowser2->Navigate(bsSite, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
   
   //獲取窗口
   HWND wnd;
   pWebBrowser2->get_HWND((LONG*)(&wnd));
   
   pWebBrowser2->Release();
  }
 }
}


調用之前請確保打開IE瀏覽器

已經源代碼上傳:免積分下載地址 http://download.csdn.net/detail/moonshine99/4799948

源代碼是VC6的開發環境,這個接口vs2008上也測過,可以用



還有一個例子:

通過COM操作IE瀏覽器的一個類,在程序中

加入類即可直接通過類操作IE

http://download.csdn.net/detail/moonshine99/4807289

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章