我在网上找了好久,不是看不懂,就是找不到,哎.好笨!下边是问题:问题:使用Cwebbrowser2打开网页以后,用什么函数可以得到当前程序中使用Cwebbrowser2打开网页的HTML源代码(就是和IE中打开[查看]->[源文件]一样的代码)?希望可以贴上来具体实现的VC++代码和详细注解!不明白问题的可以仔细问我下,我可以说的更详细的,谢谢大家帮忙了哦!(50分)

解决方案 »

  1.   

    IHTMLDocument2* pDoc = CWebBrowser2::GetDocument();
    IHTMLElement* pBody, *pParent;
    l_pDoc->get_body(&pBody);
    pBody->get_parentElement( &pParent );
    BSTR bstrHtml;
    pParent->get_outerHTML(&bstrHtml);
    CString strHtmlText(bstrHtml); // 这就是你想得到的源文件, 可能没有<!Doctype>标记.
    ::SysFreeString(bstrHtml);另一种方法:
    IPersistStreamInit *pPSI=NULL;
    IStream *pStream=NULL; 
    HGLOBAL  hHTMLText; 
        
    if  (FAILED(pDoc->QueryInterface(&pPSI))) return;


    hHTMLText = GlobalAlloc(GMEM_FIXED, 65534 );
    CreateStreamOnHGlobal(hHTMLText, TRUE, &pStream); 
    pPSI->Save(pStream, FALSE);

    CString strSource ; // Html 文本
    unsigned short unicode = *(unsigned short*)hHTMLText;
    if( unicode==0xFEFF )
    {
    wchar_t * wideString = (wchar_t*)(hHTMLText);
    wideString += 1;
    int len = wcslen( wideString );
    int count = wcstombs( NULL, wideString,len);
    char* pBuf = new char[count+1];
    memset( pBuf, 0 , count );
    count = wcstombs( pBuf,wideString,count);
    if( count > 0 )
    pBuf[count] = '\0';
    strSource = CString( pBuf );
    delete[] pBuf;
    }
    else
    {
    strSource = CString( (char*)hHTMLText );
    }

    pStream->Release();
    pPSI->Release();
      

  2.   

    朋友,谢谢你了哦!但我还是看不懂哦,可以再帮帮我吗?
    我用的是VC(MFC),你能告诉我你的代码怎么加到我的下边的代码里吗?谢谢了
    void CHehe88Dlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here}
      

  3.   

    用   Microsoft   Internet   Transfer   Control!     
        
      s=inet1.OpenURL("http://ygyuan.go.163.com/")   
        
      s就是你所要的啦!
      

  4.   

    还是不会,我的定义CWebBrowser2  m_browser;
    怎么把你们的代码加到我的
    void CHehe88Dlg::OnButton2() 
    {
    // TODO: Add your control notification handler code here}
    中直接就好使哦.
    我不知道s是什么类型哦.
    我是这么加进去的,
    CString string1;
    string1=m_browser.OpenURL("http://ygyuan.go.163.com/");
    你你看看我的编译错误信息:
    error C2039: 'OpenURL' : is not a member of 'CWebBrowser2'
    hehe88.exe - 1 error(s), 0 warning(s)再帮帮我,谢谢!
      

  5.   

    呵呵,谢谢你[yjgx007]!
    问题搞定了.同时也谢谢其他帮我的朋友了.