根据Internet Explorer_Server窗口得到IHtmlDocument2接口

王朝网络·vc·作者佚名  2006-01-08
宽屏版  字体: |||超大  

代码很少,自己看

#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();

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有