TWebBowser 使用技巧

来源:互联网 发布:帝国cms采集正文图片 编辑:程序博客网 时间:2024/05/04 03:56

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

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;
 

原创粉丝点击