Delphi HTML顯示與編輯

Delphi裡有一個TWebBrowser的控件,這個控件是windows裡的TOleControl控件,

可以用來顯示與編輯HTML

 

 

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtrls, SHDocVw, activex;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button2: TButton;
    Memo1: TMemo;
    Button1: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Edit1: TEdit;
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses   mshtml;

{$R *.dfm}



procedure TForm1.Button2Click(Sender: TObject);
var
StrStream:TStringStream;
SetNoteStr: string;
begin
  SetNoteStr :=Memo1.Text;
  StrStream:=TStringStream.Create(SetNoteStr);
  //WebBrowser1.Navigate('about:blank');
  try
    StrStream.Position:=0;
    (WebBrowser1.Document as IPersistStreamInit).Load(TStreamadapter.Create(StrStream));
  finally
    StrStream.Free;
  end;

  Edit1.SetFocus;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  with WebBrowser1.Document as IHTMLDocument2 do
  begin
    //改变字体的前景色
    execCommand('ForeColor', False, 'red');
    //改变字体的粗细
    execCommand('Bold', False, 1);
    //打开插入图片对话框,插入图片
    execCommand('InsertImage', True, '');
    //文本居中
    execCommand('JustifyCenter', False, 0);
    //执行撤销上一步操作
    execCommand('Undo', False, 0);
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  with WebBrowser1.Document as IHTMLDocument2 do
  begin
    //改变字体的前景色
    execCommand('ForeColor', False, 'red');
  end;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  with WebBrowser1.Document as IHTMLDocument2 do
  begin
    //改变字体的粗细
    execCommand('Bold', False, 1);
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  with WebBrowser1.Document as IHTMLDocument2 do
  begin
    //改变字体的粗细
    execCommand('FontSize', False, StrToInt(Edit1.Text));
  end;
end;

procedure TForm1.Button7Click(Sender: TObject);
var txt: string;
begin
  txt := IHtmlDocument2(WebBrowser1.Document).Body.outerHTML;
  Memo1.Text := txt;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Navigate('about:blank');
  (WebBrowser1.Document as IHTMLDocument2).Set_designMode('On');
end;

end.

你可能感兴趣的:(Delphi HTML顯示與編輯)