楼主提醒我了,我用流的方式显示MHT文件有问题,代码如下,所以我想IE应该还做了一定程度的处理,不过可以看出来的是WebBrower显示的和我把mht扩展名修改成htm的效果不同,所以里面的编码是经过IE处理的,但是WebBrower不能直接显示,下面的代码关键还是那个接口IPersistStreamInit,MSDN里面的介绍比较少(可能是我没找到),所以想到在ASP里面的Response.BinaryWrite这个方法,如果我们在程序里面可以实现这样其实就可以了,继续找资料中……
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, OleCtrls, SHDocVw, Activex, StdCtrls, Buttons, ExtCtrls;
type
TForm1 = class(TForm)
WebBrowser1: TWebBrowser;
BitBtn1: TBitBtn;
procedure BitBtn1Click(Sender: TObject);
private
function ShowHtml(mWebBrowser: TWebBrowser;MS : TMemoryStream): Boolean;
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function TForm1.ShowHtml(mWebBrowser: TWebBrowser; MS : TMemoryStream): Boolean;
var
vMemoryStream: TMemoryStream;
begin
Result := False;
if not (Assigned(MS ) and Assigned(mWebBrowser)) then Exit;
mWebBrowser.Navigate('about:blank');
if not Assigned(mWebBrowser.Document) then Exit;
vMemoryStream := TMemoryStream.Create;
try
MS.SaveToStream(vMemoryStream);
try
vMemoryStream.Position := 0;
Application.ProcessMessages; //
(mWebBrowser.Document as IPersistStreamInit).Load(
TStreamAdapter.Create(vMemoryStream));
except
Exit;
end;
finally
vMemoryStream.Free;
end;
Result := True;
end; { ShowHtml }
procedure TForm1.BitBtn1Click(Sender: TObject);
var
FS : TFileStream;
MS : TMemoryStream;
begin
FS := TFileStream.Create('c:/a.mht', fmOpenRead );
MS := TMemoryStream.Create;
try
FS.Position := 0;
MS.CopyFrom(FS, Fs.Size);
ShowHtml(WebBrowser1, MS);
finally
MS.Free;
FS.Free;
end;
end;
end.