打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
从一个窗口句柄获取IWebBrowser2和IHTMLDocument2接口
从一个窗口句柄获取IWebBrowser2和IHTMLDocument2接口
分类:VC++ 2011-01-10 15:52 3077人阅读 (0)
internetmicrosoftnullinterface浏览器server
从一个窗口句柄获取IWebBrowser2和IHTMLDocument2接口
更新日期:2010-1-10
测试环境:VC6.0+WinXP
━━━━━━━━━━━━━━━━━━━━━━━━
将以下代码张贴到对话框程序,运行即可。调用之前请确保打开IE浏览器
愿顺利!阿弥陀佛!
/****************************************************************************
寻找指定类名的子窗口句柄
****************************************************************************/
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();
}
/****************************************************************************
//调用测试
****************************************************************************/
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,从而获取IHTMLDocument2 接口。
必须包含的头文件
#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.shilehui.com/";
VARIANT vEmpty;
VariantInit(&vEmpty);
pWebBrowser2->Navigate(bsSite, &vEmpty, &vEmpty, &vEmpty, &vEmpty);
//获取窗口
HWND wnd;
pWebBrowser2->get_HWND((LONG*)(&wnd));
pWebBrowser2->Release();
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
从HWnd得到IWebbrowser2接口
偷窥桌面程序和IE浏览器的密码编辑框
如何获取网页密码框中的密码
Delphi通过IE窗口句柄获取网页接口(IWebBrowser2) good
获取IE (控件)的所有链接(包括Frameset, iframe)
C#中webBrowser加载页面中访问不同域的iFrame引发System.UnauthorizedAccessException异常的解决办法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服