您的位置:首页 > Web前端 > HTML

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

2006-06-19 17:54 127 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: