这个是网页的url:http://bbs.club.sohu.com/r-zz0580-225928-0-0-0.html取得页面的IwebBrowser接口后,我对页面进行了iframe的检测:
代码如下://function:若网页中有iframe,找到属于主要内容的iframe,并从相关联的document里渠道body指针;
void GetBody( CComQIPtr<IHTMLDocument2> pDoc, CComQIPtr< IHTMLElement > &pBody )
{
CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
pDoc->get_frames( &spFramesCollection2 );
bool flag = FALSE;
//取得子框架个数
long nn = 0;
HRESULT hr = spFramesCollection2->get_length( &nn );
int maxBodyArea = 0;
map< long, CComQIPtr<IHTMLElement> >BodyMap;
if ( !FAILED ( hr ) || nn )
{
for( int nni = 0; nni < nn; nni++ )
{
CComVariant vDispWin2; //取得子框架的自动化接口
hr = spFramesCollection2->item( &CComVariant( nni ), &vDispWin2 );
if ( FAILED ( hr ) ) continue;
CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
if( !spWin2 ) continue; //取得子框架的 IHTMLWindow2 接口
CComPtr < IHTMLDocument2 > spDoc2;
spWin2->get_document( &spDoc2 ); //取得子框架的 IHTMLDocument2 接口
if( !spDoc2 ) continue;
CComQIPtr< IHTMLElement >pBodys;
spDoc2->get_body( &pBodys );
CComBSTR text;
pBodys->get_innerText( &text );
CString strText = ( char* )( _bstr_t )text;
if( !getStrLenth( strText ) ) continue;
flag = true;
CComQIPtr< IHTMLElement2 >pBodyElem2( pBodys );
long bh = 0;
long bw = 0;
pBodyElem2->get_scrollHeight( &bh );
pBodyElem2->get_scrollWidth( &bw );
BodyMap.insert( map< long, CComQIPtr<IHTMLElement> >::value_type(bh*bw, pBodys) );
if( maxBodyArea < bh*bw ) maxBodyArea = bh*bw;
}
}
if( flag )
{
map< long, CComQIPtr<IHTMLElement> >::iterator iBodyMap = BodyMap.find( maxBodyArea );
CComQIPtr<IHTMLElement>pElem;
pDoc->get_body( &pElem );
CComQIPtr<IHTMLElement2>pElem2( pElem );
long eh = 0;
long ew = 0;
pElem2->get_scrollHeight( &eh );
pElem2->get_scrollWidth( &ew );
if( eh*ew < maxBodyArea )
pBody = iBodyMap->second;
else
pBody = pElem;
}
else
pDoc->get_body( &pBody );
}
这个函数执行过程中可以看到它里面没有和document关联的iframe;
所以我直接从IWebBrowser取到document2,得到body元素接口指针得到body指针后,取高度和宽度:
CComQIPtr<IHTMLElement2>pBody2( pBody )
pBody2->get_scrollHeight( &heightscroll );
pBody2->get_scrollWidth( &widthscroll );但是得到的高度是244;而页面的实际高度目测也不可能是这么短,之后使用offsetHeight,clientHeight,getBoundingClientRect()取高度,均为244;
哪哪位高手指点下?
代码如下://function:若网页中有iframe,找到属于主要内容的iframe,并从相关联的document里渠道body指针;
void GetBody( CComQIPtr<IHTMLDocument2> pDoc, CComQIPtr< IHTMLElement > &pBody )
{
CComPtr< IHTMLFramesCollection2 > spFramesCollection2;
pDoc->get_frames( &spFramesCollection2 );
bool flag = FALSE;
//取得子框架个数
long nn = 0;
HRESULT hr = spFramesCollection2->get_length( &nn );
int maxBodyArea = 0;
map< long, CComQIPtr<IHTMLElement> >BodyMap;
if ( !FAILED ( hr ) || nn )
{
for( int nni = 0; nni < nn; nni++ )
{
CComVariant vDispWin2; //取得子框架的自动化接口
hr = spFramesCollection2->item( &CComVariant( nni ), &vDispWin2 );
if ( FAILED ( hr ) ) continue;
CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal;
if( !spWin2 ) continue; //取得子框架的 IHTMLWindow2 接口
CComPtr < IHTMLDocument2 > spDoc2;
spWin2->get_document( &spDoc2 ); //取得子框架的 IHTMLDocument2 接口
if( !spDoc2 ) continue;
CComQIPtr< IHTMLElement >pBodys;
spDoc2->get_body( &pBodys );
CComBSTR text;
pBodys->get_innerText( &text );
CString strText = ( char* )( _bstr_t )text;
if( !getStrLenth( strText ) ) continue;
flag = true;
CComQIPtr< IHTMLElement2 >pBodyElem2( pBodys );
long bh = 0;
long bw = 0;
pBodyElem2->get_scrollHeight( &bh );
pBodyElem2->get_scrollWidth( &bw );
BodyMap.insert( map< long, CComQIPtr<IHTMLElement> >::value_type(bh*bw, pBodys) );
if( maxBodyArea < bh*bw ) maxBodyArea = bh*bw;
}
}
if( flag )
{
map< long, CComQIPtr<IHTMLElement> >::iterator iBodyMap = BodyMap.find( maxBodyArea );
CComQIPtr<IHTMLElement>pElem;
pDoc->get_body( &pElem );
CComQIPtr<IHTMLElement2>pElem2( pElem );
long eh = 0;
long ew = 0;
pElem2->get_scrollHeight( &eh );
pElem2->get_scrollWidth( &ew );
if( eh*ew < maxBodyArea )
pBody = iBodyMap->second;
else
pBody = pElem;
}
else
pDoc->get_body( &pBody );
}
这个函数执行过程中可以看到它里面没有和document关联的iframe;
所以我直接从IWebBrowser取到document2,得到body元素接口指针得到body指针后,取高度和宽度:
CComQIPtr<IHTMLElement2>pBody2( pBody )
pBody2->get_scrollHeight( &heightscroll );
pBody2->get_scrollWidth( &widthscroll );但是得到的高度是244;而页面的实际高度目测也不可能是这么短,之后使用offsetHeight,clientHeight,getBoundingClientRect()取高度,均为244;
哪哪位高手指点下?
解决方案 »
- CString 如何转 char[]
- 求助:在VB里调用VC写的COM的DLL
- 如何通过计算机名获得IP
- 大家来讨论一下,关于子线程和主线程的终止运行的先后顺序!
- 有谁知道CBitmap类中SetBitmapBits(DWORD dwCount,const void* lpBits )参数lpBits 具体要求什么格式?
- <<<<<关于网络通信方面的几点疑难杂症,请大家讨论讨论>>>>> (有收获后加至高分)
- 如何传递一个数组
- HELP!16进制转10进制
- 请大家帮忙De一下bug, 关于Mouse Hook的。(WIN32)
- 为什麽???
- 一个CArchieve可以同时连一个CSocketFile和一个CFile吗?
- 用ADO访问SQL遇到的问题
IHTMLTextContainer::put_scrollTop() to scroll up or down
CSize CMfcieView::GetPageSize()
{
// get the document dispatch from browser
LPDISPATCH pDisp = GetHtmlDocument();
ASSERT(pDisp);
// get IHTMLDocument2 interface
IHTMLDocument2 *pDocument = NULL;
HRESULT hresult = pDisp->QueryInterface( IID_IHTMLDocument2, (void**)&pDocument );
ASSERT(SUCCEEDED(hresult));
ASSERT(pDocument);
// get IHTMLElement interface
IHTMLElement *pBody = NULL;
hresult = pDocument->get_body( &pBody );
ASSERT(SUCCEEDED(hresult));
ASSERT(pBody);
// from this we can get IHTMLTextContainer interface, which allows us to do scrolling
IHTMLTextContainer *pElement = NULL;
hresult = pBody->QueryInterface(IID_IHTMLTextContainer,(void**)&pElement);
ASSERT(SUCCEEDED(hresult));
ASSERT(pElement);
// try to get the whole page size - but the returned number is not always correct.
// especially with pages that use dynamic html tricks...
long scrollWidth, scrollHeight;
pElement->get_scrollHeight( &scrollHeight );
pElement->get_scrollWidth( &scrollWidth );
CSize scrollSize(scrollWidth-16, scrollHeight-16);
long real_scrollHeight;
pElement->put_scrollTop( 20000000 ); // ask to scroll really far down...
pElement->get_scrollTop( &real_scrollHeight );
// get current screen height
CRect clientRect;
GetClientRect(clientRect);
CalcWindowRect(&clientRect, CWnd::adjustBorder);
real_scrollHeight += clientRect.Height()-16;
scrollHeight = %d\n Width = %d, Height = %d ",scrollWidth, scrollHeight, clientRect.Width(), clientRect.Height());
return scrollSize;
}