{ 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 - 百度快照
#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 // 窗口的句柄,并用以上代码测试成功 } |
http://hi.baidu.com/ben2000/blog/item/ebb8d61fad145c64f724e447.html
| ||||||
代码很少,自己看 #include <mshtml.h> BOOL CALLBACK EnumChildProc(HWND hwnd,LPARAM lParam) ::GetClassName( hwnd, (LPTSTR)&buf, 100 ); { //You can store the interface pointer in a member variable // Explicitly load MSAA so we know if it's installed LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") );
作者Blog:
http://blog.csdn.net/111222/
相关文章
|
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