关闭

CHtmlView读取带框架的网页的函数。

743人阅读 评论(0) 收藏 举报

 BOOL GetFrameSource( CString& refString, long frameIndex =0)

frameIndex 为框架集的序号

0 代表主框架,如果frameIndex 大于总的框架数则返回FALSE

BOOL CHtmlView::GetFrameSource( CString& refString, long frameIndex )
{
 BOOL bRetVal = FALSE;
 if( frameIndex == 0 )
 {
  bRetVal = GetSource( refString );
 }
 else if( frameIndex > 0 )
 {
  frameIndex = frameIndex - 1;
  CComQIPtr<IDispatch> pDisp = GetHtmlDocument();

  if ( pDisp != NULL )
  {
   CComQIPtr<IHTMLDocument2> pHtmlDoc = pDisp;

   if ( pHtmlDoc != NULL )
   {
    CComQIPtr<IHTMLFramesCollection2> pFrames;
    pHtmlDoc->get_frames( &pFrames );

    if ( pFrames != NULL )
    {
     long frameCount = 0;
     pFrames->get_length( &frameCount );

     if ( frameCount > frameIndex )
     {
      COleVariant varIndex( frameIndex, VT_I4 );
      COleVariant varpDisp;
      pFrames->item( varIndex, varpDisp );
      CComQIPtr<IDispatch> pDispDoc = varpDisp.pdispVal;

      if( pDispDoc != NULL )
      {
       CComQIPtr<IHTMLWindow2> pHtmlWnd = pDispDoc;

       if( pHtmlWnd != NULL )
       {
        CComQIPtr<IHTMLDocument2> pHtmlDocf;
        pHtmlWnd->get_document( &pHtmlDocf );

        if( pHtmlDocf != NULL )
        {
         HGLOBAL hMemory;
         hMemory = GlobalAlloc( GMEM_MOVEABLE, 0);

         if (hMemory != NULL)
         {
          CComQIPtr<IPersistStreamInit> spPSI = pHtmlDocf;

          if( spPSI != NULL)
          {
           CComPtr<IStream> spStream;
           if ( SUCCEEDED( CreateStreamOnHGlobal(hMemory, TRUE, &spStream) ) )
           {
            spPSI->Save( spStream, FALSE );
            LPCTSTR pstr = (LPCTSTR) GlobalLock( hMemory );
            if ( pstr != NULL )
            {
             bRetVal = TRUE;
             TRY
             {                      
              refString = pstr;
             }
             CATCH_ALL(e)
             {
              bRetVal = FALSE;
              DELETE_EXCEPTION(e);
             }
             END_CATCH_ALL

             if( bRetVal == FALSE )
              GlobalFree( hMemory );
             else
              GlobalUnlock( hMemory );
            }
           }
          }
         }
        }
       }
      }
     }
    }
   }
  }
 }

 return bRetVal;
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:26183次
    • 积分:437
    • 等级:
    • 排名:千里之外
    • 原创:11篇
    • 转载:5篇
    • 译文:0篇
    • 评论:54条
    文章分类
    最新评论