用chtmlview打开的带框架的网页,如何取得框架中所有包含的网页的html源文件?

解决方案 »

  1.   

    QueryInterface IWebBrowser2
    see http://support.microsoft.com/kb/q271868/
      

  2.   

    我根据上面的资料写了下面的函数,
    BOOL CHtmlView::GetFrameSource( IDispatch *pDisp, CString& refString )
    {
        BOOL bRetVal = FALSE;
        HRESULT hr = S_OK;
        if ( pDisp != NULL )
        {
            CComQIPtr<IWebBrowser2> pBrowser = pDisp;
            if ( pBrowser != NULL )
            {
                IDispatch *pDispDoc = NULL;
                hr = pBrowser->get_Document(&pDispDoc);
                if ( !( FAILED(hr) || !pDisp ) ) 
                {
                    CComQIPtr<IPersistStreamInit> pStmInit = pDisp;
                    if ( !pStmInit )
                    {
                        HGLOBAL hMemory;
                        hMemory = GlobalAlloc(GMEM_MOVEABLE, 0);
                        if (hMemory != NULL)
                        {
                            CComQIPtr<IPersistStreamInit> spPSI = pDispDoc;
                            if( spPSI != NULL)
                            {
                                CComPtr<IStream> spStream;
                                if (SUCCEEDED(CreateStreamOnHGlobal(hMemory, TRUE, &spStream)))
                                {
                                    spPSI->Save(spStream, FALSE);
                                    LPCTSTR pstr = (LPCTSTR) GlobalLock(hMemory);
                                    if (pstr != NULL)
                                    {
                                        // Stream is always ANSI, but CString
                                        // assignment operator will convert implicitly.
                                        bRetVal = TRUE;
                                        TRY
                                        {                       
                                            refString = pstr;
                                        }
                                        CATCH_ALL(e)
                                        {
                                            bRetVal = FALSE;
                                            DELETE_EXCEPTION(e);
                                        }
                                        END_CATCH_ALL                                        if(bRetVal == FALSE)
                                                GlobalFree(hMemory);
                                            else
                                                GlobalUnlock(hMemory);
                                    }
                                }
                            }
                        }
                    }
                }
                RELEASE(pDispDoc);
            }
        }
        return bRetVal;
    }但是只能用在DocumentComplete里,因为我不知道怎么获得pDisp,好像只有在DocumentComplete里才能获得框架各页的pDisp,这样没有通用性,我想要实现的是像GetSource一样的功能,就是只要网页下载完毕,就可以在任意时刻获得框架html源文件,而不是只能在DocumentComplete里
    最好能直接告诉我该怎么做,不要让我在一堆资料里自己找
      

  3.   

    终于成了
    下面是代码,老大帮我看看有没有什么错,还有什么需要改进的地方。
    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)
                                                    {
                                                        // Stream is always ANSI, but CString
                                                        // assignment operator will convert implicitly.
                                                        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;
    }