从HWnd得到IWebbrowser2接口

程序代码
IWebBrowser2[color=#0000ff]* 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 ;
}[/color]
 

www.zxboy.com/article.asp?id=100 43K 2008-10-23 - 百度快照




通过 IHTMLDocument2 接口操作网页
2008年11月23日 星期日 下午 10:08
#include <mshtml.h>
#include <oleacc.h>
#include "atl//atlbase.h"
// 使用BCB中自带的oleacc.lib,也可以从IDE中添加:Project-->Add to Project
// 注意这里用的是我的机器上的路径
#pragma link "D://Program Files//Borland//CBuilder6//Lib//Psdk//oleacc.lib"

void __fastcall TForm1::Button1Click(TObject *Sender)
{
// 这里直接指定一个句柄,实际应用中可以通过各种方式获取句柄
HWND hWnd = (void *)0x001402B6;

IHTMLDocument2* spDoc2 = NULL;
DWORD dwRes;

UINT nMsg = ::RegisterWindowMessage("WM_HTML_GETOBJECT");
::SendMessageTimeout(hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, &dwRes);

// 因为使用了静态的lib,不需要再GetProcAddress取函数地址了
HRESULT hr = ObjectFromLresult(dwRes, IID_IHTMLDocument, 0, (void **)&spDoc2);
if(SUCCEEDED(hr))
{
IDispatch *pDisp;
IHTMLWindow2 *pWin;
spDoc2->get_Script(&pDisp);
pDisp->QueryInterface(IID_IHTMLWindow2, (void **)&pWin);
pDisp->Release();
pWin->get_document(&spDoc2);
pWin->Release();

// 将页面的背景设为蓝色
VARIANT vColor;
vColor.vt = VT_BSTR;
vColor.bstrVal = WideString("blue").c_bstr();
spDoc2->put_bgColor(vColor);
}
// 以上代码在用IE打开一个网页以后,用Spy4Win取得Internet Explorer_Server
// 窗口的句柄,并用以上代码测试成功
}

类别:c++builder | 添加到搜藏 | 浏览( 20) | 评论 (0) <script language="javascript"> /* 

http://hi.baidu.com/ben2000/blog/item/ebb8d61fad145c64f724e447.html




根据Internet Explorer_Server窗口得到IHtmlDocument2接口     选择自 111222 的 Blog
关键字  根据Internet Explorer_Server窗口得到IHtmlDocument2接口/IE/WebBrowser
出处 

代码很少,自己看

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

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
 TCHAR buf[100];

 ::GetClassName( hwnd, (LPTSTR)&buf, 100 );
 if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 ) /

{
  *(HWND*)lParam = hwnd;
  return FALSE;
 }
 else
  return TRUE;
};

//You can store the interface pointer in a member variable
//for easier access
void CDlg::OnGetDocInterface(HWND hWnd)
{
 CoInitialize( NULL );

 // Explicitly load MSAA so we know if it's installed
 HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
 if ( hInst != NULL )
 {
  if ( hWnd != NULL )
  {
   HWND hWndChild=NULL;
   // Get 1st document window
   ::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );
   if ( hWndChild )
   {
    CComPtr<IHTMLDocument2> spDoc;
    LRESULT lRes;
   
    UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
    ::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

    LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
    if ( pfObjectFromLresult != NULL )
    {
     HRESULT hr;
     hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
     if ( SUCCEEDED(hr) )
     {
      CComPtr<IDispatch> spDisp;
      CComQIPtr<IHTMLWindow2> spWin;
      spDoc->get_Script( &spDisp );
      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();
}


相关文章


http://dev.csdn.net/article/11/11722.shtm


根据Internet Explorer_Server窗口得到IHtmlDocument2接口:程序设计,VC 专栏


代码很少,自己看

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

BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
{
 TCHAR buf[100];

 ::GetClassName( hwnd, (LPTSTR)&buf, 100 );
 if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 ) 

{
  *(HWND*)lParam = hwnd;
  return FALSE;
 }
 else
  return TRUE;
};

//You can store the interface pointer in a member variable
//for easier access
void CDlg::OnGetDocInterface(HWND hWnd)
{
 CoInitialize( NULL );

 // Explicitly load MSAA so we know if it's installed
 HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
 if ( hInst != NULL )
 {
  if ( hWnd != NULL )
  {
   HWND hWndChild=NULL;
   // Get 1st document window
   ::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );
   if ( hWndChild )
   {
    CComPtr<IHTMLDocument2> spDoc;
    LRESULT lRes;
   
    UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
    ::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );

    LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
    if ( pfObjectFromLresult != NULL )
    {
     HRESULT hr;
     hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
     if ( SUCCEEDED(hr) )
     {
      CComPtr<IDispatch> spDisp;
      CComQIPtr<IHTMLWindow2> spWin;
      spDoc->get_Script( &spDisp );
      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();
}



http://www.pujiwang.com/Html/VisualC++/2006-1/6/0106841.html


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值