Delphi中2种方法把HTML写入TWebBrowser中

提示:将HTML代码直接写入到TWebBrowser浏览器组件中去…以下代码Delphi2007调试通过

unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ActiveX, OleCtrls, SHDocVw, StdCtrls,MSHTML;
type
TForm3 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
procedure WB_LoadHTML(WebBrowser: TWebBrowser; HTMLCode: string);
var
    sl: TStringList;
    ms: TMemoryStream;
begin
    WebBrowser1.Navigate('about:blank');
    if Assigned(WebBrowser1.Document) then
    begin
      sl := TStringList.Create;
      try
        ms := TMemoryStream.Create;
        try
          sl.Text := HTMLCode;
          sl.SaveToStream(ms);
          ms.Seek(0, 0);
          (WebBrowser1.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms));
        finally
          ms.Free;
        end;
      finally
        sl.Free;
      end;
    end;
end;
begin
WB_LoadHTML(WebBrowser1,'test');
end;
procedure TForm3.Button2Click(Sender: TObject);
var
vv: Variant;
HTMLDocument: IHTMLDocument2;
begin
vv := VarArrayCreate([0,0],varVariant);
try
    vv[0] := 'test';
    WebBrowser1.Navigate('about:blank');
    HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
    HTMLDocument.write(PSafeArray(TVarData(vv).VArray));
    HTMLDocument.close;
finally
    HtmlDocument:=nil;
    vv:=Unassigned;
end;
end;
end.

留下评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

12 − = 8