5-8万年薪顶级嵌入式,京沪深就业地 浅谈并行编程中的任务分解模式
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  VC/MFC >  HTML/XML

怎样用HtmlView实现把浏览过的网页保存到本地呢?

楼主chui88(飞扬)2002-06-06 16:29:57 在 VC/MFC / HTML/XML 提问

我需要把浏览过的网页(XML页面)保存到本地,怎样编码呢?  
   
  希望给出源码,谢谢。 问题点数:50、回复次数:21Top

1 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-06 22:11:18 得分 50

HRESULT                     hr         =   E_FAIL;  
          IDispatch*               pDisp   =   NULL;  
          IHTMLDocument2*     pDoc     =   NULL;  
   
          pDisp                                     =   GetHtmlDocument();  
   
        if(SUCCEEDED(hr   =   pDisp->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc)))  
        {  
                IPersistFile* pFile = NULL;  
                if(SUCCEEDED(pDoc->QueryInterface(IID_IPersistFile,(void**)&pFile)))  
                {  
  LPCOLESTR file   =   L"c:\\test1.htm";  
  pFile->Save(file,TRUE);  
                }  
        }Top

2 楼chui88(飞扬)回复于 2002-06-10 14:46:52 得分 0

可是浏览的是XML的页面啊,怎么不行啊?得到的只是一个空的html头啊Top

3 楼zhuwenzheng(卧薪尝胆)回复于 2002-06-10 14:51:44 得分 0

http://www.vckbase.com/document/viewdoc.asp?id=345Top

4 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-10 15:27:59 得分 0

void   CAView::DocumentComplete(LPDISPATCH   pDisp,   VARIANT*   URL)  
  {  
  IDispatchPtr       spDisp;  
  HRESULT                   hr;  
   
        hr   =   m_pBrowserApp->QueryInterface(IID_IDispatch,   (void**)   &spDisp);  
        //   Is   the   IDispatch*   passed   to   us   for   the   top-level   window   ?  
        if   (pDisp   ==   spDisp)  
        {  
        IHTMLDocument2Ptr   spDoc;  
   
              //   Get   the   active   document  
              spDoc   =   GetHtmlDocument();  
              if   (   spDoc   )  
              {  
              IHTMLWindow2Ptr   spWin;  
   
                    //   Get   the   top-level   window  
                    spDisp   =   spDoc->Script;  
                    spWin   =   spDisp;  
                    if   (   spWin   )  
                    {  
                          //   Get   the   document  
                          spDoc   =   spWin->document;  
                          if   (   spDoc   )  
                          {  
                          IDispatchExPtr   spDispEx;  
   
                                //   Get   the   document's   IDispatchEx  
                                spDoc->QueryInterface(   IID_IDispatchEx,  
                                                                              (void**)&spDispEx   );  
                                if   (   spDispEx   )  
                                {  
                                _bstr_t       bstrName("XMLDocument");  
                                DISPID   dispid;  
   
                                      //   Get   the   XMLDocument   expando   property  
                                      spDispEx->GetDispID(   bstrName,  
                                                                                fdexNameCaseSensitive,  
                                                                                &dispid   );  
                                      if   (   SUCCEEDED(hr)   &&   dispid   !=   DISPID_UNKNOWN   )  
                                      {  
                                      VARIANT   var;  
                                      DISPPARAMS   dpNoArgs   =   {NULL,   NULL,   0,   0};  
   
                                            //   Get   the   XMLDocument   value  
                                            hr   =   spDispEx->Invoke(   dispid,  
                                                                                IID_NULL,  
                                                                                LOCALE_USER_DEFAULT,  
                                                                                DISPATCH_PROPERTYGET,  
                                                                                &dpNoArgs,  
                                                                                &var,  
                                                                                NULL,  
                                                                                NULL   );  
                                            if   (   SUCCEEDED(hr)   &&   var.vt   ==   VT_DISPATCH   )  
                                            {  
                                            IXMLDOMDocument*   pXMLDoc=NULL;  
   
                                                  //   Get   the   IXMLDOMDocument   interface  
                                                  var.pdispVal->QueryInterface(  
                                                                                IID_IXMLDOMDocument,  
                                                                                (void**)&pXMLDoc   );  
                                                  VariantClear(   &var   );  
                                                  if   (   pXMLDoc   )  
                                                  {  
                                                  //   Get   the   root   element  
                                                  IXMLDOMElement*   pXMLElem=NULL;  
   
                                                        pXMLDoc->get_documentElement(   &pXMLElem   );  
                                                        if   (   pXMLElem   )  
                                                        {  
                                                        BSTR   bstr;  
                                                        USES_CONVERSION;  
   
                                                              //   Get/display   the   tag   name  
                                                              pXMLElem->get_tagName(   &bstr   );  
                                                              AfxMessageBox(   OLE2T(bstr)   );  
                                                              pXMLElem->Release();  
                                                        }  
                                                        pXMLDoc->Release();  
                                                  }  
                                            }  
                                      }  
                                }  
                          }  
                    }  
              }  
        }  
  }  
  Top

5 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-10 15:28:34 得分 0

void   CAView::DocumentComplete(LPDISPATCH   pDisp,   VARIANT*   URL)  
  {  
  IDispatchPtr       spDisp;  
  HRESULT                   hr;  
   
        hr   =   m_pBrowserApp->QueryInterface(IID_IDispatch,   (void**)   &spDisp);  
        //   Is   the   IDispatch*   passed   to   us   for   the   top-level   window   ?  
        if   (pDisp   ==   spDisp)  
        {  
        IHTMLDocument2Ptr   spDoc;  
   
              //   Get   the   active   document  
              spDoc   =   GetHtmlDocument();  
              if   (   spDoc   )  
              {  
              IHTMLWindow2Ptr   spWin;  
   
                    //   Get   the   top-level   window  
                    spDisp   =   spDoc->Script;  
                    spWin   =   spDisp;  
                    if   (   spWin   )  
                    {  
                          //   Get   the   document  
                          spDoc   =   spWin->document;  
                          if   (   spDoc   )  
                          {  
                          IDispatchExPtr   spDispEx;  
   
                                //   Get   the   document's   IDispatchEx  
                                spDoc->QueryInterface(   IID_IDispatchEx,  
                                                                              (void**)&spDispEx   );  
                                if   (   spDispEx   )  
                                {  
                                _bstr_t       bstrName("XMLDocument");  
                                DISPID   dispid;  
   
                                      //   Get   the   XMLDocument   expando   property  
                                      spDispEx->GetDispID(   bstrName,  
                                                                                fdexNameCaseSensitive,  
                                                                                &dispid   );  
                                      if   (   SUCCEEDED(hr)   &&   dispid   !=   DISPID_UNKNOWN   )  
                                      {  
                                      VARIANT   var;  
                                      DISPPARAMS   dpNoArgs   =   {NULL,   NULL,   0,   0};  
   
                                            //   Get   the   XMLDocument   value  
                                            hr   =   spDispEx->Invoke(   dispid,  
                                                                                IID_NULL,  
                                                                                LOCALE_USER_DEFAULT,  
                                                                                DISPATCH_PROPERTYGET,  
                                                                                &dpNoArgs,  
                                                                                &var,  
                                                                                NULL,  
                                                                                NULL   );  
                                            if   (   SUCCEEDED(hr)   &&   var.vt   ==   VT_DISPATCH   )  
                                            {  
                                            IXMLDOMDocument*   pXMLDoc=NULL;  
   
                                                  //   Get   the   IXMLDOMDocument   interface  
                                                  var.pdispVal->QueryInterface(  
                                                                                IID_IXMLDOMDocument,  
                                                                                (void**)&pXMLDoc   );  
                                                  VariantClear(   &var   );  
                                                  if   (   pXMLDoc   )  
                                                  {  
                                                  //   Get   the   root   element  
                                                  IXMLDOMElement*   pXMLElem=NULL;  
   
                                                        pXMLDoc->get_documentElement(   &pXMLElem   );  
                                                        if   (   pXMLElem   )  
                                                        {  
                                                        BSTR   bstr;  
                                                        USES_CONVERSION;  
   
                                                              //   Get/display   the   tag   name  
                                                              pXMLElem->get_tagName(   &bstr   );  
                                                              AfxMessageBox(   OLE2T(bstr)   );  
                                                              pXMLElem->Release();  
                                                        }  
                                                        pXMLDoc->Release();  
                                                  }  
                                            }  
                                      }  
                                }  
                          }  
                    }  
              }  
        }  
  }  
  Top

6 楼chui88(飞扬)回复于 2002-06-10 17:47:34 得分 0

DocumentComplete()  
   
  我查了MSDN是ActiveX的接口函数哦,  
   
  在HtmlView里面怎么加进去呢?Top

7 楼chui88(飞扬)回复于 2002-06-11 09:18:43 得分 0

能够给我工程看看吗?谢谢,发到我的邮箱chui88@21cn.comTop

8 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-11 09:21:19 得分 0

看看CHtmlView的源代码  
   
  BEGIN_EVENTSINK_MAP(CHtmlView,   CFormView)  
  ON_EVENT(CHtmlView,   AFX_IDW_PANE_FIRST,   259   /*   DocumentComplete   */,   DocumentComplete,   VTS_DISPATCH   VTS_PVARIANT)END_EVENTSINK_MAP()  
   
  void   CHtmlView::DocumentComplete(LPDISPATCH   pDisp,   VARIANT*   URL)  
  {  
  UNUSED_ALWAYS(pDisp);  
  ASSERT(V_VT(URL)   ==   VT_BSTR);  
   
  CString   str(V_BSTR(URL));  
  OnDocumentComplete(str);  
  }  
  在你的类里面照抄就可以了Top

9 楼chui88(飞扬)回复于 2002-06-11 09:47:11 得分 0

我不明白的是IHTMLDocument2Ptr和IXMLDOMElement在哪里定义的啊  
   
  还要import什么文件吗?Top

10 楼chui88(飞扬)回复于 2002-06-11 18:25:44 得分 0

实在不明白,这段代码到底怎么用啊,我把BEGIN_EVENTSINK_MAP  
   
  那段抄进去了,不行哦,编译通不过,最好发给我工程看看好吗?谢谢了  
   
  chui88@21cn.comTop

11 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-11 19:55:28 得分 0

In   C++,   a   connection   can   be   accomplished   in   roughly   the   same   way.   Visual   C++   Native   Com   Support   is   used   here   for   the   sake   of   brevity.    
   
  Add   references   to   Shdocvw.dll   and   Mshtml.dll   to   the   project:    
   
  #import   <mshtml.dll>   //   Internet   Explorer   4.0x  
  #import   <mshtml.tlb>   //   Internet   Explorer   5  
  Top

12 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-11 19:55:53 得分 0

你怎么抄的?Top

13 楼chui88(飞扬)回复于 2002-06-12 08:36:54 得分 0

#import   <mshtml.tlb>   //   Internet   Explorer   5  
  这一段我早就加进去了  
   
  IXMLDOMElement可不是这里面定义的哦(我导入了这个tlb文件仔细看过)。  
   
  BEGIN_EVENTSINK_MAP(CHtmlView,   CFormView)  
  ON_EVENT(CHtmlView,   AFX_IDW_PANE_FIRST,   259   /*   DocumentComplete   */,   DocumentComplete,   VTS_DISPATCH   VTS_PVARIANT)END_EVENTSINK_MAP()  
   
  我把这一段抄进去,并加入了DocumentComplete函数,编译说未定义_GetSinkmap  
  Top

14 楼yuyk(wind)回复于 2002-06-12 09:15:05 得分 0

Top

15 楼yuyk(wind)回复于 2002-06-12 09:15:53 得分 0

Top

16 楼chui88(飞扬)回复于 2002-06-12 09:34:46 得分 0

我包含了  
  #include   "mshtml.h"  
  #import   "C:\winnt\system32\mshtml.tlb"   //   location   of   mshtml.tlb  
  编译时这一句  
  //   Get   the   top-level   window  
  spDisp   =   spDoc->Script;  
  说  
  error   C2039:   'Script'   :   is   not   a   member   of   'IHTMLDocument2'  
  怎么回事啊?  
   
  Top

17 楼chui88(飞扬)回复于 2002-06-12 09:35:13 得分 0

我包含了  
  #include   "mshtml.h"  
  #import   "C:\winnt\system32\mshtml.tlb"   //   location   of   mshtml.tlb  
  编译时这一句  
  //   Get   the   top-level   window  
  spDisp   =   spDoc->Script;  
  说  
  error   C2039:   'Script'   :   is   not   a   member   of   'IHTMLDocument2'  
  怎么回事啊?  
   
  Top

18 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-13 19:04:09 得分 0

忘记抄头文件了……去看看CHtmlVIew的定义头文件,应该有个Declare   Event   Sink的Top

19 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-13 19:08:39 得分 0

IHTMLDocument::Script   Property  
   
  Retrieves   an   interface   pointer   that   provides   access   to   the   scripting   object   via   a   handle   to   the   script   engine.    
   
  Syntax  
   
  HRESULT   IHTMLDocument::get_Script(IDispatch   **p);  
  spDoc是IHTMLDocument2Ptr吗?Top

20 楼chui88(飞扬)回复于 2002-06-14 15:07:05 得分 0

能把工程发给我看看吗?谢谢了,解决了一定加分  
   
  我这里问题很奇怪,spDoc后面打->后List   Member里面有Scripts,  
   
  编译却说error   C2039:   'Script'   :   is   not   a   member   of   'IHTMLDocument2'  
   
  IHTMLDocument2Ptr spDoc;  
   
  照你的代码抄的,不会有错啊。还有IXMLDOMElement我也不知道怎么定义。Top

21 楼storein(满天星)回复于 2002-06-14 15:21:16 得分 0

关注Top

22 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-14 18:52:48 得分 0

修改MFCIE示例的mfcieVw.cpp  
  #include   "stdafx.h"  
  #include   "mfcie.h"  
  #include   "MainFrm.h"  
   
  #include   "mfcieDoc.h"  
  #include   "mfcieVw.h"  
   
  #pragma   warning(disable   :   4192)  
  #pragma   warning(disable   :   4049)  
  #pragma   warning(disable   :   4146)  
  #import   <mshtml.tlb>  
  #import   <msxml4.dll>  
  #pragma   warning(default:   4192)  
  #pragma   warning(default:   4049)  
  #pragma   warning(default:   4146)  
  //   Include   Microsoft   HIML   definitions...  
   
  #include   <dispex.h>  
   
  //You   may   derive   a   class   from   CComModule   and   use   it   if   you   want   to   override  
  //something,   but   do   not   change   the   name   of   _Module  
  #include   <atlbase.h>//for   atlcom.h  
  extern   CComModule   _Module;//for   atlbase.h  
  #include   <atlcom.h>//for   OLE2T  
   
  ……  
  END_MESSAGE_MAP()  
   
  BEGIN_EVENTSINK_MAP(CMfcieView,   CHtmlView)  
  ON_EVENT(CMfcieView,   AFX_IDW_PANE_FIRST,   259   /*   DocumentComplete   */,   DocumentComplete,   VTS_DISPATCH   VTS_PVARIANT)  
  END_EVENTSINK_MAP()  
   
  /////////////////////////////////////////////////////////////////////////////  
  //   CMfcieView   construction/destruction  
   
  ……  
  //END   OF   MFC   MEMORY   LEAK   BUG   FIX  
   
  void   CMfcieView::DocumentComplete(LPDISPATCH   pDisp,   VARIANT*   URL)  
  {  
  ASSERT(V_VT(URL)   ==   VT_BSTR);  
  IDispatchPtr       spDisp;  
  HRESULT                   hr;  
   
        hr   =   m_pBrowserApp->QueryInterface(IID_IDispatch,   (void**)   &spDisp);  
        //   Is   the   IDispatch*   passed   to   us   for   the   top-level   window   ?  
        if   (pDisp   ==   spDisp)  
        {  
      MSHTML::IHTMLDocument2Ptr   spDoc;  
   
              //   Get   the   active   document  
              spDoc   =   GetHtmlDocument();  
              if   (   spDoc   )  
              {  
              MSHTML::IHTMLWindow2Ptr   spWin;  
   
                    //   Get   the   top-level   window  
                    spDisp   =   spDoc->Script;  
                    spWin   =   spDisp;  
                    if   (   spWin   )  
                    {  
                          //   Get   the   document  
                          spDoc   =   spWin->document;  
                          if   (   spDoc   )  
                          {  
                          IDispatchExPtr   spDispEx;  
   
                                //   Get   the   document's   IDispatchEx  
                                spDoc->QueryInterface(   IID_IDispatchEx,  
                                                                              (void**)&spDispEx   );  
                                if   (   spDispEx   )  
                                {  
                                _bstr_t       bstrName("XMLDocument");  
                                DISPID   dispid;  
   
                                      //   Get   the   XMLDocument   expando   property  
                                      spDispEx->GetDispID(   bstrName,  
                                                                                fdexNameCaseSensitive,  
                                                                                &dispid   );  
                                      if   (   SUCCEEDED(hr)   &&   dispid   !=   DISPID_UNKNOWN   )  
                                      {  
                                      VARIANT   var;  
                                      DISPPARAMS   dpNoArgs   =   {NULL,   NULL,   0,   0};  
   
                                            //   Get   the   XMLDocument   value  
                                            hr   =   spDispEx->Invoke(   dispid,  
                                                                                IID_NULL,  
                                                                                LOCALE_USER_DEFAULT,  
                                                                                DISPATCH_PROPERTYGET,  
                                                                                &dpNoArgs,  
                                                                                &var,  
                                                                                NULL,  
                                                                                NULL   );  
                                            if   (   SUCCEEDED(hr)   &&   var.vt   ==   VT_DISPATCH   )  
                                            {  
                                            MSXML2::IXMLDOMDocument*   pXMLDoc=NULL;  
   
                                                  //   Get   the   IXMLDOMDocument   interface  
                                                  var.pdispVal->QueryInterface(  
                                                                                IID_IXMLDOMDocument,  
                                                                                (void**)&pXMLDoc   );  
                                                  VariantClear(   &var   );  
                                                  if   (   pXMLDoc   )  
                                                  {  
                                                  //   Get   the   root   element  
                                                  MSXML2::IXMLDOMElement*   pXMLElem=NULL;  
   
                                                        pXMLDoc->get_documentElement(   &pXMLElem   );  
                                                        if   (   pXMLElem   )  
                                                        {  
                                                        BSTR   bstr;  
                                                        USES_CONVERSION;  
   
                                                              //   Get/display   the   tag   name  
                                                              pXMLElem->get_tagName(   &bstr   );  
                                                              AfxMessageBox(   OLE2T(bstr)   );  
                                                              pXMLElem->Release();  
                                                        }  
                                                        pXMLDoc->Release();  
                                                  }  
                                            }  
                                      }  
                                }  
                          }  
                    }  
              }  
        }  
  }  
  //end   new   code    
  Top

23 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-14 18:54:48 得分 0

头文件中添加成员函数  
  void   DocumentComplete(LPDISPATCH   pDisp,   VARIANT*   URL);Top

24 楼jiangsheng(蒋晟.Net[MVP])回复于 2002-06-14 19:02:29 得分 0

又忘记抄DECLARE_EVENTSINK_MAP了  
  ……  
  //}}AFX_MSG  
  DECLARE_MESSAGE_MAP()  
  DECLARE_EVENTSINK_MAP()  
  };Top

相关问题

  • 怎样把XML的网页保存下来脱机浏览啊?
  • 怎样保存浏览过的IE网页?
  • 如何在程序中清除保存在浏览器缓存中的网页.
  • 如何在VB编程中使用webbrowser控件浏览网页,无界面保存网页?
  • shtml网页保存?
  • 有谁知道,IE、Webbrowser浏览过程中网页保存在什么目录中?
  • 解决问题一定给分!如何编程控制IE保存正在浏览的网页?
  • 如何用程序实现将webbrower中浏览的网页保存成*.mht格式。
  • 保存网页问题
  • 保存网页的问题

关键词

  • hr
  • 浏览
  • spdoc
  • spdispex
  • spwin
  • documentcomplete
  • spdisp
  • ihtmldocument
  • eventsink
  • pdisp

得分解答快速导航

  • 帖主:chui88
  • jiangsheng

相关链接

  • Visual C++类图书
  • Visual C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
x 提问