怎么能存成。mht文件呢?网页是很多文件的组合啊,假如非要存成一个文件,不如截图吧:)

解决方案 »

  1.   

    可是IE本身不就可以完成.mht文件的存储吗?!
    我要在程序中动态给出文件名进行存储,还有哪位大侠可以教我。
      

  2.   

    用这个。多给我点分哟。
    --------------------------------
    procedure TForm1.SaveHTMLSourceToFile(const FileName: string; 
      WB: TWebBrowser); 
    var 
      PersistStream: IPersistStreamInit; 
      FileStream: TFileStream; 
      Stream: IStream; 
      SaveResult: HRESULT; 
    begin 
      PersistStream := WB.Document as IPersistStreamInit; 
      FileStream := TFileStream.Create(FileName, fmCreate); 
      try 
        Stream := TStreamAdapter.Create(FileStream, soReference) as IStream; 
        SaveResult := PersistStream.Save(Stream, True); 
        if FAILED(SaveResult) then 
          MessageBox(Handle, 'Fail to save HTML source', 'Error', 0); 
      finally 
        { we are passing soReference in TStreamAdapter constructor, 
          it is our responsibility to destroy the TFileStream object. } 
        FileStream.Free; 
      end; 
    end; pocedure TForm1.Button1Click(Sender: TObject); 
    begin 
      if SaveDialog1.Execute then 
        SaveHTMLSourceToFile(SaveDialog1.FileName, WebBrowser1); 
    end; 
      

  3.   

    上面这位老兄,可能是我的基础太差,你的代码我有点看不懂。
    var 
      PersistStream: IPersistStreamInit; 
      Stream: IStream; 
    上面是你定义的变量,但上面这两句我的Delphi编不过去,你能不能给我一个完整点的代码,起码把你的Uses段的东西给我,谢谢了!
    另外,加分是没有问题的,我可以出到300分,或更高也行。
      

  4.   

    to:ChinaDelphiFan
    你这种方法可以存下一个网页的原码,但我要的是存下所有文字和图片,由其是图片,你的程序中好象没有,只是有连接,不过还是要谢谢你能给我回话。可是,我的问题还是没有解决。
    请各位大侠们快来帮忙呀!!!
      

  5.   

    那用这个吧
    ----------------------------------
    procedure TForm1.Button3Click(Sender: TObject);
    var
       Vin,Vout : OleVariant;
    begin
      vin:='';
       vout:='';
       WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DODEFAULT,vin, vout);
    end;
      

  6.   

    如此好了。
    存一个网页搞那么复杂干什么?
    用IE的另存功能吧。存为html的文件。
    图片之类全都会安置好。
    再采用winzip压缩,压缩后做成exe文件。不就是一个文件了吗?呵呵。
      

  7.   

    To: ChinaDelphiFan
        我所需要的就是这个意思,但还有一个问题是,我不能要存档这个窗体,我需要在程序中指定出文件名,然后直接存档,请问有没有办法,再次谢谢你。也请其他高手帮我一把!
      
      

  8.   

    有没有人知道呀,我真的很急着要用呀!
    WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DODEFAULT,vin, vout);
    有没有人可以把上面这条语句的SAVE窗口去掉。
      

  9.   

    据我所知,你的要求只有用上面的语句实现。至于去掉窗口,有一些控件可以实现。基本原理是,事先向键盘缓冲区发送
    一些击键信息,然后再调用该save窗口。由于很快速,save窗口是看不见的。
      

  10.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, OleCtrls, SHDocVw, activeX;type
      TForm1 = class(TForm)
        WebBrowser1: TWebBrowser;
        Button1: TButton;
        SaveDialog1: TSaveDialog;
        Button2: TButton;
        Button3: TButton;    procedure SaveHTMLSourceToFile(const FileName: string;WB: TWebBrowser);
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure SimulateKeyDown(Key : byte);
    begin
      keybd_event(Key, 0, 0, 0);
    end;procedure SimulateKeyUp(Key : byte);
    begin
      keybd_event(Key, 0, KEYEVENTF_KEYUP, 0);
    end;procedure SimulateKeystroke(Key : byte;
                                extra : DWORD);
    begin
      keybd_event(Key,
                  extra,
                  0,
                  0);
      keybd_event(Key,
                  extra,
                  KEYEVENTF_KEYUP,
                  0);
    end;procedure SendKeys(s : string);
    var
      i : integer;
      flag : bool;
      w : word;
    begin
     {Get the state of the caps lock key}
      flag := not GetKeyState(VK_CAPITAL) and 1 = 0;
     {If the caps lock key is on then turn it off}
      if flag then
        SimulateKeystroke(VK_CAPITAL, 0);
      for i := 1 to Length(s) do begin
        w := VkKeyScan(s[i]);
       {If there is not an error in the key translation}
        if ((HiByte(w) <> $FF) and
            (LoByte(w) <> $FF)) then begin
         {If the key requires the shift key down - hold it down}
          if HiByte(w) and 1 = 1 then
            SimulateKeyDown(VK_SHIFT);
         {Send the VK_KEY}
          SimulateKeystroke(LoByte(w), 0);
         {If the key required the shift key down - release it}
          if HiByte(w) and 1 = 1 then
            SimulateKeyUp(VK_SHIFT);
        end;
      end;
     {if the caps lock key was on at start, turn it back on}
      if flag then
        SimulateKeystroke(VK_CAPITAL, 0);
    end;procedure TForm1.SaveHTMLSourceToFile(const FileName: string;
      WB: TWebBrowser);
    var 
      PersistStream: IPersistStreamInit;
      FileStream: TFileStream; 
      Stream: IStream; 
      SaveResult: HRESULT;
    begin 
      PersistStream := WB.Document as IPersistStreamInit; 
      FileStream := TFileStream.Create(FileName, fmCreate); 
      try 
        Stream := TStreamAdapter.Create(FileStream, soReference) as IStream;
        SaveResult := PersistStream.Save(Stream, True); 
        if FAILED(SaveResult) then 
          MessageBox(Handle, 'Fail to save HTML source', 'Error', 0); 
      finally 
        { we are passing soReference in TStreamAdapter constructor, 
          it is our responsibility to destroy the TFileStream object. } 
        FileStream.Free; 
      end; 
    end; procedure TForm1.Button1Click(Sender: TObject); 
    begin
      if SaveDialog1.Execute then
        SaveHTMLSourceToFile(SaveDialog1.FileName, WebBrowser1);
    end;
    procedure TForm1.Button2Click(Sender: TObject);
    begin
          webbrowser1.Navigate('www.csdn.net');
    end;procedure TForm1.Button3Click(Sender: TObject);
    var
       Vin,Vout : OleVariant;
       savepath, keysequence: string;
    begin
      vin:='aaaaa.mht';
       vout:='';
     //  WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DODEFAULT,vin, vout);
      // WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_DONTPROMPTUSER,vin, vout);   savepath:='c:\tmp\';
       keysequence:=Format('%s%s',[savepath, FormatDateTime('yymmddmmss',now)+'.mht']);
       sendkeys(keysequence);
       SimulateKeyStroke(VK_TAB,0);
       SimulateKeyStroke(VK_DOWN,0);
       SimulateKeyStroke(VK_DOWN,0);
       simulateKeyStroke(VK_RETURN,0);
       SimulateKeyStroke(VK_RETURN,0);
       WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_PROMPTUSER);
    end;end.
      

  11.   

    To: ChinaDelphiFan
    谢谢你的回复,基本上可以完成我的要求。
    如果没有更好的答案,我将把分送你。还有没有更好的答案!
      

  12.   

    当然有!你去看看这个软件:www.shijun.com
      

  13.   

    mht文件好象也并非真的把图片也存进去
    有次我在家里保存了mht文件拷到其它地方看,图片就没显示出来
      

  14.   

    to haishen:
     你直接问软件的作者吧,他可金山公司开发wps的哟!
      

  15.   

    请用“直接读取TWebbrowser控件的源码”一文,搜索一下就能找到,我在《商务导航》软件中大量用到该方法,另外,开可以向里写源码!!!