您现在的位置:首页 >> API >> API >> 内容

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

时间:2011/9/3 15:43:49 点击:

  核心提示:以下代码Delphi2007调试通过unit Unit3;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics,...

以下代码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,'<html><body  bgcolor="#0099FF">test</html>');
end;

procedure TForm3.Button2Click(Sender: TObject);
var
  vv: Variant;
  HTMLDocument: IHTMLDocument2;
begin
  vv := VarArrayCreate([0,0],varVariant);
  try
    vv[0] := '<html><body  bgcolor="#0099FF">test</html>';
    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.

 

Tags:方法 写入 
作者:站长 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号