如何把webbrowser显示的网页内容保存到单一的MHT文件中去,并且不显示保存对话框,实现文本图片隐式保存到单一的.mht文件中去?

解决方案 »

  1.   

    有问题你找伴水
    //Delphi6+Windows2000调试通过~~  
       
      (*//  
      标题:将网页保存为mht文件  
      说明:接口只定义需要使用的部分,如要研究请参考其他文档  
      设计:Zswang  
      支持:[email protected]  
      日期:2004-01-05  
      //*)  
       
      ///////Begin   Source  
      uses   ComObj;  
       
      ///////Begin   接口定义  
      //   Constants   for   enum   SaveOptionsEnum  
      type  
          SaveOptionsEnum   =   TOleEnum;  
      const  
          adSaveCreateNotExist   =   $00000001;  
          adSaveCreateOverWrite   =   $00000002;  
       
      //   Constants   for   enum   CdoMHTMLFlags  
      type  
          CdoMHTMLFlags   =   TOleEnum;  
      const  
          cdoSuppressNone   =   $00000000;  
          cdoSuppressImages   =   $00000001;  
          cdoSuppressBGSounds   =   $00000002;  
          cdoSuppressFrames   =   $00000004;  
          cdoSuppressObjects   =   $00000008;  
          cdoSuppressStyleSheets   =   $00000010;  
          cdoSuppressAll   =   $0000001F;  
       
      //   Constants   for   enum   StreamTypeEnum  
      type  
          StreamTypeEnum   =   TOleEnum;  
      const  
          adTypeBinary   =   $00000001;  
          adTypeText   =   $00000002;  
       
      //   Constants   for   enum   LineSeparatorEnum  
      type  
          LineSeparatorEnum   =   TOleEnum;  
      const  
          adLF   =   $0000000A;  
          adCR   =   $0000000D;  
          adCRLF   =   $FFFFFFFF;  
       
      //   Constants   for   enum   ObjectStateEnum  
      type  
          ObjectStateEnum   =   TOleEnum;  
      const  
          adStateClosed   =   $00000000;  
          adStateOpen   =   $00000001;  
          adStateConnecting   =   $00000002;  
          adStateExecuting   =   $00000004;  
          adStateFetching   =   $00000008;  
       
      //   Constants   for   enum   ConnectModeEnum  
      type  
          ConnectModeEnum   =   TOleEnum;  
      const  
          adModeUnknown   =   $00000000;  
          adModeRead   =   $00000001;  
          adModeWrite   =   $00000002;  
          adModeReadWrite   =   $00000003;  
          adModeShareDenyRead   =   $00000004;  
          adModeShareDenyWrite   =   $00000008;  
          adModeShareExclusive   =   $0000000C;  
          adModeShareDenyNone   =   $00000010;  
          adModeRecursive   =   $00400000;  
       
      //   Constants   for   enum   StreamOpenOptionsEnum  
      type  
          StreamOpenOptionsEnum   =   TOleEnum;  
      const  
          adOpenStreamUnspecified   =   $FFFFFFFF;  
          adOpenStreamAsync   =   $00000001;  
          adOpenStreamFromRecord   =   $00000004;  
       
      //   Constants   for   enum   StreamWriteEnum  
      type  
          StreamWriteEnum   =   TOleEnum;  
      const  
          adWriteChar   =   $00000000;  
          adWriteLine   =   $00000001;  
          stWriteChar   =   $00000000;  
          stWriteLine   =   $00000001;  
       
      //   Constants   for   enum   CdoDSNOptions  
      type  
          CdoDSNOptions   =   TOleEnum;  
      const  
          cdoDSNDefault   =   $00000000;  
          cdoDSNNever   =   $00000001;  
          cdoDSNFailure   =   $00000002;  
          cdoDSNSuccess   =   $00000004;  
          cdoDSNDelay   =   $00000008;  
          cdoDSNSuccessFailOrDelay   =   $0000000E;  
       
      //   Constants   for   enum   CdoReferenceType  
      type  
          CdoReferenceType   =   TOleEnum;  
      const  
          cdoRefTypeId   =   $00000000;  
          cdoRefTypeLocation   =   $00000001;  
       
      type  
          IBodyPart   =   interface(IDispatch)  
              ['{CD000021-8B95-11D1-82DB-00C04FB1625D}']  
          end;  
          IBodyParts   =   interface(IDispatch)  
              ['{CD000023-8B95-11D1-82DB-00C04FB1625D}']  
          end;  
          _Collection   =   interface(IDispatch)  
              ['{00000512-0000-0010-8000-00AA006D2EA4}']  
          end;  
          Fields15   =   interface(_Collection)  
              ['{00000506-0000-0010-8000-00AA006D2EA4}']  
          end;  
          Fields20   =   interface(Fields15)  
              ['{0000054D-0000-0010-8000-00AA006D2EA4}']  
          end;  
          Fields   =   interface(Fields20)  
              ['{00000564-0000-0010-8000-00AA006D2EA4}']  
          end;  
          IDataSource   =   interface(IDispatch)  
              ['{CD000029-8B95-11D1-82DB-00C04FB1625D}']  
          end;  
       
      type  
          _Stream   =   interface(IDispatch)  
              ['{00000565-0000-0010-8000-00AA006D2EA4}']  
              function     Get_Size:   Integer;   safecall;  
              function     Get_EOS:   WordBool;   safecall;  
              function     Get_Position:   Integer;   safecall;  
              procedure   Set_Position(pPos:   Integer);   safecall;  
              function     Get_Type_:   StreamTypeEnum;   safecall;  
              procedure   Set_Type_(ptype:   StreamTypeEnum);   safecall;  
              function     Get_LineSeparator:   LineSeparatorEnum;   safecall;  
              procedure   Set_LineSeparator(pLS:   LineSeparatorEnum);   safecall;  
              function     Get_State:   ObjectStateEnum;   safecall;  
              function     Get_Mode:   ConnectModeEnum;   safecall;  
              procedure   Set_Mode(pMode:   ConnectModeEnum);   safecall;  
              function     Get_Charset:   WideString;   safecall;  
              procedure   Set_Charset(const   pbstrCharset:   WideString);   safecall;  
              function     Read(NumBytes:   Integer):   OleVariant;   safecall;  
              procedure   Open(Source:   OleVariant;   Mode:   ConnectModeEnum;   Options:   StreamOpenOptionsEnum;  
                                            const   UserName:   WideString;   const   Password:   WideString);   safecall;  
              procedure   Close;   safecall;  
              procedure   SkipLine;   safecall;  
              procedure   Write(Buffer:   OleVariant);   safecall;  
              procedure   SetEOS;   safecall;  
              procedure   CopyTo(const   DestStream:   _Stream;   CharNumber:   Integer);   safecall;  
              procedure   Flush;   safecall;  
              procedure   SaveToFile(const   FileName:   WideString;   Options:   SaveOptionsEnum);   safecall;  
              procedure   LoadFromFile(const   FileName:   WideString);   safecall;  
              function     ReadText(NumChars:   Integer):   WideString;   safecall;  
              procedure   WriteText(const   Data:   WideString;   Options:   StreamWriteEnum);   safecall;  
              procedure   Cancel;   safecall;  
              property   Size:   Integer   read   Get_Size;  
              property   EOS:   WordBool   read   Get_EOS;  
              property   Position:   Integer   read   Get_Position   write   Set_Position;  
              property   Type_:   StreamTypeEnum   read   Get_Type_   write   Set_Type_;  
              property   LineSeparator:   LineSeparatorEnum   read   Get_LineSeparator   write   Set_LineSeparator;  
              property   State:   ObjectStateEnum   read   Get_State;  
              property   Mode:   ConnectModeEnum   read   Get_Mode   write   Set_Mode;  
              property   Charset:   WideString   read   Get_Charset   write   Set_Charset;  
          end;  
       
          IConfiguration   =   interface(IDispatch)  
              ['{CD000022-8B95-11D1-82DB-00C04FB1625D}']  
          end;   
      

  2.   

    IMessage   =   interface(IDispatch)  
              ['{CD000020-8B95-11D1-82DB-00C04FB1625D}']  
              function     Get_BCC:   WideString;   safecall;  
              procedure   Set_BCC(const   pBCC:   WideString);   safecall;  
              function     Get_CC:   WideString;   safecall;  
              procedure   Set_CC(const   pCC:   WideString);   safecall;  
              function     Get_FollowUpTo:   WideString;   safecall;  
              procedure   Set_FollowUpTo(const   pFollowUpTo:   WideString);   safecall;  
              function     Get_From:   WideString;   safecall;  
              procedure   Set_From(const   pFrom:   WideString);   safecall;  
              function     Get_Keywords:   WideString;   safecall;  
              procedure   Set_Keywords(const   pKeywords:   WideString);   safecall;  
              function     Get_MimeFormatted:   WordBool;   safecall;  
              procedure   Set_MimeFormatted(pMimeFormatted:   WordBool);   safecall;  
              function     Get_Newsgroups:   WideString;   safecall;  
              procedure   Set_Newsgroups(const   pNewsgroups:   WideString);   safecall;  
              function     Get_Organization:   WideString;   safecall;  
              procedure   Set_Organization(const   pOrganization:   WideString);   safecall;  
              function     Get_ReceivedTime:   TDateTime;   safecall;  
              function     Get_ReplyTo:   WideString;   safecall;  
              procedure   Set_ReplyTo(const   pReplyTo:   WideString);   safecall;  
              function     Get_DSNOptions:   CdoDSNOptions;   safecall;  
              procedure   Set_DSNOptions(pDSNOptions:   CdoDSNOptions);   safecall;  
              function     Get_SentOn:   TDateTime;   safecall;  
              function     Get_Subject:   WideString;   safecall;  
              procedure   Set_Subject(const   pSubject:   WideString);   safecall;  
              function     Get_To_:   WideString;   safecall;  
              procedure   Set_To_(const   pTo:   WideString);   safecall;  
              function     Get_TextBody:   WideString;   safecall;  
              procedure   Set_TextBody(const   pTextBody:   WideString);   safecall;  
              function     Get_HTMLBody:   WideString;   safecall;  
              procedure   Set_HTMLBody(const   pHTMLBody:   WideString);   safecall;  
              function     Get_Attachments:   IBodyParts;   safecall;  
              function     Get_Sender:   WideString;   safecall;  
              procedure   Set_Sender(const   pSender:   WideString);   safecall;  
              function     Get_Configuration:   IConfiguration;   safecall;  
              procedure   _Set_Configuration(const   pConfiguration:   IConfiguration);   safecall;  
              procedure   Set_Configuration(const   pConfiguration:   IConfiguration);   safecall;  
              function     Get_AutoGenerateTextBody:   WordBool;   safecall;  
              procedure   Set_AutoGenerateTextBody(pAutoGenerateTextBody:   WordBool);   safecall;  
              function     Get_EnvelopeFields:   Fields;   safecall;  
              function     Get_TextBodyPart:   IBodyPart;   safecall;  
              function     Get_HTMLBodyPart:   IBodyPart;   safecall;  
              function     Get_BodyPart:   IBodyPart;   safecall;  
              function     Get_DataSource:   IDataSource;   safecall;  
              function     Get_Fields:   Fields;   safecall;  
              function     Get_MDNRequested:   WordBool;   safecall;  
              procedure   Set_MDNRequested(pMDNRequested:   WordBool);   safecall;  
              function     AddRelatedBodyPart(const   URL:   WideString;   const   Reference:   WideString;  
                                                                        ReferenceType:   CdoReferenceType;   const   UserName:   WideString;  
                                                                        const   Password:   WideString):   IBodyPart;   safecall;  
              function     AddAttachment(const   URL:   WideString;   const   UserName:   WideString;  
                                                              const   Password:   WideString):   IBodyPart;   safecall;  
              procedure   CreateMHTMLBody(const   URL:   WideString;   Flags:   CdoMHTMLFlags;  
                                                                  const   UserName:   WideString;   const   Password:   WideString);   safecall;  
              function     Forward:   IMessage;   safecall;  
              procedure   Post;   safecall;  
              function     PostReply:   IMessage;   safecall;  
              function     Reply:   IMessage;   safecall;  
              function     ReplyAll:   IMessage;   safecall;  
              procedure   Send;   safecall;  
              function     GetStream:   _Stream;   safecall;  
              function     GetInterface(const   Interface_:   WideString):   IDispatch;   safecall;  
              property   BCC:   WideString   read   Get_BCC   write   Set_BCC;  
              property   CC:   WideString   read   Get_CC   write   Set_CC;  
              property   FollowUpTo:   WideString   read   Get_FollowUpTo   write   Set_FollowUpTo;  
              property   From:   WideString   read   Get_From   write   Set_From;  
              property   Keywords:   WideString   read   Get_Keywords   write   Set_Keywords;  
              property   MimeFormatted:   WordBool   read   Get_MimeFormatted   write   Set_MimeFormatted;  
              property   Newsgroups:   WideString   read   Get_Newsgroups   write   Set_Newsgroups;  
              property   Organization:   WideString   read   Get_Organization   write   Set_Organization;  
              property   ReceivedTime:   TDateTime   read   Get_ReceivedTime;  
              property   ReplyTo:   WideString   read   Get_ReplyTo   write   Set_ReplyTo;  
              property   DSNOptions:   CdoDSNOptions   read   Get_DSNOptions   write   Set_DSNOptions;  
              property   SentOn:   TDateTime   read   Get_SentOn;  
              property   Subject:   WideString   read   Get_Subject   write   Set_Subject;  
              property   To_:   WideString   read   Get_To_   write   Set_To_;  
              property   TextBody:   WideString   read   Get_TextBody   write   Set_TextBody;  
              property   HTMLBody:   WideString   read   Get_HTMLBody   write   Set_HTMLBody;  
              property   Attachments:   IBodyParts   read   Get_Attachments;  
              property   Sender:   WideString   read   Get_Sender   write   Set_Sender;  
              property   Configuration:   IConfiguration   read   Get_Configuration   write   _Set_Configuration;  
              property   AutoGenerateTextBody:   WordBool   read   Get_AutoGenerateTextBody   write   Set_AutoGenerateTextBody;  
              property   EnvelopeFields:   Fields   read   Get_EnvelopeFields;  
              property   TextBodyPart:   IBodyPart   read   Get_TextBodyPart;  
              property   HTMLBodyPart:   IBodyPart   read   Get_HTMLBodyPart;  
              property   BodyPart:   IBodyPart   read   Get_BodyPart;  
              property   DataSource:   IDataSource   read   Get_DataSource;  
              property   Fields:   Fields   read   Get_Fields;  
              property   MDNRequested:   WordBool   read   Get_MDNRequested   write   Set_MDNRequested;  
          end;  
       
      const  
          CLASS_Message:   TGUID   =   '{CD000001-8B95-11D1-82DB-00C04FB1625D}';  
          CLASS_Configuration:   TGUID   =   '{CD000002-8B95-11D1-82DB-00C04FB1625D}';  
      ///////End   接口定义  
       
      procedure   SaveWholePage(mURL:   string;   mFileName:   TFileName);  
      var  
          vMessage:   IMessage;  
          vConfiguration:   IConfiguration;  
          vStream:   _Stream;  
      begin  
          vMessage   :=   CreateComObject(CLASS_Message)   as   IMessage;  
          vConfiguration   :=   CreateComObject(CLASS_Configuration)   as   IConfiguration;  
          try  
              vMessage.Configuration   :=   vConfiguration;  
              vMessage.CreateMHTMLBody(mURL,   cdoSuppressAll,   '',   '');  
              vStream   :=   vMessage.GetStream;  
              vStream.SaveToFile(mFileName,   adSaveCreateOverWrite);  
          finally  
              vMessage   :=   nil;  
              vConfiguration   :=   nil;  
              vStream   :=   nil;  
          end;  
      end;  
      ///////End   Source  
       
      //Example  
      procedure   TForm1.Button1Click(Sender:   TObject);  
      begin  
          SaveWholePage('http://www.eping.net/fourm/',   'c:\temp\temp.mht');  
      end;   
      

  3.   

    我测试了上面的代码,通不过,还有没有别的办法,直接保存webbrowser中的内容为mht文件?
      

  4.   

    下个 极速浏览器 delphi源代码研究下
    我刚看了下,有保存为mth的选项!
      

  5.   

    是的,极速浏览器的代码我看了,是用的EmbeddedWB_D2005控件做的,但这个控件的保存为MHT是显示为一个对话框,是这样的形式:
    procedure TfrmMain.SaveAs1Click(Sender: TObject);
    begin
      EmbeddedWB1.SaveDialog;
    end;
    若用下面保存文件的代码,只能保存为html,若改为mht,则保存后打开是乱码
    procedure TfrmMain.SaveClick(Sender: TObject);
    begin
      EmbeddedWB1.SaveToFile('Site.html');
    end;如何修改控件的代码,才能不显示文件类型对话框,而直接将控件里面的内容保存为mht文件且能打开正常显示呢?