TWebBowser 使用技巧

#说明:如果你的程序无法通过,请确认是否已经添加了以下几行代码。

initialization
OleInitialize(nil);

finalization
OleUninitialize;

1.保存WebBrowser当前页面或URL为MHT文件格式。

procedure SaveAsMHT(const webBrowser: TWebBrowser; const fileName: String);
//needs ActiveX, CDO_TLB, ADODB_TLB, [install type library CDO, and Activex HTML Oleobject.
var
  imsg: IMessage;
  iconf: IConfiguration;
  stream: _Stream;
begin
  imsg := CoMessage.Create;
  iconf := CoConfiguration.Create;

  try
    imsg.Configuration := iconf;
    imsg.HTMLBody := GetBrowserHtml(webBrowser);
    stream := imsg.GetStream;
    stream.SaveToFile(fileName, adSaveCreateOverWrite);
  finally
    imsg := nil;
    iconf := nil;
    stream := nil;
  end;
end;

procedure SaveURLasMHT(const url: String; const fileName: String);
//needs ActiveX, CDO_TLB, ADODB_TLB,
var
  imsg: IMessage;
  iconf: IConfiguration;
  stream: _Stream;
begin
  imsg := CoMessage.Create;
  iconf := CoConfiguration.Create;

  try
    imsg.Configuration := iconf;
    imsg.CreateMHTMLBody(url,cdoSuppressNone,'','');
   // imsg.CreateMHTMLBody(url,cdoSuppressAll,'','');
    stream := imsg.GetStream;
    stream.SaveToFile(fileName, adSaveCreateOverWrite);
  finally
    imsg := nil;
    iconf := nil;
    stream := nil;
  end;
end;

2.直接装载HTML代码到WebBrowser的三种方法。

procedure WBLoadHTML1(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
   sl: TStringList;
   ms: TMemoryStream;
begin
   WebBrowser.Navigate('about:blank') ;
   while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
    Application.ProcessMessages;

   if Assigned(WebBrowser.Document) then
   begin
     sl := TStringList.Create;
     try
       ms := TMemoryStream.Create;
       try
         sl.Text := HTMLCode;
         sl.SaveToStream(ms) ;
         ms.Seek(0, 0) ;
         (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
       finally
         ms.Free;
       end;
     finally
       sl.Free;
     end;
   end;
end;

procedure WBLoadHTML2(WebBrowser: TWebBrowser; HTMLCode: string) ;
begin
WebBrowser.Navigate('about:'+HTMLCode);
end;

procedure WBLoadHTML3(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
HTMLDocument:IHTMLDocument2;
v: Variant;
begin
HTMLDocument := WebBrowser.Document as IHTMLDocument2;
WebBrowser.Navigate('about:blank') ;
while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
Application.ProcessMessages;
if (assigned(HtmlDocument)) then
begin
        v := VarArrayCreate([0, 0], varVariant);
        v[0] := HTMLCode; // Here's your HTML string
        HTMLDocument.Write(PSafeArray(TVarData(v).VArray));
        HTMLDocument.Close;
  end;
end;

3.获取WebBrowser的HTML代码

function GetBrowserHtml(const webBrowser: TWebBrowser): String;
 //needs ActiveX, CDO_TLB, ADODB_TLB,
var
  strStream: TStringStream;
  adapter: IStream;
  browserStream: IPersistStreamInit;
begin
  strStream := TStringStream.Create('');
  try
    browserStream := webBrowser.Document as IPersistStreamInit;
    adapter := TStreamAdapter.Create(strStream,soReference);
    browserStream.Save(adapter,true);
    result := strStream.DataString;
  finally
  end;
  strStream.Free();
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值