根据Internet Explorer_Server窗口得到IHtmlDocument2

  1. #include <mshtml.h>
  2. #include <atlbase.h>
  3. #include <oleacc.h>
  4. BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam)
  5. {
  6.  TCHAR buf[100];
  7.  ::GetClassName( hwnd, (LPTSTR)&buf, 100 );
  8.  if ( _tcscmp( buf, _T("Internet Explorer_Server") ) == 0 ) 
  9. {
  10.   *(HWND*)lParam = hwnd;
  11.   return FALSE;
  12.  }
  13.  else
  14.   return TRUE;
  15. };
  16. //You can store the interface pointer in a member variable 
  17. //for easier access
  18. void CDlg::OnGetDocInterface(HWND hWnd) 
  19. {
  20.  CoInitialize( NULL );
  21.  // Explicitly load MSAA so we know if it's installed
  22.  HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") );
  23.  if ( hInst != NULL )
  24.  {
  25.   if ( hWnd != NULL )
  26.   {
  27.    HWND hWndChild=NULL;
  28.    // Get 1st document window
  29.    ::EnumChildWindows( hWnd, EnumChildProc, (LPARAM)&hWndChild );
  30.    if ( hWndChild )
  31.    {
  32.     CComPtr<IHTMLDocument2> spDoc;
  33.     LRESULT lRes;
  34.    
  35.     UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") );
  36.     ::SendMessageTimeout( hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes );
  37.     LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
  38.     if ( pfObjectFromLresult != NULL )
  39.     {
  40.      HRESULT hr;
  41.      hr = (*pfObjectFromLresult)( lRes, IID_IHTMLDocument, 0, (void**)&spDoc );
  42.      if ( SUCCEEDED(hr) )
  43.      {
  44.       CComPtr<IDispatch> spDisp;
  45.       CComQIPtr<IHTMLWindow2> spWin;
  46.       spDoc->get_Script( &spDisp );
  47.       spWin = spDisp;
  48.       spWin->get_document( &spDoc.p );
  49.       // Change background color to red
  50.       spDoc->put_bgColor( CComVariant("red") );
  51.      }
  52.     }
  53.    } // else document not ready
  54.   } // else Internet Explorer is not running
  55.   ::FreeLibrary( hInst );
  56.  } // else Active Accessibility is not installed
  57.  CoUninitialize();
  58. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值