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;
}