这个是网页的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;
哪哪位高手指点下?

解决方案 »

  1.   

    GetPageSize()   is   for   getting   the   length   of   the   webpage,   that   is   neccesory   to   know   how   much   you   want   to   scroll   down.   You   can   use    
      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-&gt;QueryInterface(   IID_IHTMLDocument2,   (void**)&pDocument   );  
      ASSERT(SUCCEEDED(hresult));  
      ASSERT(pDocument);  
       
      //   get   IHTMLElement   interface  
      IHTMLElement   *pBody   =   NULL;  
      hresult   =   pDocument-&gt;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-&gt;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-&gt;get_scrollHeight(   &scrollHeight   );  
      pElement-&gt;get_scrollWidth(   &scrollWidth   );  
      CSize   scrollSize(scrollWidth-16,   scrollHeight-16);  
       
      long   real_scrollHeight;  
      pElement-&gt;put_scrollTop(   20000000   );   //   ask   to   scroll   really   far   down...  
      pElement-&gt;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;  
      }