zoukankan      html  css  js  c++  java
  • 使用TWebBrowser组件保存网页为html和mht文件 收藏

    uses ActiveX;
    ...
    procedure WB_SaveAs_HTML(WB : TWebBrowser; const FileName : string) ;
    var
      PersistStream: IPersistStreamInit;
      Stream: IStream;
      FileStream: TFileStream;
    begin
      if not Assigned(WB.Document) then
      begin
        ShowMessage('Document not loaded!') ;
        Exit;
      end;

      PersistStream := WB.Document as IPersistStreamInit;
      FileStream := TFileStream.Create(FileName, fmCreate) ;
      try
        Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
        if Failed(PersistStream.Save(Stream, True)) then ShowMessage('SaveAs HTML fail!') ;
      finally
        FileStream.Free;
      end;
    end; (* WB_SaveAs_HTML *)

    使用方法:

    WebBrowser1.Navigate('http://www.uufax.com') ;

    //then save
    WB_SaveAs_HTML(WebBrowser1,'c:/WebBrowser1.html') ;

    二、另存为MHT单一文件

    uses CDO_TLB, ADODB_TLB;
    ...
    procedure WB_SaveAs_MHT(WB: TWebBrowser; FileName: TFileName) ;
    var
      Msg: IMessage;
      Conf: IConfiguration;
      Stream: _Stream;
      URL : widestring;
    begin
      if not Assigned(WB.Document) then Exit;

      URL := WB.LocationURL;

      Msg := CoMessage.Create;
      Conf := CoConfiguration.Create;
      try
        Msg.Configuration := Conf;
        Msg.CreateMHTMLBody(URL, cdoSuppressAll, '', '') ;
        Stream := Msg.GetStream;
        Stream.SaveToFile(FileName, adSaveCreateOverWrite) ;
      finally
        Msg := nil;
        Conf := nil;
        Stream := nil;
      end;
    end; (* WB_SaveAs_MHT *)

    使用方法:

    //first navigate
    WebBrowser1.Navigate('http://www.uufax.com') ;

    //then save
    WB_SaveAs_MHT(WebBrowser1,'c:/WebBrowser1.mht') ;


    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sunstone/archive/2009/11/09/4788743.aspx

  • 相关阅读:
    我的WCF之旅(1):创建一个简单的WCF程序
    网页设计中颜色的搭配
    CSS HACK:全面兼容IE6/IE7/IE8/FF的CSS HACK
    UVa 1326 Jurassic Remains
    UVa 10340 All in All
    UVa 673 Parentheses Balance
    UVa 442 Matrix Chain Multiplication
    UVa 10970 Big Chocolate
    UVa 679 Dropping Balls
    UVa 133 The Dole Queue
  • 原文地址:https://www.cnblogs.com/findumars/p/5011876.html
Copyright © 2011-2022 走看看